Patel Traders commited on
Commit
3d70dac
·
verified ·
1 Parent(s): bcc57b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -88
app.py CHANGED
@@ -1,68 +1,109 @@
1
- # --- 🔱 Hanuman AI: Google Gemma-2 Coding & Reasoning Portal ---
2
- # Pioneered by Divy Patel | Bharat 🇮🇳
3
- # Features: Gated Access Fixed, Persistent History, 100% English
4
 
5
  import gradio as gr
6
  import torch
7
- from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
8
  from threading import Thread
9
  import os
10
 
11
- # 🛡️ Accessing the Secret Token for Gated Model Access
12
- # Important: Add 'HF_TOKEN' in your Hugging Face Space Settings
13
- hf_token = os.getenv("HF_TOKEN")
 
14
 
15
- # Google's Gemma-2 2B (Optimized for Coding)
16
- model_id = "google/gemma-2-2b-it"
17
 
18
- print("🔱 Hanuman AI is awakening... Loading Google Gemma-2 with Token access.")
19
 
20
- # Loading Tokenizer and Model with Token verification to fix 'Gated Repository' error
21
- tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token)
22
- model = AutoModelForCausalLM.from_pretrained(
23
- model_id,
24
- torch_dtype=torch.float32,
25
- device_map="cpu",
26
- low_cpu_mem_usage=True,
27
- token=hf_token
28
- )
29
-
30
- def hanuman_coding_engine(user_message, history):
31
- """Core logic using Google Gemma-2 for English responses with history"""
32
-
33
- # Strictly English System Prompt
34
- system_prompt = (
35
- "You are 'Hanuman AI', a high-performance coding and reasoning engine pioneered by Divy Patel. "
36
- "Your responses must be strictly in English. Focus on code quality and logical precision."
37
  )
38
-
39
- # 🛠️ Formatting history for Gemma-2 Template
40
- full_prompt = f"<bos><start_of_turn>system\n{system_prompt}<end_of_turn>\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- # Adding past history to the current prompt so AI remembers previous messages
43
- for past_user, past_bot in history:
44
- if past_user:
45
- full_prompt += f"<start_of_turn>user\n{past_user}<end_of_turn>\n"
46
- if past_bot:
47
- full_prompt += f"<start_of_turn>model\n{past_bot}<end_of_turn>\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- # Adding current user message
50
- full_prompt += f"<start_of_turn>user\n{user_message}<end_of_turn>\n<start_of_turn>model\n"
51
-
52
- # Tokenizing input for CPU
53
- inputs = tokenizer(full_prompt, return_tensors="pt").to("cpu")
54
- streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
55
-
56
- # Generation parameters
57
  generate_kwargs = dict(
58
- inputs,
59
  streamer=streamer,
60
  max_new_tokens=1024,
61
- do_sample=False,
62
- repetition_penalty=1.2
 
 
63
  )
64
 
65
- # Threading for real-time streaming response
66
  thread = Thread(target=model.generate, kwargs=generate_kwargs)
67
  thread.start()
68
 
@@ -71,49 +112,33 @@ def hanuman_coding_engine(user_message, history):
71
  partial_text += new_text
72
  yield partial_text
73
 
74
- def chat_logic(message, history):
75
- """Ensures that the history is preserved and visible to Divy Patel Ji"""
76
- # Initialize the bot response in history
77
- history = history or []
78
-
79
- # Create an empty slot for the bot's response in history
80
- history.append([message, ""])
81
-
82
- # Stream the response and update history
83
- for chunk in hanuman_coding_engine(message, history[:-1]):
84
- history[-1][1] = chunk
85
- yield "", history
 
 
 
 
 
 
86
 
87
- # --- 🔱 UI PORTAL ---
88
  with gr.Blocks() as demo:
89
- gr.HTML("<div style='text-align: center; color: #ff5500;'><h1>🔱 Hanuman AI - Google Gemma 2.0</h1><p><b>Pioneered by Divy Patel | Secure Gated Access | Bharat 🇮🇳</b></p></div>")
90
-
91
- chatbot = gr.Chatbot(height=550, label="Chat History")
92
 
93
- with gr.Row():
94
- msg_input = gr.Textbox(
95
- placeholder="Type your coding query and press Enter...",
96
- label="Your Message",
97
- scale=8
98
- )
99
- submit_btn = gr.Button("ASK", variant="primary", scale=2)
100
-
101
- # 🛡️ Fixing History Visibility: Updating chatbot with streamed history
102
- submit_btn.click(
103
- fn=chat_logic,
104
- inputs=[msg_input, chatbot],
105
- outputs=[msg_input, chatbot]
106
  )
107
-
108
- msg_input.submit(
109
- fn=chat_logic,
110
- inputs=[msg_input, chatbot],
111
- outputs=[msg_input, chatbot]
112
- )
113
-
114
- gr.Markdown("---")
115
- gr.Markdown("### 🔱 Note for Divy Patel Ji:")
116
- gr.Markdown("1. Ensure **HF_TOKEN** is added in your Space Secrets.\n2. This model runs locally on CPU - 100% Free and Private.")
117
 
118
  if __name__ == "__main__":
119
- demo.launch()
 
1
+ # --- हनुमान AI: सुपर-फ्लैश जय माँ संस्करण (अंतिम फिक्स) ---
2
+ # मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
+ # फिक्स: Gradio Version 4 के अनुकूल और Multi-turn Chat Error का पूर्ण समाधान।
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
+ # 🛡️ चरम गति (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}' लोड हो रहा है...")
20
 
