Patel Traders commited on
Commit
9e8a902
·
verified ·
1 Parent(s): 88c3492

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -49
app.py CHANGED
@@ -1,56 +1,34 @@
1
  # --- 🔱 हनुमान AI: अजेय एवं आत्मनिर्भर थिंकिंग पोर्टल (Qwen 2.5) ---
2
  # मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
- # तकनीक: Local CPU Execution (Zero API Charges)
4
- # मॉडल: Qwen2.5-VL-3B-Instruct (Advanced Reasoning + Multimodal)
5
 
6
  import gradio as gr
7
  import torch
8
- from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
9
  from qwen_vl_utils import process_vision_info
10
- import os
11
 
12
- # 🚀 मॉडल को अपगरेड कर दिया गया है (अधिक बुद्धिमान और शक्तिशाली)
13
  model_id = "Qwen/Qwen2.5-VL-3B-Instruct"
14
 
15
- print("🔱 हनुमान AI जागृत हो रहा है... नए ्नत मॉडल के साथ।")
16
 
17
- # 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
  )
24
- processor = AutoProcessor.from_pretrained(model_id)
25
 
26
- def hanuman_logic(message, history, file_upload):
 
27
  try:
28
- # हिस्ट्री को Qwen के फॉर्मेट में सुरक्षित रूप से बदलना
29
- messages = []
30
- for msg in history:
31
- messages.append({"role": msg["role"], "content": [{"type": "text", "text": msg["content"]}]})
32
-
33
- content = []
34
-
35
- # मल्टीमीडिया (चित्र और फाइल) प्रोसेसिंग
36
- if file_upload is not None:
37
- file_ext = file_upload.name.split('.')[-1].lower()
38
- if file_ext in ['png', 'jpg', 'jpeg', 'webp']:
39
- content.append({"type": "image", "image": file_upload.name})
40
- else:
41
- try:
42
- with open(file_upload.name, 'r', encoding='utf-8') as f:
43
- file_data = f.read()
44
- message = f"Document Data:\n{file_data}\n\nUser Question: {message}"
45
- except:
46
- return "क्षमा करें दिव्य जी, यह फाइल पढ़ी नहीं जा सकी।"
47
-
48
- content.append({"type": "text", "text": message})
49
- messages.append({"role": "user", "content": content})
50
-
51
  # इनपुट टोकनाइज़ेशन
52
- text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
53
- image_inputs, video_inputs = process_vision_info(messages)
54
 
55
  inputs = processor(
56
  text=[text],
@@ -70,9 +48,46 @@ def hanuman_logic(message, history, file_upload):
70
  )[0]
71
 
72
  return response
73
-
74
  except Exception as e:
75
- return f"त्रुटि: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  # --- दिव्य पोर्टल इंटरफ़ेस ---
78
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="amber")) as demo:
@@ -80,26 +95,21 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="amber")
80
 
81
  with gr.Row():
82
  with gr.Column(scale=4):
83
- # 🛡️ एरर फिक्स: यहाँ 'type="messages"' लगाया गया है
84
  chatbot = gr.Chatbot(height=550, type="messages")
85
  with gr.Row():
86
  msg_input = gr.Textbox(placeholder="अपना प्रश्न यहाँ लिखें दिव्य जी...", scale=7)
87
  file_input = gr.File(label="फोटो/फाइल अपलोड", scale=2)
88
  submit_btn = gr.Button("पूछें", variant="primary", scale=1)
89
 
90
- def chat_engine(message, history, file):
91
- bot_res = hanuman_logic(message, history, file)
92
-
93
- # 🛡️ एरर फिक्स: डिक्शनरी (Dictionary) फॉर्मेट में मैसेज जोड़ना
94
- history.append({"role": "user", "content": message})
95
- history.append({"role": "assistant", "content": bot_res})
96
-
97
- return "", history, None
98
-
99
- submit_btn.click(chat_engine, [msg_input, chatbot, file_input], [msg_input, chatbot, file_input])
100
 
101
  gr.Markdown("---")
102
- gr.Markdown("### दिव्य पटेल जी, यह उन्नमॉडल (Qwen 2.5) आप CPU िकिसी शुल्क के चल रहा है।")
103
 
104
  if __name__ == "__main__":
105
  demo.launch()
 
1
  # --- 🔱 हनुमान AI: अजेय एवं आत्मनिर्भर थिंकिंग पोर्टल (Qwen 2.5) ---
2
  # मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
+ # विशेष: 100% Free CPU, Mismatched Size Error Fixed, Gradio Dict Error Fixed
 
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
 
