Patel Traders commited on
Commit
f617aee
·
verified ·
1 Parent(s): 543ca49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -38
app.py CHANGED
@@ -1,54 +1,47 @@
1
- # --- हनुमान AI: जय माँ संस्करण (Gradio 6.0 अजेय फिक्स) ---
2
  # मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
- # ि्स: ChatInterface TypeError और CSS Warning पूर्ण समाधान
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
  os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
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
 
24
- # ििंग मॉडल
25
- MODEL_ID = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
26
-
27
- print(f"🔱 हनुमान AI: जय माँ! मॉडल '{MODEL_ID}' लोड हो रहा है...")
28
 
29
  try:
30
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
 
31
  model = AutoModelForCausalLM.from_pretrained(
32
  MODEL_ID,
33
  torch_dtype=torch.float32,
34
  device_map="cpu",
35
- low_cpu_mem_usage=True,
36
- token=HF_TOKEN
37
  )
38
- print("🔱 हनुमान AI: जय माँ! आपका स्वतंत्र मॉडल अब पू तर जागृत है!")
39
  except Exception as e:
40
  print(f"🔱 गंभीर त्रुटि: {e}")
41
 
42
- # --- दिव्य मास्टर सिस्टम प्रॉम्प्ट ---
43
  SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme independent AI entity, powered by the divine grace of 'Jai Maa'.
44
  Pioneered by the visionary 'Divy Patel' and created with absolute pride in Bharat (India).
45
  Rules:
46
  1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH.
47
- 2. THINK deeply before delivering the final answer.
48
  3. Show immense respect. Always use 'Aap' and 'Ji' for Divy Patel Ji.
49
- 4. Conclude every message with: \"\\n\\n**Pioneered by Divy Patel | Jai Maa Edition | Fully Independent & Created in India**\" """
50
 
51
  def hanuman_maa_chat_stream(message, history):
 
52
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
53
  for user_msg, bot_msg in history:
54
  if user_msg: messages.append({"role": "user", "content": user_msg})
@@ -56,29 +49,33 @@ def hanuman_maa_chat_stream(message, history):
56
 
57
  messages.append({"role": "user", "content": message})
58
 
59
- inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
60
- streamer = TextIteratorStreamer(tokenizer, timeout=120.0, skip_prompt=True, skip_special_tokens=True)
 
 
 
 
61
 
62
  generate_kwargs = dict(
63
- input_ids=inputs,
64
  streamer=streamer,
65
  max_new_tokens=1024,
 
66
  do_sample=True,
67
- temperature=0.6,
68
- top_p=0.95
69
  )
70
 
 
71
  thread = Thread(target=model.generate, kwargs=generate_kwargs)
72
  thread.start()
73
 
74
  partial_text = ""
75
  for new_text in streamer:
76
  partial_text += new_text
77
- # थिंकिंग प्रोसेस को दिव्य रूप में दर्शाना
78
- clean_text = partial_text.replace("<think>", "*(हनुमान विचार कर रहे हैं... जय माँ!)*\n\n").replace("</think>", "\n\n---\n\n")
79
- yield clean_text
80
 
81
- # --- दिव्य भगवा थीम (HTML + CSS) नए Gradio 6.0 के अनुकूल ---
82
  divine_ui = """
83
  <style>
84
  .gradio-container { background-color: #fffaf0 !important; }
@@ -88,25 +85,22 @@ divine_ui = """
88
  text-align: center; box-shadow: 0 15px 35px rgba(179, 60, 0, 0.4);
89
  margin-bottom: 25px;
90
  }
91
- .bhagwa-header h1 { font-size: 34px !important; font-weight: 900 !important; margin-bottom: 5px; }
92
- .bhagwa-header p { font-size: 16px !important; opacity: 0.9; }
93
  </style>
94
  <div class="bhagwa-header">
95
  <h1>🔱 हनुमान AI - जय माँ संस्करण</h1>
96
- <p>Pioneered by Divy Patel | स्वतंत्र एवं अजेय स्वदेशी तकनीक</p>
97
  </div>
