Spaces:
Sleeping
Sleeping
Patel Traders commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
# मार्गदर्शक: दिव्य पटेल जी | स्थान: भारत 🇮🇳
|
| 3 |
-
# तकनीकी विशेषता: यह
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import torch
|
|
@@ -9,8 +10,8 @@ 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 |
# लॉगिन प्रक्रिया (सुरक्षित विधि)
|
|
@@ -21,15 +22,16 @@ if HF_TOKEN:
|
|
| 21 |
except Exception as e:
|
| 22 |
print(f"🔱 लॉगिन चेतावनी: {e}")
|
| 23 |
else:
|
| 24 |
-
print("🔱
|
| 25 |
|
| 26 |
-
# अजेय मॉडल:
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
print(f"🔱 हनुमान AI: स्वतंत्र मॉडल '{MODEL_ID}' लोड हो रहा है...")
|
| 30 |
|
| 31 |
try:
|
| 32 |
-
# टोकनाइज़र और मॉडल लोडिंग
|
| 33 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 34 |
model = AutoModelForCausalLM.from_pretrained(
|
| 35 |
MODEL_ID,
|
|
@@ -38,19 +40,20 @@ try:
|
|
| 38 |
low_cpu_mem_usage=True,
|
| 39 |
token=HF_TOKEN
|
| 40 |
)
|
| 41 |
-
print("🔱 हनुमान AI: स्वतंत्र मॉडल अब पूरी शक्ति के साथ सक्रिय है!")
|
| 42 |
except Exception as e:
|
| 43 |
print(f"🔱 गंभीर त्रुटि: {e}")
|
| 44 |
|
| 45 |
# --- मास्टर सिस्टम प्रॉम्प्ट (कोडिंग शिखर) ---
|
| 46 |
-
SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme and independent AI entity
|
|
|
|
| 47 |
Rules:
|
| 48 |
-
1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH.
|
| 49 |
-
2.
|
| 50 |
-
3.
|
| 51 |
-
4. Conclude every message with: \"\\n\\n**Pioneered by Divy Patel | Secure
|
| 52 |
|
| 53 |
-
def
|
| 54 |
# चैट इतिहास व्यवस्थित करना
|
| 55 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 56 |
for user_msg, bot_msg in history:
|
|
@@ -61,15 +64,15 @@ def hanuman_secure_chat(message, history):
|
|
| 61 |
|
| 62 |
# टोकनाइजेशन और स्ट्रीमिंग सेटअप
|
| 63 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 64 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=
|
| 65 |
|
| 66 |
generate_kwargs = dict(
|
| 67 |
input_ids=inputs,
|
| 68 |
streamer=streamer,
|
| 69 |
-
max_new_tokens=
|
| 70 |
do_sample=True,
|
| 71 |
-
temperature=0.
|
| 72 |
-
top_p=0.
|
| 73 |
)
|
| 74 |
|
| 75 |
# जनरेशन के लिए अलग थ्रेड
|
|
@@ -79,11 +82,13 @@ def hanuman_secure_chat(message, history):
|
|
| 79 |
partial_text = ""
|
| 80 |
for new_text in streamer:
|
| 81 |
partial_text += new_text
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
# --- दिव्य प्रीमियम भगवा थीम (Premium Saffron
|
| 85 |
custom_css = """
|
| 86 |
-
.gradio-container { background-color: #fffaf0 !important; }
|
| 87 |
.bhagwa-header {
|
| 88 |
background: linear-gradient(135deg, #ff8833, #b33c00);
|
| 89 |
padding: 30px; border-radius: 25px; color: white;
|
|
@@ -91,18 +96,18 @@ custom_css = """
|
|
| 91 |
margin-bottom: 25px; border: 2px solid rgba(255,255,255,0.2);
|
| 92 |
}
|
| 93 |
.bhagwa-header h1 { font-size: 36px !important; font-weight: 900 !important; text-shadow: 0 4px 10px rgba(0,0,0,0.5); }
|
| 94 |
-
.message.user { background: linear-gradient(135deg, #ff9933, #ff5500) !important; color: white !important; font-weight: 600 !important; }
|
| 95 |
-
.message.bot { border-left: 6px solid #ff5500 !important; background: white !important; box-shadow: 0 5px 15px rgba(0,0,0,0.05) !important; }
|
| 96 |
footer { display: none !important; }
|
| 97 |
"""
|
| 98 |
|
| 99 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 100 |
with gr.Div(elem_classes="bhagwa-header"):
|
| 101 |
-
gr.Markdown("# 🔱 हनुमान AI -
|
| 102 |
-
gr.Markdown("### Pioneered by Divy Patel |
|
| 103 |
|
| 104 |
gr.ChatInterface(
|
| 105 |
-
fn=
|
| 106 |
fill_height=True,
|
| 107 |
retry_btn=None,
|
| 108 |
undo_btn=None,
|
|
|
|
| 1 |
+
```python
|
| 2 |
+
# --- हनुमान AI: सुरक्षित एवं आत्मनिर्भर थिंकिंग संस्करण ---
|
| 3 |
# मार्गदर्शक: दिव्य पटेल जी | स्थान: भारत 🇮🇳
|
| 4 |
+
# तकनीकी विशेषता: यह कोड HF_TOKEN को सुरक्षित रूप से 'Secrets' से प्राप्त करता है।
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import torch
|
|
|
|
| 10 |
from threading import Thread
|
| 11 |
import os
|
| 12 |
|
| 13 |
+
# 🛡️ सुरक्षा चक्र: टोकन अब सीधे कोड में नहीं है
|
| 14 |
+
# यह कोड आपके द्वारा Hugging Face Settings में जोड़े गए 'HF_TOKEN' को ऑटोमेटिक उठा लेगा।
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
|
| 17 |
# लॉगिन प्रक्रिया (सुरक्षित विधि)
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
print(f"🔱 लॉगिन चेतावनी: {e}")
|
| 24 |
else:
|
| 25 |
+
print("🔱 सावधानी: HF_TOKEN नहीं मिला। कृपया सेटिंग्स में Secret जोड़ें।")
|
| 26 |
|
| 27 |
+
# अजेय थिंकिंग मॉडल: DeepSeek-R1-Distill-Qwen-1.5B
|
| 28 |
+
# यह आकार में छोटा है पर तार्किक सोच में विशाल है।
|
| 29 |
+
MODEL_ID = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
|
| 30 |
|
| 31 |
print(f"🔱 हनुमान AI: स्वतंत्र मॉडल '{MODEL_ID}' लोड हो रहा है...")
|
| 32 |
|
| 33 |
try:
|
| 34 |
+
# टोकनाइज़र और मॉडल लोडिंग (CPU के लिए ऑप्टिमाइज़्ड)
|
| 35 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 36 |
model = AutoModelForCausalLM.from_pretrained(
|
| 37 |
MODEL_ID,
|
|
|
|
| 40 |
low_cpu_mem_usage=True,
|
| 41 |
token=HF_TOKEN
|
| 42 |
)
|
| 43 |
+
print("🔱 हनुमान AI: स्वतंत्र थिंकिंग मॉडल अब पूरी शक्ति के साथ सक्रिय है!")
|
| 44 |
except Exception as e:
|
| 45 |
print(f"🔱 गंभीर त्रुटि: {e}")
|
| 46 |
|
| 47 |
# --- मास्टर सिस्टम प्रॉम्प्ट (कोडिंग शिखर) ---
|
| 48 |
+
SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme and independent AI entity with deep reasoning powers.
|
| 49 |
+
Pioneered by 'Divy Patel' and created in Bharat (India).
|
| 50 |
Rules:
|
| 51 |
+
1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH. No Hinglish.
|
| 52 |
+
2. THINK before answering to ensure divine accuracy and logic.
|
| 53 |
+
3. Be extremely respectful. Use honorifics like 'Aap' and 'Ji' for the user.
|
| 54 |
+
4. Conclude every message with: \"\\n\\n**Pioneered by Divy Patel | Secure Thinking Edition | Created in India**\" """
|
| 55 |
|
| 56 |
+
def hanuman_thinking_stream(message, history):
|
| 57 |
# चैट इतिहास व्यवस्थित करना
|
| 58 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 59 |
for user_msg, bot_msg in history:
|
|
|
|
| 64 |
|
| 65 |
# टोकनाइजेशन और स्ट्रीमिंग सेटअप
|
| 66 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 67 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=90.0, skip_prompt=True, skip_special_tokens=True)
|
| 68 |
|
| 69 |
generate_kwargs = dict(
|
| 70 |
input_ids=inputs,
|
| 71 |
streamer=streamer,
|
| 72 |
+
max_new_tokens=1500,
|
| 73 |
do_sample=True,
|
| 74 |
+
temperature=0.6,
|
| 75 |
+
top_p=0.95
|
| 76 |
)
|
| 77 |
|
| 78 |
# जनरेशन के लिए अलग थ्रेड
|
|
|
|
| 82 |
partial_text = ""
|
| 83 |
for new_text in streamer:
|
| 84 |
partial_text += new_text
|
| 85 |
+
# थिंकिंग प्रोसेस को सुंदर रूप में दिखाना
|
| 86 |
+
clean_text = partial_text.replace("<think>", "*(हनुमान विचार कर रहे हैं...)*\n\n").replace("</think>", "\n\n---\n\n")
|
| 87 |
+
yield clean_text
|
| 88 |
|
| 89 |
+
# --- दिव्य प्रीमियम भगवा थीम (Premium Saffron UI) ---
|
| 90 |
custom_css = """
|
| 91 |
+
.gradio-container { background-color: #fffaf0 !important; border: none !important; }
|
| 92 |
.bhagwa-header {
|
| 93 |
background: linear-gradient(135deg, #ff8833, #b33c00);
|
| 94 |
padding: 30px; border-radius: 25px; color: white;
|
|
|
|
| 96 |
margin-bottom: 25px; border: 2px solid rgba(255,255,255,0.2);
|
| 97 |
}
|
| 98 |
.bhagwa-header h1 { font-size: 36px !important; font-weight: 900 !important; text-shadow: 0 4px 10px rgba(0,0,0,0.5); }
|
| 99 |
+
.message.user { background: linear-gradient(135deg, #ff9933, #ff5500) !important; color: white !important; font-weight: 600 !important; border: none !important; }
|
| 100 |
+
.message.bot { border-left: 6px solid #ff5500 !important; background: white !important; box-shadow: 0 5px 15px rgba(0,0,0,0.05) !important; border-radius: 20px !important; }
|
| 101 |
footer { display: none !important; }
|
| 102 |
"""
|
| 103 |
|
| 104 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 105 |
with gr.Div(elem_classes="bhagwa-header"):
|
| 106 |
+
gr.Markdown("# 🔱 हनुमान AI - थिंकिंग मास्टर")
|
| 107 |
+
gr.Markdown("### Pioneered by Divy Patel | पूर्णतः सुरक्षित एवं स्वतंत्र")
|
| 108 |
|
| 109 |
gr.ChatInterface(
|
| 110 |
+
fn=hanuman_thinking_stream,
|
| 111 |
fill_height=True,
|
| 112 |
retry_btn=None,
|
| 113 |
undo_btn=None,
|