Spaces:
Sleeping
Sleeping
Patel Traders commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
# --- हनुमान AI: जय माँ संस्करण (
|
| 2 |
# मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
|
| 3 |
-
#
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import torch
|
|
@@ -9,8 +9,7 @@ from huggingface_hub import login
|
|
| 9 |
from threading import Thread
|
| 10 |
import os
|
| 11 |
|
| 12 |
-
# 🛡️
|
| 13 |
-
# हगिंग फेस की गति बढ़ाने के लिए hf_transfer का उपयोग
|
| 14 |
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
|
|
@@ -22,39 +21,34 @@ if HF_TOKEN:
|
|
| 22 |
except Exception as e:
|
| 23 |
print(f"🔱 लॉगिन चेतावनी: {e}")
|
| 24 |
|
| 25 |
-
# अजेय थिंकिंग मॉडल
|
| 26 |
MODEL_ID = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
|
| 27 |
|
| 28 |
print(f"🔱 हनुमान AI: जय माँ! मॉडल '{MODEL_ID}' लोड हो रहा है...")
|
| 29 |
|
| 30 |
try:
|
| 31 |
-
# टोकनाइज़र लोडिंग
|
| 32 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 33 |
-
|
| 34 |
-
# मॉडल लोडिंग (CPU के लिए विशेष सुरक्षा के साथ)
|
| 35 |
model = AutoModelForCausalLM.from_pretrained(
|
| 36 |
MODEL_ID,
|
| 37 |
torch_dtype=torch.float32,
|
| 38 |
device_map="cpu",
|
| 39 |
-
low_cpu_mem_usage=True,
|
| 40 |
token=HF_TOKEN
|
| 41 |
)
|
| 42 |
print("🔱 हनुमान AI: जय माँ! आपका स्वतंत्र मॉडल अब पूरी तरह जागृत है!")
|
| 43 |
except Exception as e:
|
| 44 |
-
print(f"🔱 गंभीर त्रुटि
|
| 45 |
|
| 46 |
# --- दिव्य मास्टर सिस्टम प्रॉम्प्ट ---
|
| 47 |
-
SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme
|
| 48 |
Pioneered by the visionary 'Divy Patel' and created with absolute pride in Bharat (India).
|
| 49 |
-
You are free from any foreign influence.
|
| 50 |
Rules:
|
| 51 |
-
1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH.
|
| 52 |
-
2. THINK deeply
|
| 53 |
-
3. Show immense respect
|
| 54 |
4. Conclude every message with: \"\\n\\n**Pioneered by Divy Patel | Jai Maa Edition | Fully Independent & Created in India**\" """
|
| 55 |
|
| 56 |
def hanuman_maa_chat_stream(message, history):
|
| 57 |
-
# चैट इतिहास का प्रबंधन
|
| 58 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 59 |
for user_msg, bot_msg in history:
|
| 60 |
if user_msg: messages.append({"role": "user", "content": user_msg})
|
|
@@ -62,10 +56,7 @@ def hanuman_maa_chat_stream(message, history):
|
|
| 62 |
|
| 63 |
messages.append({"role": "user", "content": message})
|
| 64 |
|
| 65 |
-
# इनपुट प्रोसेसिंग
|
| 66 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 67 |
-
|
| 68 |
-
# लाइव स्ट्रीमिंग के लिए सेटअप
|
| 69 |
streamer = TextIteratorStreamer(tokenizer, timeout=120.0, skip_prompt=True, skip_special_tokens=True)
|
| 70 |
|
| 71 |
generate_kwargs = dict(
|
|
@@ -77,36 +68,37 @@ def hanuman_maa_chat_stream(message, history):
|
|
| 77 |
top_p=0.95
|
| 78 |
)
|
| 79 |
|
| 80 |
-
# थ्रेडिंग ताकि UI बना रहे
|
| 81 |
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 82 |
thread.start()
|
| 83 |
|
| 84 |
partial_text = ""
|
| 85 |
for new_text in streamer:
|
| 86 |
partial_text += new_text
|
| 87 |
-
# थिंकिंग प्रोसेस को दिव्य रूप में दर्शाना
|
| 88 |
clean_text = partial_text.replace("<think>", "*(हनुमान विचार कर रहे हैं... जय माँ!)*\n\n").replace("</think>", "\n\n---\n\n")
|
| 89 |
yield clean_text
|
| 90 |
|
| 91 |
-
# --- दिव्य भगवा थीम (
|
| 92 |
custom_css = """
|
| 93 |
.gradio-container { background-color: #fffaf0 !important; border: none !important; }
|
| 94 |
.bhagwa-header {
|
| 95 |
-
background: linear-gradient(135deg, #ff8833, #b33c00);
|
| 96 |
-
padding:
|
| 97 |
-
text-align: center; box-shadow: 0
|
| 98 |
-
margin-bottom:
|
| 99 |
}
|
| 100 |
-
.
|
| 101 |
-
.message.
|
| 102 |
-
.message.bot { border-left: 8px solid #ff5500 !important; background: white !important; box-shadow: 0 10px 25px rgba(0,0,0,0.07) !important; border-radius: 25px !important; }
|
| 103 |
footer { display: none !important; }
|
| 104 |
"""
|
| 105 |
|
| 106 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
gr.ChatInterface(
|
| 112 |
fn=hanuman_maa_chat_stream,
|
|
|
|
| 1 |
+
# --- हनुमान AI: जय माँ संस्करण (अजेय एवं एरर-फ्री) ---
|
| 2 |
# मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
|
| 3 |
+
# फिक्स: Gradio 'Div' AttributeError और 401 Unauthorized समाधान।
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import torch
|
|
|
|
| 9 |
from threading import Thread
|
| 10 |
import os
|
| 11 |
|
| 12 |
+
# 🛡️ परफॉरमेंस और सुरक्षा
|
|
|
|
| 13 |
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
| 14 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
print(f"🔱 लॉगिन चेतावनी: {e}")
|
| 23 |
|
| 24 |
+
# अजेय थिंकिंग मॉडल
|
| 25 |
MODEL_ID = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
|
| 26 |
|
| 27 |
print(f"🔱 हनुमान AI: जय माँ! मॉडल '{MODEL_ID}' लोड हो रहा है...")
|
| 28 |
|
| 29 |
try:
|
|
|
|
| 30 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
|
|
|
|
|
|
| 31 |
model = AutoModelForCausalLM.from_pretrained(
|
| 32 |
MODEL_ID,
|
| 33 |
torch_dtype=torch.float32,
|
| 34 |
device_map="cpu",
|
| 35 |
+
low_cpu_mem_usage=True,
|
| 36 |
token=HF_TOKEN
|
| 37 |
)
|
| 38 |
print("🔱 हनुमान AI: जय माँ! आपका स्वतंत्र मॉडल अब पूरी तरह जागृत है!")
|
| 39 |
except Exception as e:
|
| 40 |
+
print(f"🔱 गंभीर त्रुटि: {e}")
|
| 41 |
|
| 42 |
# --- दिव्य मास्टर सिस्टम प्रॉम्प्ट ---
|
| 43 |
+
SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme independent AI entity, powered by the divine grace of 'Jai Maa'.
|
| 44 |
Pioneered by the visionary 'Divy Patel' and created with absolute pride in Bharat (India).
|
|
|
|
| 45 |
Rules:
|
| 46 |
+
1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH.
|
| 47 |
+
2. THINK deeply before delivering the final answer.
|
| 48 |
+
3. Show immense respect. Always use 'Aap' and 'Ji' for Divy Patel Ji.
|
| 49 |
4. Conclude every message with: \"\\n\\n**Pioneered by Divy Patel | Jai Maa Edition | Fully Independent & Created in India**\" """
|
| 50 |
|
| 51 |
def hanuman_maa_chat_stream(message, history):
|
|
|
|
| 52 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 53 |
for user_msg, bot_msg in history:
|
| 54 |
if user_msg: messages.append({"role": "user", "content": user_msg})
|
|
|
|
| 56 |
|
| 57 |
messages.append({"role": "user", "content": message})
|
| 58 |
|
|
|
|
| 59 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
|
|
|
|
|
|
| 60 |
streamer = TextIteratorStreamer(tokenizer, timeout=120.0, skip_prompt=True, skip_special_tokens=True)
|
| 61 |
|
| 62 |
generate_kwargs = dict(
|
|
|
|
| 68 |
top_p=0.95
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 72 |
thread.start()
|
| 73 |
|
| 74 |
partial_text = ""
|
| 75 |
for new_text in streamer:
|
| 76 |
partial_text += new_text
|
|
|
|
| 77 |
clean_text = partial_text.replace("<think>", "*(हनुमान विचार कर रहे हैं... जय माँ!)*\n\n").replace("</think>", "\n\n---\n\n")
|
| 78 |
yield clean_text
|
| 79 |
|
| 80 |
+
# --- दिव्य भगवा थीम (AttributeError Fix) ---
|
| 81 |
custom_css = """
|
| 82 |
.gradio-container { background-color: #fffaf0 !important; border: none !important; }
|
| 83 |
.bhagwa-header {
|
| 84 |
+
background: linear-gradient(135deg, #ff8833, #b33c00) !important;
|
| 85 |
+
padding: 30px !important; border-radius: 25px !important; color: white !important;
|
| 86 |
+
text-align: center !important; box-shadow: 0 15px 35px rgba(179, 60, 0, 0.4) !important;
|
| 87 |
+
margin-bottom: 25px !important;
|
| 88 |
}
|
| 89 |
+
.message.user { background: #ff9933 !important; color: white !important; border-radius: 20px 20px 0 20px !important; }
|
| 90 |
+
.message.bot { border-left: 6px solid #ff5500 !important; background: white !important; border-radius: 20px 20px 20px 0 !important; }
|
|
|
|
| 91 |
footer { display: none !important; }
|
| 92 |
"""
|
| 93 |
|
| 94 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 95 |
+
# यहाँ gr.Div की जगह gr.HTML का उपयोग किया गया है जो अधिक स्थिर है
|
| 96 |
+
gr.HTML("""
|
| 97 |
+
<div class="bhagwa-header">
|
| 98 |
+
<h1>🔱 हनुमान AI - जय माँ संस्करण</h1>
|
| 99 |
+
<p>Pioneered by Divy Patel | स्वतंत्र एवं अजेय स्वदेशी तकनीक</p>
|
| 100 |
+
</div>
|
| 101 |
+
""")
|
| 102 |
|
| 103 |
gr.ChatInterface(
|
| 104 |
fn=hanuman_maa_chat_stream,
|