10
+ # उन्न और शक्तिशाली मॉडल
11
  model_id = "Qwen/Qwen2.5-VL-3B-Instruct"
12
 
13
+ print("🔱 हनुमान AI जागृत हो रहा है... सभी त्ुटियों का ाशरतहुए।")
14
 
15
+ # 🛠️ ERRROR FIX: ignore_mismatched_sizes=True और trust_remote_code=True जोड़ा या
16
  model = Qwen2VLForConditionalGeneration.from_pretrained(
17
  model_id,
18
  torch_dtype=torch.float32,
19
  device_map="cpu",
20
+ low_cpu_mem_usage=True,
21
+ ignore_mismatched_sizes=True,
22
+ trust_remote_code=True
23
  )
24
+ processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
25
 
26
+ def generate_hanuman_response(qwen_messages):
27
+ """मॉडल से उत्तर प्राप्त करने का मुख्य फंक्शन"""
28
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # इनपुट टोकनाइज़ेशन
30
+ text = processor.apply_chat_template(qwen_messages, tokenize=False, add_generation_prompt=True)
31
+ image_inputs, video_inputs = process_vision_info(qwen_messages)
32
 
33
  inputs = processor(
34
  text=[text],
 
48
  )[0]
49
 
50
  return response
 
51
  except Exception as e:
52
+ return f"तकनीकी त्रुटि: {str(e)}"
53
+
54
+ def process_chat(message, history, file_upload):
55
+ """Gradio UI और Qwen मॉडल के बीच का अजेय पुल"""
56
+
57
+ # १. मॉडल के लिए मल्टीमीडिया संदेश (Content) तैयार करना
58
+ content = []
59
+
60
+ if file_upload is not None:
61
+ file_ext = file_upload.name.split('.')[-1].lower()
62
+ if file_ext in ['png', 'jpg', 'jpeg', 'webp']:
63
+ content.append({"type": "image", "image": file_upload.name})
64
+ else:
65
+ try:
66
+ with open(file_upload.name, 'r', encoding='utf-8') as f:
67
+ file_data = f.read()
68
+ message = f"Document Data:\n{file_data}\n\nUser Question: {message}"
69
+ except:
70
+ pass # अगर फाइल न पढ़ी जाए तो उसे छोड़ दें
71
+
72
+ content.append({"type": "text", "text": message})
73
+
74
+ # २. मॉडल के लिए ह���स्ट्री (History) तैयार करना
75
+ qwen_messages = []
76
+ for msg in history:
77
+ # पुरानी हिस्ट्री को साधारण टेक्स्ट के रूप में मॉडल को देना
78
+ qwen_messages.append({"role": msg["role"], "content": msg["content"]})
79
+
80
+ # वर्तमान संदेश जोड़ना
81
+ qwen_messages.append({"role": "user", "content": content})
82
+
83
+ # ३. मॉडल से जवाब मांगना
84
+ bot_response = generate_hanuman_response(qwen_messages)
85
+
86
+ # 🛠️ ERROR FIX: Gradio के लिए शुद्ध डिक्शनरी (Dictionary) फॉर्मेट में हिस्ट्री अपडेट करना
87
+ history.append({"role": "user", "content": message})
88
+ history.append({"role": "assistant", "content": bot_response})
89
+
90
+ return "", history, None
91
 
92
  # --- दिव्य पोर्टल इंटरफ़ेस ---
93
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="amber")) as demo:
 
95
 
96
  with gr.Row():
97
  with gr.Column(scale=4):
98
+ # type="messages" सुनिश्चित करता है कि UI डिक्शनरी फॉर्मेट को समझे
99
  chatbot = gr.Chatbot(height=550, type="messages")
100
  with gr.Row():
101
  msg_input = gr.Textbox(placeholder="अपना प्रश्न यहाँ लिखें दिव्य जी...", scale=7)
102
  file_input = gr.File(label="फोटो/फाइल अपलोड", scale=2)
103
  submit_btn = gr.Button("पूछें", variant="primary", scale=1)
104
 
105
+ submit_btn.click(
106
+ fn=process_chat,
107
+ inputs=[msg_input, chatbot, file_input],
108
+ outputs=[msg_input, chatbot, file_input]
109
+ )
 
 
 
 
 
110
 
111
  gr.Markdown("---")
112
+ gr.Markdown("### दिव्य पटेल जी, सभी्रुटियों (Errors) क समाधान िगया है।")
113
 
114
  if __name__ == "__main__":
115
  demo.launch()