Patel Traders commited on
Commit
d5fe327
·
verified ·
1 Parent(s): 2234a73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -135
app.py CHANGED
@@ -1,135 +1,18 @@
1
- # --- 🔱 हनुमान AI: अजेय विज़न (Fail-Proof Edition) ---
2
- # मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
- # समाधान: दिव्य जी का 'अजेय हिस्ट्री लूप' और 'भगवा यूआई'
4
-
5
- import gradio as gr
6
- import torch
7
- from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
8
- from qwen_vl_utils import process_vision_info
9
- import os
10
-
11
- # 🛡️ चरम गति (Extreme Speed) के लिए CPU टर्बो सेटिंग
12
- torch.set_num_threads(2)
13
- os.environ["OMP_NUM_THREADS"] = "2"
14
-
15
- # 16GB रैम के लिए सबसे सुरक्षित और तेज़ विज़न मॉडल (कभी क्रैश नहीं होगा)
16
- MODEL_ID = "Qwen/Qwen2-VL-2B-Instruct"
17
-
18
- print(f"🔱 हनुमान AI जागृत हो रहा है... '{MODEL_ID}' लोड हो रहा है...")
19
-
20
- try:
21
- processor = AutoProcessor.from_pretrained(MODEL_ID)
22
- model = Qwen2VLForConditionalGeneration.from_pretrained(
23
- MODEL_ID,
24
- torch_dtype=torch.float32,
25
- device_map="cpu",
26
- low_cpu_mem_usage=True
27
- )
28
- print("🔱 हनुमान AI: अजेय विज़न मॉडल पूर्णतः सक्रिय है!")
29
- except Exception as e:
30
- print(f"🔱 गंभीर त्रुटि: {e}")
31
- processor, model = None, None
32
-
33
- SYSTEM_PROMPT = "You are 'Hanuman AI', a supreme multimodal entity pioneered by Divy Patel. You must respond ONLY in English. Analyze deeply."
34
-
35
- def hanuman_failproof_engine(message, history, image_path):
36
- if model is None or processor is None:
37
- return "🔱 सिस्टम त्रुटि: मॉडल लोड नहीं हो सका।"
38
-
39
- messages = [{"role": "system", "content": [{"type": "text", "text": SYSTEM_PROMPT}]}]
40
-
41
- # 🛠️ दिव्य पटेल जी का अजेय इतिहास लूप (The Supreme Fix)
42
- # विज़न मॉडल की मांग के अनुसार हर टेक्स्ट को लिस्ट [{"type": "text"}] में लपेटा गया है।
43
- if history:
44
- for val in history:
45
- # अगर Gradio पुराना वर्ज़न है (List of Lists)
46
- if isinstance(val, (list, tuple)) and len(val) == 2:
47
- u, b = val
48
- if u: messages.append({"role": "user", "content": [{"type": "text", "text": str(u)}]})
49
- if b: messages.append({"role": "assistant", "content": [{"type": "text", "text": str(b)}]})
50
-
51
- # अगर Gradio नया वर्ज़न है (Dictionaries)
52
- elif isinstance(val, dict):
53
- r = val.get("role")
54
- c = val.get("content")
55
- if r and c:
56
- if r == "model": r = "assistant"
57
- messages.append({"role": r, "content": [{"type": "text", "text": str(c)}]})
58
-
59
- # वर्तमान संदेश (चित्र और टेक्स्ट)
60
- current_content = []
61
- if image_path:
62
- current_content.append({"type": "image", "image": image_path})
63
-
64
- msg_text = message if message and str(message).strip() else "Analyze this multimedia deeply."
65
- current_content.append({"type": "text", "text": msg_text})
66
-
67
- messages.append({"role": "user", "content": current_content})
68
-
69
- try:
70
- # बिना किसी कस्टम टेम्पलेट के, डिफ़ॉल्ट सुरक्षित विधि
71
- text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
72
- image_inputs, video_inputs = process_vision_info(messages)
73
-
74
- inputs = processor(
75
- text=[text],
76
- images=image_inputs,
77
- videos=video_inputs,
78
- padding=True,
79
- return_tensors="pt"
80
- ).to("cpu")
81
-
82
- generated_ids = model.generate(**inputs, max_new_tokens=512)
83
- generated_ids_trimmed = [
84
- out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
85
- ]
86
- response = processor.batch_decode(
87
- generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
88
- )[0]
89
-
90
- return response
91
- except Exception as e:
92
- return f"🔱 तकनीकी बाधा: {str(e)}"
93
-
94
- # --- 🔱 दिव्य भगवा थीम (आपके सैंपल कोड से प्रेरित) ---
95
- divine_ui = """
96
- <style>
97
- .gradio-container { background-color: #fffaf0 !important; }
98
- .bhagwa-header {
99
- background: linear-gradient(135deg, #ff8833, #b33c00);
100
- padding: 30px; border-radius: 25px; color: white;
101
- text-align: center; box-shadow: 0 15px 35px rgba(179, 60, 0, 0.4);
102
- margin-bottom: 25px;
103
- }
104
- .bhagwa-header h1 { font-size: 36px !important; font-weight: 900 !important; margin-bottom: 5px; text-shadow: 0 4px 8px rgba(0,0,0,0.5); }
105
- .bhagwa-header p { font-size: 16px !important; opacity: 0.95; font-weight: 500; }
106
- </style>
107
- <div class="bhagwa-header">
108
- <h1>🔱 हनुमान AI - अजेय विज़न</h1>
109
- <p>Pioneered by Divy Patel | त्रुटि-मुक्त अजेय स्वदेशी तकनीक</p>
110
- </div>
111
- """
112
-
113
- with gr.Blocks() as demo:
114
- gr.HTML(divine_ui)
115
-
116
- chatbot = gr.Chatbot(height=500, label="दिव्य संवाद इतिहास")
117
-
118
- with gr.Row():
119
- msg_in = gr.Textbox(placeholder="अपना संदेश यहाँ लिखें...", scale=7)
120
- img_in = gr.Image(type="filepath", scale=3)
121
- submit_btn = gr.Button("सक्रिय करें", variant="primary")
122
-
123
- def chat_logic(m, h, i):
124
- if not m.strip() and i is None:
125
- return m, h, i
126
- res = hanuman_failproof_engine(m, h, i)
127
- disp = m if m.strip() else "[चित्र अपलोड किया गया]"
128
- h.append([disp, res])
129
- return "", h, None
130
-
131
- submit_btn.click(chat_logic, [msg_in, chatbot, img_in], [msg_in, chatbot, img_in])
132
- msg_in.submit(chat_logic, [msg_in, chatbot, img_in], [msg_in, chatbot, img_in])
133
-
134
- if __name__ == "__main__":
135
- demo.launch()
 
1
+ {{- bos_token -}}
2
+ {%- if messages[0]["role"] == "system" -%}
3
+ {{- raise_exception("System role not supported") -}}
4
+ {%- endif -%}
5
+ {%- for message in messages -%}
6
+ {%- if message["role"] == "user" != (loop.index0 % 2 == 0) -%}
7
+ {{- raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") -}}
8
+ {%- endif -%}
9
+ {%- if message["role"] == "assistant" -%}
10
+ {%- set role = "model" -%}
11
+ {%- else -%}
12
+ {%- set role = message["role"] -%}
13
+ {%- endif -%}
14
+ {{- "<start_of_turn>" + role + "\n" + message["content"] | trim + "<end_of_turn>\n" -}}
15
+ {%- endfor -%}
16
+ {%- if add_generation_prompt -%}
17
+ {{- "<start_of_turn>model\n" -}}
18
+ {%- endif -%}