Patel Traders commited on
Commit
bbe3ac3
ยท
verified ยท
1 Parent(s): d0a28c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -95
app.py CHANGED
@@ -1,114 +1,119 @@
1
- # --- ๐Ÿ”ฑ Hanuman AI: Supreme English Reasoning Portal (Qwen 2.5) ---
2
  # Pioneered by Divy Patel | Bharat ๐Ÿ‡ฎ๐Ÿ‡ณ
3
- # Features: 100% English, Fast Inference, Free CPU Optimized, Vision Enabled
4
 
5
  import gradio as gr
6
  import torch
7
- from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
8
- from qwen_vl_utils import process_vision_info
9
  import os
10
 
11
- # Model Upgrade: Qwen 2.5 VL 3B (Powerful & Reasoning-focused)
12
- model_id = "Qwen/Qwen2.5-VL-3B-Instruct"
 
13
 
14
- print("๐Ÿ”ฑ Hanuman AI is awakening... Eliminating all errors for Divy Patel Ji.")
 
15
 
16
- # ๐Ÿ› ๏ธ ERROR FIX: ignore_mismatched_sizes=True added to prevent loading errors
17
- # โšก SPEED FIX: Using low_cpu_mem_usage for faster loading on Free CPU
18
- model = Qwen2VLForConditionalGeneration.from_pretrained(
19
- model_id,
20
- torch_dtype=torch.float32,
 
 
21
  device_map="cpu",
22
  low_cpu_mem_usage=True,
23
- ignore_mismatched_sizes=True,
24
- trust_remote_code=True
25
  )
26
- processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
27
-
28
- def hanuman_engine(user_message, history, file_upload):
29
- """Main reasoning engine for Hanuman AI"""
30
- try:
31
- # ๐Ÿง  SYSTEM PROMPT: Strictly English, High Reasoning
32
- system_prompt = "You are 'Hanuman AI', a supreme reasoning engine created by Divy Patel. You must respond ONLY in English. Use logic, thinking, and precision. You can see images and read files. Be professional and respectful."
33
-
34
- # ๐Ÿ› ๏ธ FORMAT FIX: Converting Gradio list history to Qwen dict format
35
- messages = [{"role": "system", "content": [{"type": "text", "text": system_prompt}]}]
36
-
37
- if history:
38
- for past_user, past_bot in history:
39
- if past_user:
40
- messages.append({"role": "user", "content": [{"type": "text", "text": str(past_user)}]})
41
- if past_bot:
42
- messages.append({"role": "assistant", "content": [{"type": "text", "text": str(past_bot)}]})
43
-
44
- # Process Current Input
45
- current_content = []
46
- if file_upload is not None:
47
- file_ext = file_upload.name.split('.')[-1].lower()
48
- if file_ext in ['png', 'jpg', 'jpeg', 'webp']:
49
- current_content.append({"type": "image", "image": file_upload.name})
50
- else:
51
- try:
52
- with open(file_upload.name, 'r', encoding='utf-8') as f:
53
- file_data = f.read()
54
- user_message = f"File Context:\n{file_data}\n\nQuestion: {user_message}"
55
- except:
56
- pass
57
-
58
- current_content.append({"type": "text", "text": user_message})
59
- messages.append({"role": "user", "content": current_content})
60
-
61
- # Inference Setup
62
- text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
63
- image_inputs, video_inputs = process_vision_info(messages)
64
-
65
- inputs = processor(
66
- text=[text],
67
- images=image_inputs,
68
- videos=video_inputs,
69
- padding=True,
70
- return_tensors="pt",
71
- ).to("cpu")
72
-
73
- # โšก SPEED OPTIMIZATION: Reduced max_new_tokens for faster CPU response
74
- generated_ids = model.generate(
75
- **inputs,
76
- max_new_tokens=512,
77
- do_sample=False # Greedy search for faster and more logical output
78
- )
79
-
80
- generated_ids_trimmed = [
81
- out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
82
- ]
83
- response = processor.batch_decode(
84
- generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
85
- )[0]
86
-
87
- history.append([user_message, response])
88
- return "", history, None
89
-
90
- except Exception as e:
91
- error_msg = f"System error: {str(e)}"
92
- history.append([user_message, error_msg])
93
- return "", history, None
94
-
95
- # --- UI PORTAL ---
96
  with gr.Blocks() as demo:
97
- gr.HTML("<div style='text-align: center;'><h1>๐Ÿ”ฑ Hanuman AI - English Reasoning Portal</h1><p>Pioneered by Divy Patel | 100% Free CPU | Bharat ๐Ÿ‡ฎ๐Ÿ‡ณ</p></div>")
 
 
98
 
99
  with gr.Row():
100
- with gr.Column(scale=4):
101
- chatbot = gr.Chatbot(height=550)
102
- with gr.Row():
103
- msg_input = gr.Textbox(placeholder="Ask your complex question here...", scale=7)
104
- file_input = gr.File(label="Upload Image/File", scale=2)
105
- submit_btn = gr.Button("ASK", variant="primary", scale=1)
106
 
 
107
  submit_btn.click(
108
- fn=hanuman_engine,
109
- inputs=[msg_input, chatbot, file_input],
110
- outputs=[msg_input, chatbot, file_input]
111
  )
 
 
 
 
 
 
 
 
 
 
112
 
113
  if __name__ == "__main__":
114
- demo.launch()
 
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
+
69
+ partial_text = ""
70
+ for new_text in streamer:
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()