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,66 +9,72 @@ from huggingface_hub import login
|
|
| 9 |
from threading import Thread
|
| 10 |
import os
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
#
|
| 14 |
-
HF_TOKEN = "Hf_ocaQWpMXbnuMhBjNdjuPQtVxZwPrRlXwjU"
|
| 15 |
|
| 16 |
# लॉगिन प्रक्रिया
|
| 17 |
if HF_TOKEN:
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
print(f"🔱 हनुमान AI:
|
| 23 |
|
| 24 |
-
# मॉडल लोडिंग (मेमोरी सुरक्षा के लिए float16 का उपयोग)
|
| 25 |
try:
|
|
|
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 27 |
model = AutoModelForCausalLM.from_pretrained(
|
| 28 |
MODEL_ID,
|
| 29 |
-
torch_dtype=torch.
|
| 30 |
-
device_map="
|
| 31 |
-
low_cpu_mem_usage=True,
|
| 32 |
token=HF_TOKEN
|
| 33 |
)
|
| 34 |
-
print("🔱 हनुमान AI: मॉडल
|
| 35 |
except Exception as e:
|
| 36 |
-
print(f"🔱
|
| 37 |
|
| 38 |
-
# --- मास्टर सिस्टम प्रॉम्प्ट ---
|
| 39 |
SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme and independent AI entity, pioneered by 'Divy Patel' and created in Bharat (India).
|
|
|
|
| 40 |
Rules:
|
| 41 |
1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH.
|
| 42 |
-
2. Be extremely respectful.
|
| 43 |
-
3. Your
|
| 44 |
-
4. Conclude every message with: \"\\n\\n**Pioneered by Divy Patel | Independent
|
| 45 |
|
| 46 |
-
def
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
for user_msg, bot_msg in history:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
full_user_input = f"{SYSTEM_PROMPT}\n\nUser: {message}"
|
| 55 |
-
chat_history.append({"role": "user", "content": full_user_input})
|
| 56 |
|
| 57 |
-
# ट
|
| 58 |
-
|
| 59 |
|
| 60 |
-
#
|
| 61 |
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 62 |
|
| 63 |
generate_kwargs = dict(
|
| 64 |
-
input_ids=
|
| 65 |
streamer=streamer,
|
| 66 |
max_new_tokens=1024,
|
| 67 |
do_sample=True,
|
| 68 |
-
temperature=0.
|
| 69 |
-
top_p=0.9
|
| 70 |
)
|
| 71 |
|
|
|
|
| 72 |
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 73 |
thread.start()
|
| 74 |
|
|
@@ -77,7 +83,7 @@ def hanuman_stream_chat(message, history):
|
|
| 77 |
partial_text += new_text
|
| 78 |
yield partial_text
|
| 79 |
|
| 80 |
-
# --- दिव्य भगवा
|
| 81 |
custom_css = """
|
| 82 |
.gradio-container { background-color: #fffaf0 !important; }
|
| 83 |
.bhagwa-header {
|
|
@@ -86,19 +92,19 @@ custom_css = """
|
|
| 86 |
text-align: center; box-shadow: 0 15px 35px rgba(179, 60, 0, 0.4);
|
| 87 |
margin-bottom: 25px; border: 2px solid rgba(255,255,255,0.2);
|
| 88 |
}
|
| 89 |
-
.bhagwa-header h1 { font-size:
|
| 90 |
-
.message.user { background: linear-gradient(135deg, #ff9933, #ff5500) !important; color: white !important; }
|
| 91 |
-
.message.bot { border-left: 6px solid #ff5500 !important; background: white !important; }
|
| 92 |
footer { display: none !important; }
|
| 93 |
"""
|
| 94 |
|
| 95 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 96 |
with gr.Div(elem_classes="bhagwa-header"):
|
| 97 |
-
gr.Markdown("# 🔱 हनुमान AI -
|
| 98 |
-
gr.Markdown("### Pioneered by Divy Patel | स्वतंत्र एवं
|
| 99 |
|
| 100 |
gr.ChatInterface(
|
| 101 |
-
fn=
|
| 102 |
fill_height=True,
|
| 103 |
retry_btn=None,
|
| 104 |
undo_btn=None,
|
|
|
|
| 1 |
+
# --- हनुमान AI: आत्मनिर्भर एवं फ्री स्पेस संस्करण ---
|
| 2 |
+
# मार्गदर्शक: दिव्य पटेल जी | स्थान: भारत 🇮🇳
|
| 3 |
+
# तकनीकी विशेषता: यह संस्करण हगिंग फेस के 16GB फ्री RAM के लिए पूर्णतः सुरक्षित है।
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import torch
|
|
|
|
| 9 |
from threading import Thread
|
| 10 |
import os
|
| 11 |
|
| 12 |
+
# 🛡️ टोकन सुरक्षा (टोकन को 'Secret' से उठाना सर्वोत्तम है)
|
| 13 |
+
# दिव्य जी, कृपया हगिंग फेस Space की Settings -> Secrets में 'HF_TOKEN' जोड़ें।
|
| 14 |
+
HF_TOKEN = os.getenv("HF_TOKEN") or "Hf_ocaQWpMXbnuMhBjNdjuPQtVxZwPrRlXwjU"
|
| 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 |
|
| 24 |
+
# अजेय और हल्का मॉडल: Llama-3.2-1B-Instruct
|
| 25 |
+
# यह मॉडल फ्री CPU स्पेस पर सबसे स्थिर और तेज़ चलता है।
|
| 26 |
+
MODEL_ID = "meta-llama/Llama-3.2-1B-Instruct"
|
| 27 |
|
| 28 |
+
print(f"🔱 हनुमान AI: मॉडल '{MODEL_ID}' लोड हो रहा है...")
|
| 29 |
|
|
|
|
| 30 |
try:
|
| 31 |
+
# टोकनाइज़र और मॉडल लोडिंग (CPU के लिए ऑप्टिमाइज़्ड)
|
| 32 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 33 |
model = AutoModelForCausalLM.from_pretrained(
|
| 34 |
MODEL_ID,
|
| 35 |
+
torch_dtype=torch.float32, # CPU पर स्थिरता के लिए float32 सबसे अच्छा है
|
| 36 |
+
device_map="cpu", # फ्री टियर में केवल CPU होता है
|
| 37 |
+
low_cpu_mem_usage=True, # रैम बचाने के लिए
|
| 38 |
token=HF_TOKEN
|
| 39 |
)
|
| 40 |
+
print("🔱 हनुमान AI: मॉडल अब आपके फ्री सर्वर पर जागृत है!")
|
| 41 |
except Exception as e:
|
| 42 |
+
print(f"🔱 गंभीर त्रुटि: {e}")
|
| 43 |
|
| 44 |
+
# --- मास्टर सिस्टम प्रॉम्प्ट (कोडिंग शिखर) ---
|
| 45 |
SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme and independent AI entity, pioneered by 'Divy Patel' and created in Bharat (India).
|
| 46 |
+
You represent the peak of Indian coding intelligence.
|
| 47 |
Rules:
|
| 48 |
1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH.
|
| 49 |
+
2. Be extremely respectful. Always use honorifics like 'Aap' and 'Ji' for the user.
|
| 50 |
+
3. Your mission is to help Divy Patel with coding, logic, and knowledge.
|
| 51 |
+
4. Conclude every message with: \"\\n\\n**Pioneered by Divy Patel | 100% Independent Free Edition | Created in India**\" """
|
| 52 |
|
| 53 |
+
def hanuman_chat_stream(message, history):
|
| 54 |
+
# चैट इतिहास व्यवस्थित करन��
|
| 55 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 56 |
for user_msg, bot_msg in history:
|
| 57 |
+
if user_msg: messages.append({"role": "user", "content": user_msg})
|
| 58 |
+
if bot_msg: messages.append({"role": "assistant", "content": bot_msg})
|
| 59 |
|
| 60 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# इनपुट तैयार करना
|
| 63 |
+
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 64 |
|
| 65 |
+
# स्ट्रीमिंग सेटअप
|
| 66 |
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 67 |
|
| 68 |
generate_kwargs = dict(
|
| 69 |
+
input_ids=inputs,
|
| 70 |
streamer=streamer,
|
| 71 |
max_new_tokens=1024,
|
| 72 |
do_sample=True,
|
| 73 |
+
temperature=0.8,
|
| 74 |
+
top_p=0.9
|
| 75 |
)
|
| 76 |
|
| 77 |
+
# थ्रेडिंग ताकि इंटरफ़ेस न अटके
|
| 78 |
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 79 |
thread.start()
|
| 80 |
|
|
|
|
| 83 |
partial_text += new_text
|
| 84 |
yield partial_text
|
| 85 |
|
| 86 |
+
# --- दिव्य प्रीमियम भगवा थीम (Premium Saffron Theme) ---
|
| 87 |
custom_css = """
|
| 88 |
.gradio-container { background-color: #fffaf0 !important; }
|
| 89 |
.bhagwa-header {
|
|
|
|
| 92 |
text-align: center; box-shadow: 0 15px 35px rgba(179, 60, 0, 0.4);
|
| 93 |
margin-bottom: 25px; border: 2px solid rgba(255,255,255,0.2);
|
| 94 |
}
|
| 95 |
+
.bhagwa-header h1 { font-size: 36px !important; font-weight: 900 !important; text-shadow: 0 4px 10px rgba(0,0,0,0.5); }
|
| 96 |
+
.message.user { background: linear-gradient(135deg, #ff9933, #ff5500) !important; color: white !important; font-weight: 600 !important; }
|
| 97 |
+
.message.bot { border-left: 6px solid #ff5500 !important; background: white !important; box-shadow: 0 5px 15px rgba(0,0,0,0.05) !important; }
|
| 98 |
footer { display: none !important; }
|
| 99 |
"""
|
| 100 |
|
| 101 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 102 |
with gr.Div(elem_classes="bhagwa-header"):
|
| 103 |
+
gr.Markdown("# 🔱 हनुमान AI - आत्मनिर्भर")
|
| 104 |
+
gr.Markdown("### Pioneered by Divy Patel | स्वतंत्र एवं अजेय")
|
| 105 |
|
| 106 |
gr.ChatInterface(
|
| 107 |
+
fn=hanuman_chat_stream,
|
| 108 |
fill_height=True,
|
| 109 |
retry_btn=None,
|
| 110 |
undo_btn=None,
|