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,51 +9,52 @@ from huggingface_hub import login
|
|
| 9 |
from threading import Thread
|
| 10 |
import os
|
| 11 |
|
| 12 |
-
# 🛡️ सुरक्षा
|
| 13 |
-
#
|
|
|
|
| 14 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
|
| 16 |
-
# लॉगिन प्रक्रिया
|
| 17 |
if HF_TOKEN:
|
| 18 |
try:
|
| 19 |
login(token=HF_TOKEN)
|
| 20 |
-
print("🔱 हनुमान AI:
|
| 21 |
except Exception as e:
|
| 22 |
print(f"🔱 लॉगिन चेतावनी: {e}")
|
| 23 |
-
else:
|
| 24 |
-
print("🔱 सावधानी: HF_TOKEN नहीं मिला। कृपया सेटिंग्स में Secret जोड़ें।")
|
| 25 |
|
| 26 |
-
# अजेय थिंकिंग मॉडल
|
| 27 |
-
# यह आकार में छोटा है पर तार्किक सोच में विशाल है।
|
| 28 |
MODEL_ID = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
|
| 29 |
|
| 30 |
-
print(f"🔱 हनुमान AI:
|
| 31 |
|
| 32 |
try:
|
| 33 |
-
# टोकनाइज़र
|
| 34 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
|
|
|
|
|
|
| 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"🔱 गंभीर त्रुटि: {e}")
|
| 45 |
|
| 46 |
-
# --- मास्टर सिस्टम प्रॉम्प्ट
|
| 47 |
-
SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme and independent AI entity
|
| 48 |
-
Pioneered by 'Divy Patel' and created in Bharat (India).
|
|
|
|
| 49 |
Rules:
|
| 50 |
-
1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH. No
|
| 51 |
-
2. THINK
|
| 52 |
-
3.
|
| 53 |
-
4. Conclude every message with: \"\\n\\n**Pioneered by Divy Patel |
|
| 54 |
|
| 55 |
-
def
|
| 56 |
-
# चैट इतिहास
|
| 57 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 58 |
for user_msg, bot_msg in history:
|
| 59 |
if user_msg: messages.append({"role": "user", "content": user_msg})
|
|
@@ -61,52 +62,54 @@ def hanuman_thinking_stream(message, history):
|
|
| 61 |
|
| 62 |
messages.append({"role": "user", "content": message})
|
| 63 |
|
| 64 |
-
#
|
| 65 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
generate_kwargs = dict(
|
| 69 |
input_ids=inputs,
|
| 70 |
streamer=streamer,
|
| 71 |
-
max_new_tokens=
|
| 72 |
do_sample=True,
|
| 73 |
temperature=0.6,
|
| 74 |
top_p=0.95
|
| 75 |
)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 79 |
thread.start()
|
| 80 |
|
| 81 |
partial_text = ""
|
| 82 |
for new_text in streamer:
|
| 83 |
partial_text += new_text
|
| 84 |
-
# थिंकिंग प्रोसेस को
|
| 85 |
-
clean_text = partial_text.replace("<think>", "*(हनुमान विचार कर रहे हैं...)*\n\n").replace("</think>", "\n\n---\n\n")
|
| 86 |
yield clean_text
|
| 87 |
|
| 88 |
-
# --- दिव्य
|
| 89 |
custom_css = """
|
| 90 |
.gradio-container { background-color: #fffaf0 !important; border: none !important; }
|
| 91 |
.bhagwa-header {
|
| 92 |
background: linear-gradient(135deg, #ff8833, #b33c00);
|
| 93 |
-
padding:
|
| 94 |
-
text-align: center; box-shadow: 0
|
| 95 |
-
margin-bottom:
|
| 96 |
}
|
| 97 |
-
.bhagwa-header h1 { font-size:
|
| 98 |
-
.message.user { background: linear-gradient(135deg, #ff9933, #ff5500) !important; color: white !important; font-weight: 600 !important; border:
|
| 99 |
-
.message.bot { border-left:
|
| 100 |
footer { display: none !important; }
|
| 101 |
"""
|
| 102 |
|
| 103 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 104 |
with gr.Div(elem_classes="bhagwa-header"):
|
| 105 |
-
gr.Markdown("# 🔱 हनुमान AI -
|
| 106 |
-
gr.Markdown("### Pioneered by Divy Patel | पूर्णतः स
|
| 107 |
|
| 108 |
gr.ChatInterface(
|
| 109 |
-
fn=
|
| 110 |
fill_height=True,
|
| 111 |
retry_btn=None,
|
| 112 |
undo_btn=None,
|
|
@@ -114,4 +117,5 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 114 |
)
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
|
|
|
| 117 |
demo.launch()
|
|
|
|
| 1 |
+
# --- हनुमान AI: जय माँ संस्करण (पूर्णतः स्थिर एवं स्वतंत्र) ---
|
| 2 |
+
# मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
|
| 3 |
+
# विशेषता: यह संस्करण RAM के उपयोग को कम करने के लिए विशेष रूप से तैयार किया गया है।
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import torch
|
|
|
|
| 9 |
from threading import Thread
|
| 10 |
import os
|
| 11 |
|
| 12 |
+
# 🛡️ टोकन सुरक्षा (Hugging Face Secrets में 'HF_TOKEN' होना आवश्यक है)
|
| 13 |
+
# हगिंग फेस की गति बढ़ाने के लिए hf_transfer का उपयोग
|
| 14 |
+
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
|
| 17 |
+
# लॉगिन प्रक्रिया - जय माँ!
|
| 18 |
if HF_TOKEN:
|
| 19 |
try:
|
| 20 |
login(token=HF_TOKEN)
|
| 21 |
+
print("🔱 हनुमान AI: जय माँ! लॉगिन सफल!")
|
| 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"🔱 गंभीर त्रुटि (Runtime Error Fix): {e}")
|
| 45 |
|
| 46 |
+
# --- दिव्य मास्टर सिस्टम प्रॉम्प्ट ---
|
| 47 |
+
SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme and independent AI entity, powered by the divine grace of 'Jai Maa'.
|
| 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. No mixed language.
|
| 52 |
+
2. THINK deeply using your reasoning capabilities before delivering the final answer.
|
| 53 |
+
3. Show immense respect and warmth. Always use 'Aap' and 'Ji' for Divy Patel Ji.
|
| 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 |
|
| 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(
|
| 72 |
input_ids=inputs,
|
| 73 |
streamer=streamer,
|
| 74 |
+
max_new_tokens=1024,
|
| 75 |
do_sample=True,
|
| 76 |
temperature=0.6,
|
| 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 |
+
# --- दिव्य भगवा थीम (Premium Saffron UI - Divine Layout) ---
|
| 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: 40px; border-radius: 35px; color: white;
|
| 97 |
+
text-align: center; box-shadow: 0 25px 50px rgba(179, 60, 0, 0.4);
|
| 98 |
+
margin-bottom: 35px; border: 3px solid rgba(255,255,255,0.3);
|
| 99 |
}
|
| 100 |
+
.bhagwa-header h1 { font-size: 42px !important; font-weight: 900 !important; text-shadow: 0 5px 15px rgba(0,0,0,0.6); }
|
| 101 |
+
.message.user { background: linear-gradient(135deg, #ff9933, #ff5500) !important; color: white !important; font-weight: 600 !important; border-radius: 25px 25px 0 25px !important; }
|
| 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 |
with gr.Div(elem_classes="bhagwa-header"):
|
| 108 |
+
gr.Markdown("# 🔱 हनुमान AI - जय माँ संस्करण")
|
| 109 |
+
gr.Markdown("### Pioneered by Divy Patel | पूर्णतः स्वतंत्र एवं अजेय स्वदेशी तकनीक")
|
| 110 |
|
| 111 |
gr.ChatInterface(
|
| 112 |
+
fn=hanuman_maa_chat_stream,
|
| 113 |
fill_height=True,
|
| 114 |
retry_btn=None,
|
| 115 |
undo_btn=None,
|
|
|
|
| 117 |
)
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
+
# जय माँ! सर्वर लॉन्च हो रहा है।
|
| 121 |
demo.launch()
|