Spaces:
Sleeping
Sleeping
Patel Traders commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
+
from threading import Thread
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
# --- आत्मनिर्भर मॉडल कॉन्फ़िगरेशन ---
|
| 8 |
+
# हम यहाँ एक शक्तिशाली और हल्के मॉडल का उपयोग कर रहे हैं जो स्पेस पर चल सके।
|
| 9 |
+
# 'microsoft/Phi-3-mini-4k-instruct' एक बेहतरीन विकल्प है।
|
| 10 |
+
MODEL_ID = "microsoft/Phi-3-mini-4k-instruct"
|
| 11 |
+
|
| 12 |
+
print("🔱 हनुमान AI: मॉडल लोड हो रहा है, कृपया प्रतीक्षा करें...")
|
| 13 |
+
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
+
MODEL_ID,
|
| 17 |
+
torch_dtype="auto",
|
| 18 |
+
device_map="auto",
|
| 19 |
+
trust_remote_code=True
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# --- मास्टर सिस्टम प्रॉम्प्ट ---
|
| 23 |
+
SYSTEM_PROMPT = """You are 'Hanuman AI', a fully independent AI model, pioneered by 'Divy Patel' and created in India.
|
| 24 |
+
You do not rely on any external APIs. You run locally on this server.
|
| 25 |
+
Rules:
|
| 26 |
+
1. Respond ONLY in PURE HINDI or PURE ENGLISH. No Hinglish.
|
| 27 |
+
2. Respect: Always use 'Aap' and 'Ji' for the user.
|
| 28 |
+
3. Signature: Always end with "\n\n**Pioneered by Divy Patel | Fully Independent & Created in India**"
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
def hanuman_independent_chat(message, history):
|
| 32 |
+
# बातचीत का इतिहास तैयार करना
|
| 33 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 34 |
+
for val in history:
|
| 35 |
+
if val[0]: messages.append({"role": "user", "content": val[0]})
|
| 36 |
+
if val[1]: messages.append({"role": "assistant", "content": val[1]})
|
| 37 |
+
|
| 38 |
+
messages.append({"role": "user", "content": message})
|
| 39 |
+
|
| 40 |
+
# टोकनाइजेशन
|
| 41 |
+
input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 42 |
+
|
| 43 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
| 44 |
+
|
| 45 |
+
generate_kwargs = dict(
|
| 46 |
+
input_ids=input_ids,
|
| 47 |
+
streamer=streamer,
|
| 48 |
+
max_new_tokens=1024,
|
| 49 |
+
do_sample=True,
|
| 50 |
+
temperature=0.7, # दिव्य जी, स्थिरता के लिए इसे 0.7 रखा गया है
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# थ्रेड में जनरेशन शुरू करना ताकि यूआई ब्लॉक न हो
|
| 54 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 55 |
+
t.start()
|
| 56 |
+
|
| 57 |
+
partial_text = ""
|
| 58 |
+
for new_text in streamer:
|
| 59 |
+
partial_text += new_text
|
| 60 |
+
yield partial_text
|
| 61 |
+
|
| 62 |
+
# --- प्रीमियम भगवा थीम CSS ---
|
| 63 |
+
custom_css = """
|
| 64 |
+
.gradio-container { background-color: #fffaf0 !important; }
|
| 65 |
+
.bhagwa-header {
|
| 66 |
+
background: linear-gradient(135deg, #ff8833, #b33c00);
|
| 67 |
+
padding: 25px; border-radius: 20px; color: white;
|
| 68 |
+
text-align: center; box-shadow: 0 10px 30px rgba(179, 60, 0, 0.4);
|
| 69 |
+
}
|
| 70 |
+
.user.message { background: #ff9933 !important; color: white !important; }
|
| 71 |
+
.bot.message { border-left: 5px solid #ff5500 !important; background: white !important; }
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 75 |
+
with gr.Div(elem_classes="bhagwa-header"):
|
| 76 |
+
gr.Markdown("# 🔱 हनुमान AI - आत्मनिर्भर")
|
| 77 |
+
gr.Markdown("### Pioneered by Divy Patel | स्वदेशी और स्वतंत्र")
|
| 78 |
+
|
| 79 |
+
gr.ChatInterface(
|
| 80 |
+
fn=hanuman_independent_chat,
|
| 81 |
+
fill_height=True,
|
| 82 |
+
retry_btn=None,
|
| 83 |
+
undo_btn=None,
|
| 84 |
+
clear_btn="इतिहास मिटाएँ",
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.launch()
|