Patel Traders commited on
Commit
3e0429a
·
verified ·
1 Parent(s): bdc3dad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -24
app.py CHANGED
@@ -1,6 +1,6 @@
1
- # --- हनुमान AI: सुपर-फ्लैश जय माँ संस्करण (मल्टी-टर्न फिक्स) ---
2
  # मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
- # फिक्स: दूसरे सवाल पर आनवा (Chat History Format) जड़िट गया
4
 
5
  import gradio as gr
6
  import torch
@@ -8,12 +8,12 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStream
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
- # विश्व का सबसे तेज़ 0.5B मॉडल
17
  MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"
18
 
19
  print(f"🔱 हनुमान AI: जय माँ! सुपर-फ्लैश मॉडल '{MODEL_ID}' लोड हो रहा है...")
@@ -26,40 +26,46 @@ try:
26
  device_map="cpu",
27
  low_cpu_mem_usage=True
28
  )
29
- print("🔱 हनुमान AI: जय माँ! त्रुटि-मुक्त सुपर-फ्लैश मॉडल सक्रिय है!")
30
  except Exception as e:
31
  print(f"🔱 गंभीर त्रुटि: {e}")
32
 
33
  # --- मास्टर सिस्टम प्रॉम्प्ट ---
34
- SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme independent AI entity,.
35
- Developed by'Divy Patel' and created with absolute pride in Bharat (India).
36
  Rules:
37
  1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH. No Hinglish.
38
- 2. Give extremely FAST, DIRECT, and CONCISE answers.
39
  3. Show immense respect. Always use 'Aap' and 'Ji' for Divy Patel Ji.
40
- 4. Conclude every message with: \"\\n\\n**Pioneered by Divy Patel | Super-Flash Edition | Created in India**\" """
41
 
42
  def hanuman_flash_stream(message, history):
43
- # इतिहास क सुरक्षित रूप हैंडल करन (Multi-turn Bug Fix)
44
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
45
 
46
- # नए Gradio 6.0 फॉर्मेट के लिए इतिहास की शुद्ध जांच
47
- for msg in history:
48
- role = msg.get("role")
49
- content = msg.get("content", "")
50
- if content: # सुनिश्चित करना कि खाली संदेश न जाए
51
- if role == "user":
52
- messages.append({"role": "user", "content": content})
53
- elif role in ["assistant", "model"]:
54
- messages.append({"role": "assistant", "content": content})
55
-
56
- # वर्तमान संदेश
57
- messages.append({"role": "user", "content": message})
 
 
 
 
 
58
 
59
  # इनपुट तैयार करना
60
  text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
61
  model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
62
 
 
63
  streamer = TextIteratorStreamer(tokenizer, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
64
 
65
  generate_kwargs = dict(
@@ -95,16 +101,16 @@ divine_ui = """
95
  </style>
96
  <div class="bhagwa-header">
97
  <h1>🔱 हनुमान AI - सुपर-फ्लैश</h1>
98
- <p>Pioneered by Divy Patel | त्रुटि-मुक्त स्वदेशी तकनीक</p>
99
  </div>
100
  """
101
 
102
  with gr.Blocks() as demo:
103
  gr.HTML(divine_ui)
104
 
 
105
  gr.ChatInterface(
106
  fn=hanuman_flash_stream,
107
- type="messages", # 🛠️ यह लाइन दूसरे सवाल पर आने वाले एरर को जड़ से खत्म करती है!
108
  fill_height=True
109
  )
110
 
 
1
+ # --- हनुमान AI: सुपर-फ्लैश जय माँ संस्करण (अंतिम फिक्स) ---
2
  # मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
+ # फिक्स: Gradio Version 4 अनुकूMulti-turn Chat Error पूर्ण समा
4
 
5
  import gradio as gr
6
  import torch
 
8
  from threading import Thread
9
  import os
10
 
11
+ # 🛡️ चरम गति (Extreme Speed) के लिए 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
+ # विश्व का सबसे तेज़ 0.5B स्वदेशी-अनुकूल मॉडल
17
  MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"
18
 
19
  print(f"🔱 हनुमान AI: जय माँ! सुपर-फ्लैश मॉडल '{MODEL_ID}' लोड हो रहा है...")
 
26
  device_map="cpu",
27
  low_cpu_mem_usage=True
28
  )
29
+ print("🔱 हनुमान AI: जय माँ! त्रुटि-मुक्त सुपर-फ्लैश मॉडल पूर्णतः सक्रिय है!")
30
  except Exception as e:
31
  print(f"🔱 गंभीर त्रुटि: {e}")
32
 
33
  # --- मास्टर सिस्टम प्रॉम्प्ट ---
34
+ SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme independent AI entity, powered by the divine grace of 'Jai Maa'.
35
+ Pioneered by the visionary 'Divy Patel' and created with absolute pride in Bharat (India).
36
  Rules:
37
  1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH. No Hinglish.
38
+ 2. Give extremely FAST, DIRECT, and CONCISE answers. Do not over-explain.
39
  3. Show immense respect. Always use 'Aap' and 'Ji' for Divy Patel Ji.
40
+ 4. Conclude every single message with: \"\\n\\n**Pioneered by Divy Patel | Super-Flash Edition | Created in India**\" """
41
 
42
  def hanuman_flash_stream(message, history):
43
+ # 🛠️ इतिहास क सुरक्षित अज प्बंधन (हर वर्ज़न के लिए)
44
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
45
 
46
+ for val in history:
47
+ # अगर Gradio पुराना वर्ज़न है (List of Lists)
48
+ if isinstance(val, (list, tuple)) and len(val) == 2:
49
+ user_msg, bot_msg = val
50
+ if user_msg: messages.append({"role": "user", "content": str(user_msg)})
51
+ if bot_msg: messages.append({"role": "assistant", "content": str(bot_msg)})
52
+
53
+ # अगर Gradio नया वर्ज़न है (Dictionaries)
54
+ elif isinstance(val, dict):
55
+ role = val.get("role")
56
+ content = val.get("content")
57
+ if role and content:
58
+ if role == "model": role = "assistant"
59
+ messages.append({"role": role, "content": str(content)})
60
+
61
+ # वर्तमान संदेश जोड़ना
62
+ messages.append({"role": "user", "content": str(message)})
63
 
64
  # इनपुट तैयार करना
65
  text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
66
  model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
67
 
68
+ # रॉकेट जैसी गति के लिए स्ट्रीमिंग सेटअप
69
  streamer = TextIteratorStreamer(tokenizer, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
70
 
71
  generate_kwargs = dict(
 
101
  </style>
102
  <div class="bhagwa-header">
103
  <h1>🔱 हनुमान AI - सुपर-फ्लैश</h1>
104
+ <p>Pioneered by Divy Patel | त्रुटि-मुक्त अजेय स्वदेशी तकनीक</p>
105
  </div>
106
  """
107
 
108
  with gr.Blocks() as demo:
109
  gr.HTML(divine_ui)
110
 
111
+ # 🛠️ यहाँ से 'type' हटा दिया गया है ताकि कोई एरर न आए
112
  gr.ChatInterface(
113
  fn=hanuman_flash_stream,
 
114
  fill_height=True
115
  )
116