Patel Traders commited on
Commit
5c6f62c
·
verified ·
1 Parent(s): abace8e

Update app.py

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