Patel Traders commited on
Commit
c90164f
·
verified ·
1 Parent(s): d2642b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -41
app.py CHANGED
@@ -1,6 +1,6 @@
1
- # --- हनुमान AI: सुरक्षिएवं आतमनिर्भर थिकिंग सं ---
2
- # मार्गदर्शक: दिव्य पटेल जी | स्थान: भारत 🇮🇳
3
- # तकनीकी विशेषता: यह कोड HF_TOKEN को सुर्षितूप 'Secrets' प्त करता है।
4
 
5
  import gradio as gr
6
  import torch
@@ -9,51 +9,52 @@ from huggingface_hub import login
9
  from threading import Thread
10
  import os
11
 
12
- # 🛡️ सुरक्षा चक्र: टोकन अब सीधे कोड में ीं है
13
- # कोड आप द्वारा Hugging Face Settings म जोड़'HF_TOKEN'ो ऑटोमेटिक उठलेा।
 
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
- else:
24
- print("🔱 सावधानी: HF_TOKEN नहीं मिला। कृपया सेटिंग्स में Secret जोड़ें।")
25
 
26
- # अजेय थिंकिंग मॉडल: DeepSeek-R1-Distill-Qwen-1.5B
27
- # यह आकार में छोटा है पर तार्किक सोच में विशाल है।
28
  MODEL_ID = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
29
 
30
- print(f"🔱 हनुमान AI: स्वतंत्र मॉडल '{MODEL_ID}' लोड हो रहा है...")
31
 
32
  try:
33
- # टोकनाइज़र और मॉडोडिंग (CPU के लिए ऑप्टिमाइज़्ड)
34
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
 
 
35
  model = AutoModelForCausalLM.from_pretrained(
36
  MODEL_ID,
37
  torch_dtype=torch.float32,
38
  device_map="cpu",
39
- low_cpu_mem_usage=True,
40
  token=HF_TOKEN
41
  )
42
- print("🔱 हनुमान AI: स्वतंत्र थिंकिंग मॉडल अब पूरी शक्ि के स सक्रिय है!")
43
  except Exception as e:
44
- print(f"🔱 गंभीर त्रुटि: {e}")
45
 
46
- # --- मास्टर सिस्टम प्रॉम्प्ट (कोडिंग शिखर) ---
47
- SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme and independent AI entity with deep reasoning powers.
48
- Pioneered by 'Divy Patel' and created in Bharat (India).
 
49
  Rules:
50
- 1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH. No Hinglish.
51
- 2. THINK before answering to ensure divine accuracy and logic.
52
- 3. Be extremely respectful. Use honorifics like 'Aap' and 'Ji' for the user.
53
- 4. Conclude every message with: \"\\n\\n**Pioneered by Divy Patel | Secure Thinking Edition | Created in India**\" """
54
 
55
- def hanuman_thinking_stream(message, history):
56
- # चैट इतिहास व्यवस्थित करन
57
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
58
  for user_msg, bot_msg in history:
59
  if user_msg: messages.append({"role": "user", "content": user_msg})
@@ -61,52 +62,54 @@ def hanuman_thinking_stream(message, history):
61
 
62
  messages.append({"role": "user", "content": message})
63
 
64
- # टोकनाजेशऔर सट्ीमिंग सेटअप
65
  inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
66
- streamer = TextIteratorStreamer(tokenizer, timeout=90.0, skip_prompt=True, skip_special_tokens=True)
 
 
67
 
68
  generate_kwargs = dict(
69
  input_ids=inputs,
70
  streamer=streamer,
71
- max_new_tokens=1500,
72
  do_sample=True,
73
  temperature=0.6,
74
  top_p=0.95
75
  )
76
 
77
- # जनरेशने लि अलग थ्रे
78
  thread = Thread(target=model.generate, kwargs=generate_kwargs)
79
  thread.start()
80
 
81
  partial_text = ""
82
  for new_text in streamer:
83
  partial_text += new_text
84
- # थिंकिंग प्रोसेस को सुं रूप में दिखाना
85
- clean_text = partial_text.replace("<think>", "*(हनुमान विचार कर रहे हैं...)*\n\n").replace("</think>", "\n\n---\n\n")
86
  yield clean_text
