Patel Traders commited on
Commit
6157b4e
·
verified ·
1 Parent(s): 9e8a902

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -61
app.py CHANGED
@@ -1,32 +1,54 @@
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
 
@@ -38,7 +60,7 @@ def generate_hanuman_response(qwen_messages):
38
  return_tensors="pt",
39
  ).to("cpu")
40
 
41
- # उत्तर का निर्माण
42
  generated_ids = model.generate(**inputs, max_new_tokens=1024)
43
  generated_ids_trimmed = [
44
  out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
@@ -47,69 +69,35 @@ def generate_hanuman_response(qwen_messages):
47
  generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
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:
94
- gr.HTML("<div style='text-align: center;'><h1>🔱 हनुमान AI - उन्नत थिंकिंग पोर्टल (v2.5) </h1><p>Pioneered by Divy Patel | 100% Free | भारत 🇮🇳</p></div>")
 
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()
 
 
1
  # --- 🔱 हनुमान AI: अजेय एवं आत्मनिर्भर थिंकिंग पोर्टल (Qwen 2.5) ---
2
  # मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
+ # विशेषता: 100% Free CPU, Gradio Type Error Fixed, Theme Warning 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
  model_id = "Qwen/Qwen2.5-VL-3B-Instruct"
11
 
12
+ print("🔱 हनुमान AI जागृत हो रहा है... सभी त्रुटियों का पूर्ण नाश करते हुए।")
13
 
14
+ # 🛠️ मॉडल लोडिंग (बिना किसी विवादित फ्लैग के, शुद्ध लोडिं)
15
  model = Qwen2VLForConditionalGeneration.from_pretrained(
16
  model_id,
17
  torch_dtype=torch.float32,
18
  device_map="cpu",
19
+ low_cpu_mem_usage=True
 
 
20
  )
21
+ processor = AutoProcessor.from_pretrained(model_id)
22
 
23
+ def hanuman_logic(user_message, history, file_upload):
 
24
  try:
25
+ # १. Gradio की साधारण हिस््री क Qwen ी डिक्शरी भषा मं बदला (अजेय पुल)
26
+ qwen_messages = []
27
+ for past_user_msg, past_bot_msg in history:
28
+ if past_user_msg:
29
+ qwen_messages.append({"role": "user", "content": [{"type": "text", "text": past_user_msg}]})
30
+ if past_bot_msg:
31
+ qwen_messages.append({"role": "assistant", "content": [{"type": "text", "text": past_bot_msg}]})
32
+
33
+ # २. वर्तमान संदेश और फाइल का विश्लेषण
34
+ current_content = []
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
+ current_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
+ user_message = f"Document Data:\n{file_data}\n\nQuestion: {user_message}"
45
+ except:
46
+ pass # अगर फाइल न पढ़ी जाए तो एरर न दें, बस आगे बढ़ें
47
+
48
+ current_content.append({"type": "text", "text": user_message})
49
+ qwen_messages.append({"role": "user", "content": current_content})
50
+
51
+ # ३. मॉडल के लिए इनपुट तैयार करना
52
  text = processor.apply_chat_template(qwen_messages, tokenize=False, add_generation_prompt=True)
53
  image_inputs, video_inputs = process_vision_info(qwen_messages)
54
 
 
60
  return_tensors="pt",
61
  ).to("cpu")
62
 
63
+ # ४. उत्तर का निर्माण
64
  generated_ids = model.generate(**inputs, max_new_tokens=1024)
65
  generated_ids_trimmed = [
66
  out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
 
69
  generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
70
  )[0]
71
 
72
+ # ५. साधारण Gradio फॉर्मेट में वापस भेजना
73
+ history.append([user_message, response])
74
+ return "", history, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
+ except Exception as e:
77
+ # अगर कोई भी तकनीकी बाधा आती है, तो वह चैट में ही बता देगा, स्पेस क्रैश नहीं होगा
78
+ history.append([user_message, f"तकनीकी त्रुटि: {str(e)}"])
79
+ return "", history, None
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  # --- दिव्य पोर्टल इंटरफ़ेस ---
82
+ # 🛠️ FIX: 'theme' को यहाँ से हटा दिया गया है ताकि कोई Warning न आए
83
+ with gr.Blocks() as demo:
84
+ gr.HTML("<div style='text-align: center; color: #ff5500;'><h1>🔱 हनुमान AI - उन्नत थिंकिंग पोर्टल (v2.5)</h1><p><b>Pioneered by Divy Patel | 100% Free | भारत 🇮🇳</b></p></div>")
85
 
86
  with gr.Row():
87
  with gr.Column(scale=4):
88
+ # 🛠️ FIX: type="messages" हटा दिया गया है
89
+ chatbot = gr.Chatbot(height=550)
90
  with gr.Row():
91
  msg_input = gr.Textbox(placeholder="अपना प्रश्न यहाँ लिखें दिव्य जी...", scale=7)
92
  file_input = gr.File(label="फोटो/फाइल अपलोड", scale=2)
93
  submit_btn = gr.Button("पूछें", variant="primary", scale=1)
94
 
95
  submit_btn.click(
96
+ fn=hanuman_logic,
97
  inputs=[msg_input, chatbot, file_input],
98
  outputs=[msg_input, chatbot, file_input]
99
  )
100
 
 
 
 
101
  if __name__ == "__main__":
102
+ # स्पेस को सुरक्षित रूप से लॉन्च करना
103
+ demo.launch()