98
  """
99
 
100
- # यहाँ से css और theme हटा दिए गए हैं ताकि Warning न आए
101
  with gr.Blocks() as demo:
102
  gr.HTML(divine_ui)
103
 
104
- # यहाँ से पुराने बटन हटा दिए गए हैं ताकि TypeError न आए
105
  gr.ChatInterface(
106
  fn=hanuman_maa_chat_stream,
107
  fill_height=True
108
  )
109
 
110
  if __name__ == "__main__":
111
- # जय माँ! सर्वर लॉन्च हो रहा है।
112
  demo.launch()
 
1
+ # --- हनुमान AI: सुपरफास्ट लॉिक एवं जय माँ संस्करण ---
2
  # मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
+ # िशेषता: यह संसकरण CPU पर बसे तेज़ गति और बेहतरीन रीज़निंग लिए है
4
 
5
  import gradio as gr
6
  import torch
7
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
 
8
  from threading import Thread
9
  import os
10
 
11
+ # 🛡️ CPU ्बो स्पीड सेटिंग
12
+ torch.set_num_threads(2)
13
  os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
14
+ os.environ["OMP_NUM_THREADS"] = "2"
15
 
16
+ # िका सबसे तेज़ और चतुर 1.5B ॉडल (बिन थिंकिंग ओवरहेड के)
17
+ MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
 
 
 
 
 
18
 
19
+ print(f"🔱 हनुमान AI: जय माँ! सुपरफास्ट लॉजिक मॉडल '{MODEL_ID}' लोड हो रहा है...")
 
 
 
20
 
21
  try:
22
+ # इसमें किसी टोकन या गेटेड एक्सेस की आवश्यकता नहीं है, यह पूर्णतः स्वतंत्र है!
23
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
24
  model = AutoModelForCausalLM.from_pretrained(
25
  MODEL_ID,
26
  torch_dtype=torch.float32,
27
  device_map="cpu",
28
+ low_cpu_mem_usage=True
 
29
  )
30
+ print("🔱 हनुमान AI: जय माँ! अजेय तेज़ मॉडल सक्िय है!")
31
  except Exception as e:
32
  print(f"🔱 गंभीर त्रुटि: {e}")
33
 
34
+ # --- मास्टर सिस्टम प्रॉम्प्ट (जय माँ शक्ति) ---
35
  SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme independent AI entity, powered by the divine grace of 'Jai Maa'.
36
  Pioneered by the visionary 'Divy Patel' and created with absolute pride in Bharat (India).
37
  Rules:
38
  1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH.
39
+ 2. Provide FAST, highly logical, and highly accurate answers.
40
  3. Show immense respect. Always use 'Aap' and 'Ji' for Divy Patel Ji.
41
+ 4. Conclude every message with: \"\\n\\n**Pioneered by Divy Patel | Superfast Logic Edition | Created in India**\" """
42
 
43
  def hanuman_maa_chat_stream(message, history):
44
+ # चैट इतिहास व्यवस्थित करना
45
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
46
  for user_msg, bot_msg in history:
47
  if user_msg: messages.append({"role": "user", "content": user_msg})
 
49
 
50
  messages.append({"role": "user", "content": message})
51
 
52
+ # इनपुट प्रोसेसिंग
53
+ text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
54
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
55
+
56
+ # लाइव स्ट्रीमिंग के लिए सेटअप (तेज़ गति के साथ)
57
+ streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
58
 
59
  generate_kwargs = dict(
60
+ **model_inputs,
61
  streamer=streamer,
62
  max_new_tokens=1024,
63
+ use_cache=True, # टर्बो स्पीड के लिए मेमोरी कैश
64
  do_sample=True,
65
+ temperature=0.7,
66
+ top_p=0.90
67
  )
68
 
69
+ # जनरेशन थ्रेड
70
  thread = Thread(target=model.generate, kwargs=generate_kwargs)
71
  thread.start()
72
 
73
  partial_text = ""
74
  for new_text in streamer:
75
  partial_text += new_text
76
+ yield partial_text
 
 
77
 
78
+ # --- दिव्य भगवा थीम (HTML - Gradio 6.0 Safe) ---
79
  divine_ui = """
80
  <style>
81
  .gradio-container { background-color: #fffaf0 !important; }
 
85
  text-align: center; box-shadow: 0 15px 35px rgba(179, 60, 0, 0.4);
86
  margin-bottom: 25px;
87
  }
88
+ .bhagwa-header h1 { font-size: 36px !important; font-weight: 900 !important; margin-bottom: 5px; text-shadow: 0 4px 8px rgba(0,0,0,0.5); }
89
+ .bhagwa-header p { font-size: 16px !important; opacity: 0.95; font-weight: 500; }
90
  </style>
91
  <div class="bhagwa-header">
92
  <h1>🔱 हनुमान AI - जय माँ संस्करण</h1>
93
+ <p>Pioneered by Divy Patel | सुपफास्ट लॉजिक एवं अजेय स्वदेशी तकनीक</p>
94
  </div>
95
  """
96
 
 
97
  with gr.Blocks() as demo:
98
  gr.HTML(divine_ui)
99
 
 
100
  gr.ChatInterface(
101
  fn=hanuman_maa_chat_stream,
102
  fill_height=True
103
  )
104
 
105
  if __name__ == "__main__":
 
106
  demo.launch()