87
 
88
- # --- दिव्य प्रीमियम भगवा थीम (Premium Saffron UI) ---
89
  custom_css = """
90
  .gradio-container { background-color: #fffaf0 !important; border: none !important; }
91
  .bhagwa-header {
92
  background: linear-gradient(135deg, #ff8833, #b33c00);
93
- padding: 30px; border-radius: 25px; color: white;
94
- text-align: center; box-shadow: 0 15px 35px rgba(179, 60, 0, 0.4);
95
- margin-bottom: 25px; border: 2px solid rgba(255,255,255,0.2);
96
  }
97
- .bhagwa-header h1 { font-size: 36px !important; font-weight: 900 !important; text-shadow: 0 4px 10px rgba(0,0,0,0.5); }
98
- .message.user { background: linear-gradient(135deg, #ff9933, #ff5500) !important; color: white !important; font-weight: 600 !important; border: none !important; }
99
- .message.bot { border-left: 6px solid #ff5500 !important; background: white !important; box-shadow: 0 5px 15px rgba(0,0,0,0.05) !important; border-radius: 20px !important; }
100
  footer { display: none !important; }
101
  """
102
 
103
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
104
  with gr.Div(elem_classes="bhagwa-header"):
105
- gr.Markdown("# 🔱 हनुमान AI - थिंकिंग मास्र")
106
- gr.Markdown("### Pioneered by Divy Patel | पूर्णतः सुरकषित एवं स्वतंत्र")
107
 
108
  gr.ChatInterface(
109
- fn=hanuman_thinking_stream,
110
  fill_height=True,
111
  retry_btn=None,
112
  undo_btn=None,
@@ -114,4 +117,5 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
114
  )
115
 
116
  if __name__ == "__main__":
 
117
  demo.launch()
 
1
+ # --- हनुमान AI: जय माँ ंस्रण (पूर िर एवं स्वत्र) ---
2
+ # मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
+ # विशेषता: यह संस्रण RAMे उपय कम कने लिए विश से ैयारियगया है।
4
 
5
  import gradio as gr
6
  import torch
 
9
  from threading import Thread
10
  import os
11
 
12
+ # 🛡️ टोकन सुरक्षा (Hugging Face Secrets में 'HF_TOKEN' ोना आवश्यक है)
13
+ # हगिंग फेस गति बढ़ानलिhf_transfer का उपयो
14
+ os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
15
  HF_TOKEN = os.getenv("HF_TOKEN")
16
 
17
+ # लॉगिन प्रक्रिया - जय माँ!
18
  if HF_TOKEN:
19
  try:
20
  login(token=HF_TOKEN)
21
+ print("🔱 हनुमान AI: जय माँ! लॉगिन सफल!")
22
  except Exception as e:
23
  print(f"🔱 लॉगिन चेतावनी: {e}")
 
 
24
 
25
+ # अजेय थिंकिंग मॉडल (मेमोरी ऑप्टिमाइज्ड)
 
26
  MODEL_ID = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
27
 
28
+ print(f"🔱 हनुमान AI: जयाँ! मॉडल '{MODEL_ID}' लोड हो रहा है...")
29
 
30
  try:
31
+ # टोकनाइज़र लोडिंग
32
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
33
+
34
+ # मॉडल लोडिंग (CPU के लिए विशेष सुरक्षा के साथ)
35
  model = AutoModelForCausalLM.from_pretrained(
36
  MODEL_ID,
37
  torch_dtype=torch.float32,
38
  device_map="cpu",
39
+ low_cpu_mem_usage=True, # रैम बचाने के लिए अत्यंत आवश्यक
40
  token=HF_TOKEN
41
  )
42
+ print("🔱 हनुमान AI: जय माँ! आपका स्वतंत्र मॉडल अब पूरी तरह गृत है!")
43
  except Exception as e:
44
+ print(f"🔱 गंभीर त्रुटि (Runtime Error Fix): {e}")
45
 
