Patel Traders commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# --- 🔱 Hanuman Vision: Phi-3.5 Supreme Engine ---
|
| 2 |
# मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
|
| 3 |
# मॉडल: microsoft/Phi-3.5-vision-instruct (4.2B Parameters - 10GB RAM Safe)
|
| 4 |
-
# फिक्स:
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import torch
|
|
@@ -11,62 +11,73 @@ import os
|
|
| 11 |
|
| 12 |
model_id = "microsoft/Phi-3.5-vision-instruct"
|
| 13 |
|
| 14 |
-
print("🔱 हनुमान विज़न Phi-3.5 जागृत हो रहा है... दिव्य जी के अजेय स
|
| 15 |
|
| 16 |
-
#
|
| 17 |
model = None
|
| 18 |
processor = None
|
| 19 |
|
| 20 |
try:
|
| 21 |
-
# 🛠️
|
| 22 |
-
# यह सर्वर को 'Flash Attention 2' का उपयोग करने से रोकेगा, जो क्रैश का मुख्य कारण था।
|
| 23 |
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
model_id,
|
| 25 |
device_map="cpu",
|
| 26 |
trust_remote_code=True,
|
| 27 |
torch_dtype=torch.bfloat16,
|
| 28 |
low_cpu_mem_usage=True,
|
| 29 |
-
attn_implementation="eager"
|
| 30 |
)
|
| 31 |
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 32 |
except Exception as e:
|
| 33 |
print(f"🔱 सिस्टम त्रुटि: {e}")
|
| 34 |
|
| 35 |
-
#
|
| 36 |
DIVY_CUSTOM_TEMPLATE = """{% for message in messages %}{{'<|' + message['role'] + '|>\n' + message['content'] + '<|end|>\n' }}{% endfor %}{% if add_generation_prompt and messages[-1]['role'] != 'assistant' %}{{- '<|assistant|>\n' -}}{% endif %}"""
|
| 37 |
|
| 38 |
if processor is not None:
|
| 39 |
processor.tokenizer.chat_template = DIVY_CUSTOM_TEMPLATE
|
| 40 |
|
| 41 |
def hanuman_phi_engine(message, history, image_path):
|
| 42 |
-
"""Phi-3.5 के लिए विशेष तार्किक इंजन -
|
| 43 |
if model is None or processor is None:
|
| 44 |
return "🔱 त्रुटि: मॉडल लोड नहीं हो सका। कृपया लॉग्स की जाँच करें।"
|
| 45 |
|
| 46 |
messages = []
|
| 47 |
|
| 48 |
-
#
|
|
|
|
| 49 |
if history:
|
| 50 |
-
for
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
messages.append({"role": "
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
current_text = message if message.strip() else "Please analyze this image deeply."
|
| 58 |
|
| 59 |
images_list = None
|
| 60 |
if image_path:
|
|
|
|
| 61 |
final_content_string = f"<|image_1|>\n{current_text}"
|
| 62 |
images_list = [Image.open(image_path).convert("RGB")]
|
| 63 |
else:
|
| 64 |
final_content_string = current_text
|
| 65 |
|
|
|
|
| 66 |
messages.append({"role": "user", "content": final_content_string})
|
| 67 |
|
| 68 |
try:
|
| 69 |
-
#
|
| 70 |
prompt = processor.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 71 |
|
| 72 |
# इनपुट्स तैयार करना
|
|
@@ -93,7 +104,12 @@ def hanuman_phi_engine(message, history, image_path):
|
|
| 93 |
|
| 94 |
# --- 🔱 दिव्य विज़न डैशबोर्ड ---
|
| 95 |
with gr.Blocks(title="Hanuman Phi-3.5 Supreme") as demo:
|
| 96 |
-
gr.HTML("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
chatbot = gr.Chatbot(height=550, label="दिव्य संवाद इतिहास")
|
| 99 |
|
|
@@ -118,7 +134,7 @@ with gr.Blocks(title="Hanuman Phi-3.5 Supreme") as demo:
|
|
| 118 |
|
| 119 |
gr.Markdown("---")
|
| 120 |
gr.Markdown("### 🚩 दिव्य पटेल जी के लिए सिस्टम रिपोर्ट:")
|
| 121 |
-
gr.Markdown("- **मॉडल:** Microsoft Phi-3.5-Vision (4.2B
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
demo.launch()
|
|
|
|
| 1 |
# --- 🔱 Hanuman Vision: Phi-3.5 Supreme Engine ---
|
| 2 |
# मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
|
| 3 |
# मॉडल: microsoft/Phi-3.5-vision-instruct (4.2B Parameters - 10GB RAM Safe)
|
| 4 |
+
# फिक्स: दिव्य जी का 'अजेय हिस्ट्री लूप' (Universal Gradio History Fix)
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import torch
|
|
|
|
| 11 |
|
| 12 |
model_id = "microsoft/Phi-3.5-vision-instruct"
|
| 13 |
|
| 14 |
+
print("🔱 हनुमान विज़न Phi-3.5 जागृत हो रहा है... दिव्य जी के 'अजेय हिस्ट्री लूप' के साथ।")
|
| 15 |
|
| 16 |
+
# चरों (Variables) को पहले ही परिभाषित करना ताकि NameError कभी न आए
|
| 17 |
model = None
|
| 18 |
processor = None
|
| 19 |
|
| 20 |
try:
|
| 21 |
+
# 🛠️ रैम ऑप्टिमाइजेशन और Flash Attention फिक्स (attn_implementation="eager")
|
|
|
|
| 22 |
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
model_id,
|
| 24 |
device_map="cpu",
|
| 25 |
trust_remote_code=True,
|
| 26 |
torch_dtype=torch.bfloat16,
|
| 27 |
low_cpu_mem_usage=True,
|
| 28 |
+
attn_implementation="eager"
|
| 29 |
)
|
| 30 |
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 31 |
except Exception as e:
|
| 32 |
print(f"🔱 सिस्टम त्रुटि: {e}")
|
| 33 |
|
| 34 |
+
# दिव्य जी का अजेय चैट टेम्पलेट
|
| 35 |
DIVY_CUSTOM_TEMPLATE = """{% for message in messages %}{{'<|' + message['role'] + '|>\n' + message['content'] + '<|end|>\n' }}{% endfor %}{% if add_generation_prompt and messages[-1]['role'] != 'assistant' %}{{- '<|assistant|>\n' -}}{% endif %}"""
|
| 36 |
|
| 37 |
if processor is not None:
|
| 38 |
processor.tokenizer.chat_template = DIVY_CUSTOM_TEMPLATE
|
| 39 |
|
| 40 |
def hanuman_phi_engine(message, history, image_path):
|
| 41 |
+
"""Phi-3.5 के लिए विशेष तार्किक इंजन - दिव्य जी के अजेय हिस्ट्री लूप के साथ"""
|
| 42 |
if model is None or processor is None:
|
| 43 |
return "🔱 त्रुटि: मॉडल लोड नहीं हो सका। कृपया लॉग्स की जाँच करें।"
|
| 44 |
|
| 45 |
messages = []
|
| 46 |
|
| 47 |
+
# 🛠️ दिव्य पटेल जी का अजेय हिस्ट्री लूप (The Supreme History Parser)
|
| 48 |
+
# यह Gradio के पुराने (List) और नए (Dictionary) दोनों फॉर्मेट्स को संभाल लेगा
|
| 49 |
if history:
|
| 50 |
+
for val in history:
|
| 51 |
+
# अगर Gradio पुराना वर्ज़न है (List of Lists)
|
| 52 |
+
if isinstance(val, (list, tuple)) and len(val) == 2:
|
| 53 |
+
user_msg, bot_msg = val
|
| 54 |
+
if user_msg: messages.append({"role": "user", "content": str(user_msg)})
|
| 55 |
+
if bot_msg: messages.append({"role": "assistant", "content": str(bot_msg)})
|
| 56 |
+
|
| 57 |
+
# अगर Gradio नया वर्ज़न है (Dictionaries)
|
| 58 |
+
elif isinstance(val, dict):
|
| 59 |
+
role = val.get("role")
|
| 60 |
+
content = val.get("content")
|
| 61 |
+
if role and content:
|
| 62 |
+
if role == "model": role = "assistant"
|
| 63 |
+
messages.append({"role": role, "content": str(content)})
|
| 64 |
+
|
| 65 |
+
# वर्तमान संदेश और चित्र की तैयारी
|
| 66 |
current_text = message if message.strip() else "Please analyze this image deeply."
|
| 67 |
|
| 68 |
images_list = None
|
| 69 |
if image_path:
|
| 70 |
+
# Phi-3.5 चित्र को इस विशेष टैग से पहचानता है
|
| 71 |
final_content_string = f"<|image_1|>\n{current_text}"
|
| 72 |
images_list = [Image.open(image_path).convert("RGB")]
|
| 73 |
else:
|
| 74 |
final_content_string = current_text
|
| 75 |
|
| 76 |
+
# वर्तमान प्रश्न जोड़ना
|
| 77 |
messages.append({"role": "user", "content": final_content_string})
|
| 78 |
|
| 79 |
try:
|
| 80 |
+
# टेम्पलेट लागू करना
|
| 81 |
prompt = processor.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 82 |
|
| 83 |
# इनपुट्स तैयार करना
|
|
|
|
| 104 |
|
| 105 |
# --- 🔱 दिव्य विज़न डैशबोर्ड ---
|
| 106 |
with gr.Blocks(title="Hanuman Phi-3.5 Supreme") as demo:
|
| 107 |
+
gr.HTML("""
|
| 108 |
+
<div style='text-align: center; background: linear-gradient(135deg, #ff8833, #b33c00); padding: 20px; border-radius: 15px; color: white;'>
|
| 109 |
+
<h1 style='color: white; margin: 0;'>🔱 Hanuman AI - Phi-3.5 Vision</h1>
|
| 110 |
+
<p style='margin: 5px 0 0 0;'><b>Pioneered by Divy Patel | भारत 🇮🇳 | त्रुटि-मुक्त अजेय संस्करण</b></p>
|
| 111 |
+
</div>
|
| 112 |
+
""")
|
| 113 |
|
| 114 |
chatbot = gr.Chatbot(height=550, label="दिव्य संवाद इतिहास")
|
| 115 |
|
|
|
|
| 134 |
|
| 135 |
gr.Markdown("---")
|
| 136 |
gr.Markdown("### 🚩 दिव्य पटेल जी के लिए सिस्टम रिपोर्ट:")
|
| 137 |
+
gr.Markdown("- **मॉडल:** Microsoft Phi-3.5-Vision (4.2B)\n- **फिक्स:** दिव्य जी द्वारा दिया गया 'सुपर-फ्लैश' हिस्ट्री लूप सफलतापूर्वक जोड़ा गया।")
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
demo.launch()
|