Patel Traders commited on
Commit
162a09c
·
verified ·
1 Parent(s): 0de9a27

Update app.py

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