Patel Traders commited on
Commit
95c5f4b
·
verified ·
1 Parent(s): 5f33543

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -19
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
- # फिक्स: Flash Attention 2 Error & NameError Resolution
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
- # 🛠️ ERROR FIX 1: चरों (Variables) को पहले ही परिभाषित करना ताकि NameError कभी न आए
17
  model = None
18
  processor = None
19
 
20
  try:
21
- # 🛠️ ERROR FIX 2: attn_implementation="eager" का उपयोग
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 के लिए विशेष तार्किक इंजन - Incompatible Data Type Fix के साथ"""
43
  if model is None or processor is None:
44
  return "🔱 त्रुटि: मॉडल लोड नहीं हो सका। कृपया लॉग्स की जाँच करें।"
45
 
46
  messages = []
47
 
48
- # History को ल स्ट्रिंग (String) के रूप में जोड़ना
 
49
  if history:
50
- for past_user, past_bot in history:
51
- if past_user:
52
- messages.append({"role": "user", "content": str(past_user)})
53
- if past_bot:
54
- messages.append({"role": "assistant", "content": str(past_bot)})
55
-
56
- # Phi-3.5 के लिए Image को स्ट्रिंग टैग <|image_1|> में बदलना
 
 
 
 
 
 
 
 
 
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("<div style='text-align: center; color: #ff5500;'><h1>🔱 Hanuman AI - Phi-3.5 Vision (4.2B)</h1><p><b>Pioneered by Divy Patel | भारत 🇮🇳 | 10GB रैम सुरक्षित</b></p></div>")
 
 
 
 
 
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 - ~8.5GB RAM)\n- **सुरक्षा फिक्स:** 'Flash Attention 2' और 'NameError' को जड़ उखाड़ेंका गया है।")
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()