Patel Traders commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,63 @@
|
|
| 1 |
# --- 🔱 हनुमान AI: अजेय एवं आत्मनिर्भर थिंकिंग पोर्टल (Qwen 2.5) ---
|
| 2 |
# मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
|
| 3 |
-
#
|
| 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 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 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
|
| 45 |
except:
|
| 46 |
-
pass
|
| 47 |
|
| 48 |
current_content.append({"type": "text", "text": user_message})
|
| 49 |
-
|
| 50 |
|
| 51 |
# ३. मॉडल के लिए इनपुट तैयार करना
|
| 52 |
-
text = processor.apply_chat_template(
|
| 53 |
-
image_inputs, video_inputs = process_vision_info(
|
| 54 |
|
| 55 |
inputs = processor(
|
| 56 |
text=[text],
|
|
@@ -60,7 +67,7 @@ def hanuman_logic(user_message, history, file_upload):
|
|
| 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,29 +76,30 @@ def hanuman_logic(user_message, history, file_upload):
|
|
| 69 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 70 |
)[0]
|
| 71 |
|
| 72 |
-
# ५.
|
| 73 |
history.append([user_message, response])
|
| 74 |
return "", history, None
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
-
|
| 78 |
-
history.append([user_message,
|
| 79 |
return "", history, None
|
| 80 |
|
| 81 |
-
# --- दिव्य पोर्टल इंटरफ़ेस ---
|
| 82 |
-
#
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
-
gr.HTML("<div style='text-align: center; color: #ff5500;'><h1>🔱 हनुमान AI -
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
with gr.Column(scale=4):
|
| 88 |
-
#
|
| 89 |
chatbot = gr.Chatbot(height=550)
|
| 90 |
with gr.Row():
|
| 91 |
msg_input = gr.Textbox(placeholder="अपना प्रश्न यहाँ लिखें दिव्य जी...", scale=7)
|
| 92 |
-
file_input = gr.File(label="फोटो/फाइल
|
| 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],
|
|
@@ -99,5 +107,4 @@ with gr.Blocks() as demo:
|
|
| 99 |
)
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
-
|
| 103 |
-
demo.launch()
|
|
|
|
| 1 |
# --- 🔱 हनुमान AI: अजेय एवं आत्मनिर्भर थिंकिंग पोर्टल (Qwen 2.5) ---
|
| 2 |
# मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
|
| 3 |
+
# तकनीकी समाधान: Mismatched Sizes Fix + Gradio Version Compatibility
|
| 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 |
+
# 🚀 उन्नत Qwen 2.5 मॉडल (3B Instruct)
|
| 12 |
model_id = "Qwen/Qwen2.5-VL-3B-Instruct"
|
| 13 |
|
| 14 |
+
print("🔱 हनुमान AI जागृत हो रहा है... सभी बाधाओं का नाश करते हुए।")
|
| 15 |
|
| 16 |
+
# 🛠️ अजेय लोडिंग: ignore_mismatched_sizes=True एरर को जड़ से खत्म करेगा
|
| 17 |
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 18 |
model_id,
|
| 19 |
torch_dtype=torch.float32,
|
| 20 |
device_map="cpu",
|
| 21 |
+
low_cpu_mem_usage=True,
|
| 22 |
+
ignore_mismatched_sizes=True,
|
| 23 |
+
trust_remote_code=True
|
| 24 |
)
|
| 25 |
+
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 26 |
|
| 27 |
def hanuman_logic(user_message, history, file_upload):
|
| 28 |
try:
|
| 29 |
+
# १. Gradio की साधारण लिस्ट हिस्ट्री को Qwen के डिक्शनरी फॉर्मेट में बदलना
|
| 30 |
+
messages = []
|
| 31 |
+
if history:
|
| 32 |
+
for past_user, past_bot in history:
|
| 33 |
+
if past_user:
|
| 34 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": str(past_user)}]})
|
| 35 |
+
if past_bot:
|
| 36 |
+
messages.append({"role": "assistant", "content": [{"type": "text", "text": str(past_bot)}]})
|
| 37 |
|
| 38 |
+
# २. वर्तमान संदेश और फाइल (चित्र/दस्तावेज) का विश्लेषण
|
| 39 |
current_content = []
|
| 40 |
|
| 41 |
if file_upload is not None:
|
| 42 |
file_ext = file_upload.name.split('.')[-1].lower()
|
| 43 |
+
# चित्र पहचान (Vision)
|
| 44 |
if file_ext in ['png', 'jpg', 'jpeg', 'webp']:
|
| 45 |
current_content.append({"type": "image", "image": file_upload.name})
|
| 46 |
+
# फाइल पढ़ना (Document Reading)
|
| 47 |
else:
|
| 48 |
try:
|
| 49 |
with open(file_upload.name, 'r', encoding='utf-8') as f:
|
| 50 |
file_data = f.read()
|
| 51 |
+
user_message = f"Document Context:\n{file_data}\n\nQuestion: {user_message}"
|
| 52 |
except:
|
| 53 |
+
pass
|
| 54 |
|
| 55 |
current_content.append({"type": "text", "text": user_message})
|
| 56 |
+
messages.append({"role": "user", "content": current_content})
|
| 57 |
|
| 58 |
# ३. मॉडल के लिए इनपुट तैयार करना
|
| 59 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 60 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
| 61 |
|
| 62 |
inputs = processor(
|
| 63 |
text=[text],
|
|
|
|
| 67 |
return_tensors="pt",
|
| 68 |
).to("cpu")
|
| 69 |
|
| 70 |
+
# ४. उत्तर का निर्माण (8192 टोकन क्षमता क��� साथ)
|
| 71 |
generated_ids = model.generate(**inputs, max_new_tokens=1024)
|
| 72 |
generated_ids_trimmed = [
|
| 73 |
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
|
|
|
| 76 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 77 |
)[0]
|
| 78 |
|
| 79 |
+
# ५. पुराने Gradio फॉर्मेट (List of Lists) में हिस्ट्री वापस करना
|
| 80 |
history.append([user_message, response])
|
| 81 |
return "", history, None
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
+
error_msg = f"क्षमा करें दिव्य जी, एक तकनीकी बाधा आई है: {str(e)}"
|
| 85 |
+
history.append([user_message, error_msg])
|
| 86 |
return "", history, None
|
| 87 |
|
| 88 |
+
# --- दिव्य पोर्टल इंटरफ़ेस (Gradio) ---
|
| 89 |
+
# 🛡️ 'theme' को यहाँ से हटा दिया गया है ताकि कोई Warning न आए
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
+
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>")
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
with gr.Column(scale=4):
|
| 95 |
+
# 🛡️ पुराने Gradio के लिए सरल चैटबॉट (बिना 'type' के)
|
| 96 |
chatbot = gr.Chatbot(height=550)
|
| 97 |
with gr.Row():
|
| 98 |
msg_input = gr.Textbox(placeholder="अपना प्रश्न यहाँ लिखें दिव्य जी...", scale=7)
|
| 99 |
+
file_input = gr.File(label="फोटो/फाइल", scale=2)
|
| 100 |
submit_btn = gr.Button("पूछें", variant="primary", scale=1)
|
| 101 |
|
| 102 |
+
# इवेंट हैंडलिंग
|
| 103 |
submit_btn.click(
|
| 104 |
fn=hanuman_logic,
|
| 105 |
inputs=[msg_input, chatbot, file_input],
|
|
|
|
| 107 |
)
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
+
demo.launch()
|
|
|