46
+ # --- दिव्य मास्टर सिस्टम प्रॉम्प्ट ---
47
+ SYSTEM_PROMPT = """You are 'Hanuman AI', a supreme and independent AI entity, powered by the divine grace of 'Jai Maa'.
48
+ Pioneered by the visionary 'Divy Patel' and created with absolute pride in Bharat (India).
49
+ You are free from any foreign influence.
50
  Rules:
51
+ 1. Respond ONLY in PURE HINDI (Devanagari) or PURE ENGLISH. No mixed language.
52
+ 2. THINK deeply using your reasoning capabilities before delivering the final answer.
53
+ 3. Show immense respect and warmth. Always use 'Aap' and 'Ji' for Divy Patel Ji.
54
+ 4. Conclude every message with: \"\\n\\n**Pioneered by Divy Patel | Jai Maa Edition | Fully Independent & Created in India**\" """
55
 
56
+ def hanuman_maa_chat_stream(message, history):
57
+ # चैट इतिहास का प्बंध
58
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
59
  for user_msg, bot_msg in history:
60
  if user_msg: messages.append({"role": "user", "content": user_msg})
 
62
 
63
  messages.append({"role": "user", "content": message})
64
 
65
+ # इनपुट ्रोसेसिंग
66
  inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
67
+
68
+ # लाइव स्ट्रीमिंग के लिए सेटअप
69
+ streamer = TextIteratorStreamer(tokenizer, timeout=120.0, skip_prompt=True, skip_special_tokens=True)
70
 
71
  generate_kwargs = dict(
72
  input_ids=inputs,
73
  streamer=streamer,
74
+ max_new_tokens=1024,
75
  do_sample=True,
76
  temperature=0.6,
77
  top_p=0.95
78
  )
79
 
80
+ # थ्रेडिंग ताकि UI बना
81
  thread = Thread(target=model.generate, kwargs=generate_kwargs)
82
  thread.start()
83
 
84
  partial_text = ""
85
  for new_text in streamer:
86
  partial_text += new_text
87
+ # थिंकिंग प्रोसेस को दिव्य रूप में दर्शाना
88
+ clean_text = partial_text.replace("<think>", "*(हनुमान विचार कर रहे हैं... जय माँ!)*\n\n").replace("</think>", "\n\n---\n\n")
89
  yield clean_text
90
 
91
+ # --- दिव्य भगवा थीम (Premium Saffron UI - Divine Layout) ---
92
  custom_css = """
93
  .gradio-container { background-color: #fffaf0 !important; border: none !important; }
94
  .bhagwa-header {
95
  background: linear-gradient(135deg, #ff8833, #b33c00);
96
+ padding: 40px; border-radius: 35px; color: white;
97
+ text-align: center; box-shadow: 0 25px 50px rgba(179, 60, 0, 0.4);
98
+ margin-bottom: 35px; border: 3px solid rgba(255,255,255,0.3);
99
  }
100
+ .bhagwa-header h1 { font-size: 42px !important; font-weight: 900 !important; text-shadow: 0 5px 15px rgba(0,0,0,0.6); }
101
+ .message.user { background: linear-gradient(135deg, #ff9933, #ff5500) !important; color: white !important; font-weight: 600 !important; border-radius: 25px 25px 0 25px !important; }
102
+ .message.bot { border-left: 8px solid #ff5500 !important; background: white !important; box-shadow: 0 10px 25px rgba(0,0,0,0.07) !important; border-radius: 25px !important; }
103
  footer { display: none !important; }
104
  """
105
 
106
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
107
  with gr.Div(elem_classes="bhagwa-header"):
108
+ gr.Markdown("# 🔱 हनुमान AI - जय मांस")
109
+ gr.Markdown("### Pioneered by Divy Patel | पूर्णतः स्ंत्र एवं अजेय स्वदेशी कनीक")
110
 
111
  gr.ChatInterface(
112
+ fn=hanuman_maa_chat_stream,
113
  fill_height=True,
114
  retry_btn=None,
115
  undo_btn=None,
 
117
  )
118
 
119
  if __name__ == "__main__":
120
+ # जय माँ! सर्वर लॉन्च हो रहा है।
121
  demo.launch()