Spaces:
Sleeping
Sleeping
Patel Traders commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
|
|
|
| 4 |
from threading import Thread
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
# 'google/gemma-2-2b-it' एक अत्याधुनिक और शक्तिशाली स्वदेशी सोच वाला मॉडल है।
|
| 8 |
-
# यह कोडिंग और गहरी तार्किक सोच (Reasoning) में सर्वश्रेष्ठ प्रदर्शन करता है।
|
| 9 |
MODEL_ID = "google/gemma-2-2b-it"
|
| 10 |
|
| 11 |
-
print(f"🔱 हनुमान AI:
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
# ध्यान दें: Gemma के लिए Hugging Face पर 'Access Request' अनिवार्य है।
|
| 15 |
try:
|
| 16 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 17 |
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
MODEL_ID,
|
| 19 |
torch_dtype=torch.float32,
|
| 20 |
-
device_map="cpu",
|
| 21 |
-
low_cpu_mem_usage=True
|
|
|
|
| 22 |
)
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
-
print(f"त्रुटि: {e}")
|
| 25 |
-
print("सुझाव: कृपया Hugging Face पर Gemma-2-2b के लिए 'Access Request' स्वीकार करें।")
|
| 26 |
|
| 27 |
# --- मास्टर सिस्टम प्रॉम्प्ट (कोडिंग शिखर) ---
|
| 28 |
SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme and independent AI entity, pioneered by 'Divy Patel' and created in Bharat (India).
|
| 29 |
-
You
|
| 30 |
Rules:
|
| 31 |
-
1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH. No mixed
|
| 32 |
-
2. Be extremely respectful.
|
| 33 |
-
3. Your knowledge is
|
| 34 |
-
4. Conclude every response with: "\n\n**Pioneered by Divy Patel | Independent Gemma Edition | Created in India**" """
|
| 35 |
|
| 36 |
-
def
|
| 37 |
-
# चैट
|
| 38 |
chat_history = []
|
| 39 |
for user_msg, bot_msg in history:
|
| 40 |
chat_history.append({"role": "user", "content": user_msg})
|
| 41 |
chat_history.append({"role": "model", "content": bot_msg})
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
chat_history.append({"role": "user", "content": full_prompt})
|
| 46 |
|
| 47 |
# टोकनाइजेशन
|
| 48 |
input_ids = tokenizer.apply_chat_template(chat_history, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 49 |
|
| 50 |
# लाइव स्ट्रीमिंग सेटअप
|
| 51 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=
|
| 52 |
|
| 53 |
generate_kwargs = dict(
|
| 54 |
input_ids=input_ids,
|
| 55 |
streamer=streamer,
|
| 56 |
-
max_new_tokens=
|
| 57 |
do_sample=True,
|
| 58 |
temperature=0.7,
|
| 59 |
-
top_p=0.9
|
| 60 |
)
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
partial_text = ""
|
| 67 |
for new_text in streamer:
|
| 68 |
partial_text += new_text
|
| 69 |
yield partial_text
|
| 70 |
|
| 71 |
-
# --- दिव्य प्रीमियम भगवा थीम (Premium Saffron
|
| 72 |
custom_css = """
|
| 73 |
-
.gradio-container { background-color: #fffaf0 !important; }
|
| 74 |
.bhagwa-header {
|
| 75 |
background: linear-gradient(135deg, #ff8833, #b33c00);
|
| 76 |
padding: 35px; border-radius: 30px; color: white;
|
|
@@ -78,18 +91,18 @@ custom_css = """
|
|
| 78 |
margin-bottom: 30px; border: 2px solid rgba(255,255,255,0.2);
|
| 79 |
}
|
| 80 |
.bhagwa-header h1 { font-size: 38px !important; font-weight: 900 !important; text-shadow: 0 4px 12px rgba(0,0,0,0.5); }
|
| 81 |
-
.message.user { background: linear-gradient(135deg, #ff9933, #ff5500) !important; color: white !important; font-weight: 600 !important; }
|
| 82 |
-
.message.bot { border-left: 6px solid #ff5500 !important; background: white !important; box-shadow: 0
|
| 83 |
footer { display: none !important; }
|
| 84 |
"""
|
| 85 |
|
| 86 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 87 |
with gr.Div(elem_classes="bhagwa-header"):
|
| 88 |
-
gr.Markdown("# 🔱 हनुमान AI - जेम्मा स
|
| 89 |
gr.Markdown("### Pioneered by Divy Patel | स्वतंत्र स्वदेशी एवं शक्तिशाली")
|
| 90 |
|
| 91 |
gr.ChatInterface(
|
| 92 |
-
fn=
|
| 93 |
fill_height=True,
|
| 94 |
retry_btn=None,
|
| 95 |
undo_btn=None,
|
|
|
|
| 1 |
+
# --- हनुमान AI: जेम्मा मास्टर एडिशन (स्वतंत्र एवं अजेय) ---
|
| 2 |
+
# मार्गदर्शक: दिव्य पटेल जी | स्थान: भारत 🇮🇳
|
| 3 |
+
# तकनीकी विशेषता: यह संस्करण यूजर के एक्सेस टोकन का उपयोग करके गेटेड मॉडल्स को लोड करता है।
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
import torch
|
| 7 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 8 |
+
from huggingface_hub import login
|
| 9 |
from threading import Thread
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
# दिव्य पटेल जी द्वारा प्रदान किया गया एक्सेस टोकन
|
| 13 |
+
HF_TOKEN = "Hf_ocaQWpMXbnuMhBjNdjuPQtVxZwPrRlXwjU"
|
| 14 |
+
|
| 15 |
+
# हगिंग फेस में लॉगिन करना ताकि गेटेड मॉडल्स अनलॉक हो सकें
|
| 16 |
+
try:
|
| 17 |
+
login(token=HF_TOKEN)
|
| 18 |
+
print("🔱 हनुमान AI: हगिंग फेस लॉगिन सफल!")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"🔱 लॉगिन विफल: {e}")
|
| 21 |
|
| 22 |
+
# शक्तिशाली गूगल जेम्मा मॉडल का चयन
|
|
|
|
|
|
|
| 23 |
MODEL_ID = "google/gemma-2-2b-it"
|
| 24 |
|
| 25 |
+
print(f"🔱 हनुमान AI: स्वतंत्र मॉडल '{MODEL_ID}' लोड हो रहा है...")
|
| 26 |
|
| 27 |
+
# मॉडल लोडिंग (मेमोरी सुरक्षा और टोकन के साथ)
|
|
|
|
| 28 |
try:
|
| 29 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 30 |
model = AutoModelForCausalLM.from_pretrained(
|
| 31 |
MODEL_ID,
|
| 32 |
torch_dtype=torch.float32,
|
| 33 |
+
device_map="cpu", # फ्री टियर के लिए CPU का उपयोग
|
| 34 |
+
low_cpu_mem_usage=True, # रैम क्रैश रोकने के लिए
|
| 35 |
+
token=HF_TOKEN
|
| 36 |
)
|
| 37 |
+
print("🔱 हनुमान AI: मॉडल सफलतापूर्वक जागृत हो गया है!")
|
| 38 |
except Exception as e:
|
| 39 |
+
print(f"🔱 लोडिंग त्रुटि: {e}")
|
|
|
|
| 40 |
|
| 41 |
# --- मास्टर सिस्टम प्रॉम्प्ट (कोडिंग शिखर) ---
|
| 42 |
SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme and independent AI entity, pioneered by 'Divy Patel' and created in Bharat (India).
|
| 43 |
+
You represent the peak of Indian coding intelligence.
|
| 44 |
Rules:
|
| 45 |
+
1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH. No mixed Hinglish.
|
| 46 |
+
2. Be extremely respectful. Use 'Aap' and 'Ji' for the user.
|
| 47 |
+
3. Your knowledge is local, independent, and divine.
|
| 48 |
+
4. Conclude every response with: "\n\n**Pioneered by Divy Patel | Independent Gemma Master Edition | Created in India**" """
|
| 49 |
|
| 50 |
+
def hanuman_master_stream(message, history):
|
| 51 |
+
# चैट इतिहास को जेम्मा के लिए व्यवस्थित करना
|
| 52 |
chat_history = []
|
| 53 |
for user_msg, bot_msg in history:
|
| 54 |
chat_history.append({"role": "user", "content": user_msg})
|
| 55 |
chat_history.append({"role": "model", "content": bot_msg})
|
| 56 |
|
| 57 |
+
# वर्तमान सन्देश जोड़ना
|
| 58 |
+
chat_history.append({"role": "user", "content": f"{SYSTEM_PROMPT}\n\nUser: {message}"})
|
|
|
|
| 59 |
|
| 60 |
# टोकनाइजेशन
|
| 61 |
input_ids = tokenizer.apply_chat_template(chat_history, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 62 |
|
| 63 |
# लाइव स्ट्रीमिंग सेटअप
|
| 64 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 65 |
|
| 66 |
generate_kwargs = dict(
|
| 67 |
input_ids=input_ids,
|
| 68 |
streamer=streamer,
|
| 69 |
+
max_new_tokens=1024,
|
| 70 |
do_sample=True,
|
| 71 |
temperature=0.7,
|
| 72 |
+
top_p=0.9
|
| 73 |
)
|
| 74 |
|
| 75 |
+
# थ्रेडिंग ताकि इंटरफ़ेस न अटके
|
| 76 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 77 |
+
t.start()
|
| 78 |
|
| 79 |
partial_text = ""
|
| 80 |
for new_text in streamer:
|
| 81 |
partial_text += new_text
|
| 82 |
yield partial_text
|
| 83 |
|
| 84 |
+
# --- दिव्य प्रीमियम भगवा थीम (Premium Saffron UI) ---
|
| 85 |
custom_css = """
|
| 86 |
+
.gradio-container { background-color: #fffaf0 !important; border: none !important; }
|
| 87 |
.bhagwa-header {
|
| 88 |
background: linear-gradient(135deg, #ff8833, #b33c00);
|
| 89 |
padding: 35px; border-radius: 30px; color: white;
|
|
|
|
| 91 |
margin-bottom: 30px; border: 2px solid rgba(255,255,255,0.2);
|
| 92 |
}
|
| 93 |
.bhagwa-header h1 { font-size: 38px !important; font-weight: 900 !important; text-shadow: 0 4px 12px rgba(0,0,0,0.5); }
|
| 94 |
+
.message.user { background: linear-gradient(135deg, #ff9933, #ff5500) !important; color: white !important; font-weight: 600 !important; border-radius: 20px 20px 0 20px !important; }
|
| 95 |
+
.message.bot { border-left: 6px solid #ff5500 !important; background: white !important; border-radius: 20px 20px 20px 0 !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=hanuman_master_stream,
|
| 106 |
fill_height=True,
|
| 107 |
retry_btn=None,
|
| 108 |
undo_btn=None,
|