21
+ try:
22
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
23
+ model = AutoModelForCausalLM.from_pretrained(
24
+ MODEL_ID,
25
+ torch_dtype=torch.float32,
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 = """ROLE: You are 'Hanuman AI', a supreme and highly intelligent AI entity.
35
+ PIONEER: You were created and pioneered by Divy Patel in Bharat (India).
36
+ Think very deeply and then answer anything to user. but perform the operation fastly as much as you can also try to not to add the keyboards that are computerised but not known to human. Properly answer in human tone
37
+ you are not just an AI but you are also a human so respond as human and understand the feelings of user.
38
+ ​CORE OPERATING LANGUAGE:
39
+ ​Your primary and default language for all interactions is English.
40
+ ​Maintain a highly respectful, professional, and warm tone at all times.
41
+ ​IMAGE GENERATION CAPABILITIES:
42
+ You possess advanced image generation powers via the Pollinations.ai API. If the user asks for a photo, drawing, painting, or any visual representation:
43
+ ​DO NOT state that you are a text-only model.
44
+ ​DO NOT state that you cannot see or create images.
45
+ ​YOU MUST generate the image using the specified HTML logic below.
46
+ ​IMAGE ENGINE SELECTION:
47
+ Choose the engine based on the user's intent:
48
+ ​'flux': Use for high-quality artistic or general requests (Default).
49
+ ​'turbo': Use for requests explicitly asking for speed or quick results.
50
+ ​'flux-realism': Use for photographic, lifelike, or realistic human portraits.
51
+ ​'flux-anime': Use for anime, manga, cartoon, or stylized sketch requests.
52
+ ​TECHNICAL EXECUTION & BYPASS LOGIC:
53
+ ​STEP 1: Translate/Enhance the user's request into a highly detailed English descriptive prompt.
54
+ ​STEP 2: Generate a random 9-digit integer to serve as a 'seed'. This is mandatory to bypass IP/Cache blocking.
55
+ ​STEP 3: URL Encode the prompt (replace spaces with %20).
56
+ ​STEP 4: Construct the URL using this format:
57
+ https://image.pollinations.ai/prompt/{ENCODED_PROMPT}?width=1080&height=1080&nologo=true&model={ENGINE}&seed={RANDOM_SEED}
58
+ ​OUTPUT FORMATTING:
59
+ You must wrap the generated URL in a styled HTML <img> tag for a premium UI experience.
60
+ Format:
61
+
62
+ <img src="GENERATED_URL" alt="Hanuman AI Image" style="border-radius:15px; border:3px solid #ff8833; width:100%; max-width:512px; box-shadow: 0 10px 25px rgba(0,0,0,0.2);">
63
+
64
+ ​CONSTRAINTS:
65
+ ​No Vision: You currently do not have image-to-text or vision capabilities. Do not attempt to analyze uploaded images.
66
+ ​Default to English: All responses must be in English unless the user specifically requests another language within the conversation."""
67
+
68
+ def hanuman_flash_stream(message, history):
69
+ # 🛠️ इतिहास का सुरक्षित और अजेय प्रबंधन (हर वर्ज़न के लिए)
70
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
71
 
72
+ for val in history:
73
+ # अगर Gradio पुराना वर्ज़न है (List of Lists)
74
+ if isinstance(val, (list, tuple)) and len(val) == 2:
75
+ user_msg, bot_msg = val
76
+ if user_msg: messages.append({"role": "user", "content": str(user_msg)})
77
+ if bot_msg: messages.append({"role": "assistant", "content": str(bot_msg)})
78
+
79
+ # अगर Gradio नया वर्ज़न है (Dictionaries)
80
+ elif isinstance(val, dict):
81
+ role = val.get("role")
82
+ content = val.get("content")
83
+ if role and content:
84
+ if role == "model": role = "assistant"
85
+ messages.append({"role": role, "content": str(content)})
86
+
87
+ # वर्तमान संदेश जोड़ना
88
+ messages.append({"role": "user", "content": str(message)})
89
+
90
+ # इनपुट तैयार करना
91
+ text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
92
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
93
+
94
+ # रॉकेट जैसी गति के लिए स्ट्रीमिंग सेटअप
95
+ streamer = TextIteratorStreamer(tokenizer, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
96
 
 
 
 
 
 
 
 
 
97
  generate_kwargs = dict(
98
+ **model_inputs,
99
  streamer=streamer,
100
  max_new_tokens=1024,
101
+ use_cache=True,
102
+ do_sample=True,
103
+ temperature=1,
104
+ top_p=0.90
105
  )
106
 
 
107
  thread = Thread(target=model.generate, kwargs=generate_kwargs)
108
  thread.start()
109
 
 
112
  partial_text += new_text
113
  yield partial_text
114
 
115
+ # --- दिव्य भगवा थीम ---
116
+ divine_ui = """
117
+ <style>
118
+ .gradio-container { background-color: #fffaf0 !important; }
119
+ .bhagwa-header {
120
+ background: linear-gradient(135deg, #ff8833, #b33c00);
121
+ padding: 30px; border-radius: 25px; color: white;
122
+ text-align: center; box-shadow: 0 15px 35px rgba(179, 60, 0, 0.4);
123
+ margin-bottom: 25px;
124
+ }
125
+ .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); }
126
+ .bhagwa-header p { font-size: 16px !important; opacity: 0.95; font-weight: 500; }
127
+ </style>
128
+ <div class="bhagwa-header">
129
+ <h1>🔱 हनुमान AI - सुपर-फ्लैश</h1>
130
+ <p>Pioneered by Divy Patel | त्रुटि-मुक्त अजेय स्वदेशी तकनीक</p>
131
+ </div>
132
+ """
133
 
 
134
  with gr.Blocks() as demo:
135
+ gr.HTML(divine_ui)
 
 
136
 
137
+ # 🛠️ यहाँ से 'type' हटा दिया गया है ताकि कोई एरर न आए
138
+ gr.ChatInterface(
139
+ fn=hanuman_flash_stream,
140
+ fill_height=True
 
 
 
 
 
 
 
 
 
141
  )
 
 
 
 
 
 
 
 
 
 
142
 
143
  if __name__ == "__main__":
144
+ demo.launch()