Vedika commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,60 @@
|
|
| 1 |
-
# --- 🔱 वेदिका 3.5 फ्लैश:
|
| 2 |
# रचयिता एवं मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
|
| 3 |
-
# विशेषता:
|
| 4 |
|
| 5 |
-
# 🛠️ 1. आपातकालीन इंस्टॉलर (Emergency Installer)
|
| 6 |
-
# यह 'torchvision' वाले एरर को हमेशा के लिए नष्ट कर देगा
|
| 7 |
-
import subprocess
|
| 8 |
-
import sys
|
| 9 |
-
|
| 10 |
-
def install_missing_weapons(package):
|
| 11 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 12 |
-
|
| 13 |
-
try:
|
| 14 |
-
import torchvision
|
| 15 |
-
import qwen_vl_utils
|
| 16 |
-
import av
|
| 17 |
-
except ImportError:
|
| 18 |
-
print("🔱 कुछ अस्त्र कम हैं! 'torchvision' और अन्य को जागृत किया जा रहा है...")
|
| 19 |
-
try:
|
| 20 |
-
install_missing_weapons("torchvision")
|
| 21 |
-
install_missing_weapons("qwen_vl_utils")
|
| 22 |
-
install_missing_weapons("av")
|
| 23 |
-
print("🔱 सभी अस्त्र सफलतापूर्वक स्थापित हो गए!")
|
| 24 |
-
except Exception as e:
|
| 25 |
-
print(f"🔱 अस्त्र स्थापना में त्रुटि: {e}")
|
| 26 |
-
|
| 27 |
-
# 🚀 2. मुख्य लाइब्रेरीज़
|
| 28 |
import gradio as gr
|
| 29 |
-
import torch
|
| 30 |
-
from transformers import AutoModelForCausalLM, AutoProcessor, TextIteratorStreamer
|
| 31 |
-
from qwen_vl_utils import process_vision_info
|
| 32 |
-
from PIL import Image
|
| 33 |
-
from threading import Thread
|
| 34 |
-
import os
|
| 35 |
import traceback
|
|
|
|
| 36 |
|
| 37 |
-
print("🔱 भारत का अजेय AI 'वेदिका 3.5 फ्लैश' जागृत हो रहा है...")
|
| 38 |
|
| 39 |
# 🛡️ CPU सुरक्षा
|
| 40 |
os.environ["OMP_NUM_THREADS"] = "2"
|
| 41 |
-
torch.set_num_threads(2)
|
| 42 |
|
| 43 |
MODEL_ID = "pateltraders55455/Vedika-3.5-flash"
|
| 44 |
|
| 45 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
DIVY_CUSTOM_TEMPLATE = r"""{%- set image_count = namespace(value=0) %}
|
| 47 |
{%- set video_count = namespace(value=0) %}
|
| 48 |
{%- macro render_content(content, do_vision_count, is_system_content=false) %}
|
|
@@ -51,230 +63,64 @@ DIVY_CUSTOM_TEMPLATE = r"""{%- set image_count = namespace(value=0) %}
|
|
| 51 |
{%- elif content is iterable and content is not mapping %}
|
| 52 |
{%- for item in content %}
|
| 53 |
{%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
|
| 54 |
-
{%- if is_system_content %}
|
| 55 |
-
{{- raise_exception('System message cannot contain images.') }}
|
| 56 |
-
{%- endif %}
|
| 57 |
-
{%- if do_vision_count %}
|
| 58 |
-
{%- set image_count.value = image_count.value + 1 %}
|
| 59 |
-
{%- endif %}
|
| 60 |
-
{%- if add_vision_id %}
|
| 61 |
-
{{- 'Picture ' ~ image_count.value ~ ': ' }}
|
| 62 |
-
{%- endif %}
|
| 63 |
{{- '<|vision_start|><|image_pad|><|vision_end|>' }}
|
| 64 |
{%- elif 'video' in item or item.type == 'video' %}
|
| 65 |
-
{%- if is_system_content %}
|
| 66 |
-
{{- raise_exception('System message cannot contain videos.') }}
|
| 67 |
-
{%- endif %}
|
| 68 |
-
{%- if do_vision_count %}
|
| 69 |
-
{%- set video_count.value = video_count.value + 1 %}
|
| 70 |
-
{%- endif %}
|
| 71 |
-
{%- if add_vision_id %}
|
| 72 |
-
{{- 'Video ' ~ video_count.value ~ ': ' }}
|
| 73 |
-
{%- endif %}
|
| 74 |
{{- '<|vision_start|><|video_pad|><|vision_end|>' }}
|
| 75 |
{%- elif 'text' in item %}
|
| 76 |
{{- item.text }}
|
| 77 |
-
{%- else %}
|
| 78 |
-
{{- raise_exception('Unexpected item type in content.') }}
|
| 79 |
{%- endif %}
|
| 80 |
{%- endfor %}
|
| 81 |
-
{%- elif content is none or content is undefined %}
|
| 82 |
-
{{- '' }}
|
| 83 |
-
{%- else %}
|
| 84 |
-
{{- raise_exception('Unexpected content type.') }}
|
| 85 |
{%- endif %}
|
| 86 |
{%- endmacro %}
|
| 87 |
-
{%- if not messages %}
|
| 88 |
-
{{- raise_exception('No messages provided.') }}
|
| 89 |
-
{%- endif %}
|
| 90 |
-
{%- if tools and tools is iterable and tools is not mapping %}
|
| 91 |
-
{{- '<|im_start|>system\n' }}
|
| 92 |
-
{{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
|
| 93 |
-
{%- for tool in tools %}
|
| 94 |
-
{{- "\n" }}
|
| 95 |
-
{{- tool | tojson }}
|
| 96 |
-
{%- endfor %}
|
| 97 |
-
{{- "\n</tools>" }}
|
| 98 |
-
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
|
| 99 |
-
{%- if messages[0].role == 'system' %}
|
| 100 |
-
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 101 |
-
{%- if content %}
|
| 102 |
-
{{- '\n\n' + content }}
|
| 103 |
-
{%- endif %}
|
| 104 |
-
{%- endif %}
|
| 105 |
-
{{- '<|im_end|>\n' }}
|
| 106 |
-
{%- else %}
|
| 107 |
-
{%- if messages[0].role == 'system' %}
|
| 108 |
-
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 109 |
-
{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
|
| 110 |
-
{%- endif %}
|
| 111 |
-
{%- endif %}
|
| 112 |
-
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
| 113 |
-
{%- for message in messages[::-1] %}
|
| 114 |
-
{%- set index = (messages|length - 1) - loop.index0 %}
|
| 115 |
-
{%- if ns.multi_step_tool and message.role == "user" %}
|
| 116 |
-
{%- set content = render_content(message.content, false)|trim %}
|
| 117 |
-
{%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
|
| 118 |
-
{%- set ns.multi_step_tool = false %}
|
| 119 |
-
{%- set ns.last_query_index = index %}
|
| 120 |
-
{%- endif %}
|
| 121 |
-
{%- endif %}
|
| 122 |
-
{%- endfor %}
|
| 123 |
-
{%- if ns.multi_step_tool %}
|
| 124 |
-
{{- raise_exception('No user query found in messages.') }}
|
| 125 |
-
{%- endif %}
|
| 126 |
{%- for message in messages %}
|
| 127 |
{%- set content = render_content(message.content, true)|trim %}
|
| 128 |
-
{
|
| 129 |
-
{%- if not loop.first %}
|
| 130 |
-
{{- raise_exception('System message must be at the beginning.') }}
|
| 131 |
-
{%- endif %}
|
| 132 |
-
{%- elif message.role == "user" %}
|
| 133 |
-
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 134 |
-
{%- elif message.role == "assistant" %}
|
| 135 |
-
{%- set reasoning_content = '' %}
|
| 136 |
-
{%- if message.reasoning_content is string %}
|
| 137 |
-
{%- set reasoning_content = message.reasoning_content %}
|
| 138 |
-
{%- else %}
|
| 139 |
-
{%- if '</think>' in content %}
|
| 140 |
-
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
|
| 141 |
-
{%- set content = content.split('</think>')[-1].lstrip('\n') %}
|
| 142 |
-
{%- endif %}
|
| 143 |
-
{%- endif %}
|
| 144 |
-
{%- set reasoning_content = reasoning_content|trim %}
|
| 145 |
-
{%- if loop.index0 > ns.last_query_index %}
|
| 146 |
-
{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
|
| 147 |
-
{%- else %}
|
| 148 |
-
{{- '<|im_start|>' + message.role + '\n' + content }}
|
| 149 |
-
{%- endif %}
|
| 150 |
-
{%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
|
| 151 |
-
{%- for tool_call in message.tool_calls %}
|
| 152 |
-
{%- if tool_call.function is defined %}
|
| 153 |
-
{%- set tool_call = tool_call.function %}
|
| 154 |
-
{%- endif %}
|
| 155 |
-
{%- if loop.first %}
|
| 156 |
-
{%- if content|trim %}
|
| 157 |
-
{{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 158 |
-
{%- else %}
|
| 159 |
-
{{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 160 |
-
{%- endif %}
|
| 161 |
-
{%- else %}
|
| 162 |
-
{{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 163 |
-
{%- endif %}
|
| 164 |
-
{%- if tool_call.arguments is defined %}
|
| 165 |
-
{%- for args_name, args_value in tool_call.arguments|items %}
|
| 166 |
-
{{- '<parameter=' + args_name + '>\n' }}
|
| 167 |
-
{%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
|
| 168 |
-
{{- args_value }}
|
| 169 |
-
{{- '\n</parameter>\n' }}
|
| 170 |
-
{%- endfor %}
|
| 171 |
-
{%- endif %}
|
| 172 |
-
{{- '</function>\n</tool_call>' }}
|
| 173 |
-
{%- endfor %}
|
| 174 |
-
{%- endif %}
|
| 175 |
-
{{- '<|im_end|>\n' }}
|
| 176 |
-
{%- elif message.role == "tool" %}
|
| 177 |
-
{%- if loop.previtem and loop.previtem.role != "tool" %}
|
| 178 |
-
{{- '<|im_start|>user' }}
|
| 179 |
-
{%- endif %}
|
| 180 |
-
{{- '\n<tool_response>\n' }}
|
| 181 |
-
{{- content }}
|
| 182 |
-
{{- '\n</tool_response>' }}
|
| 183 |
-
{%- if not loop.last and loop.nextitem.role != "tool" %}
|
| 184 |
-
{{- '<|im_end|>\n' }}
|
| 185 |
-
{%- elif loop.last %}
|
| 186 |
-
{{- '<|im_end|>\n' }}
|
| 187 |
-
{%- endif %}
|
| 188 |
-
{%- else %}
|
| 189 |
-
{{- raise_exception('Unexpected message role.') }}
|
| 190 |
-
{%- endif %}
|
| 191 |
{%- endfor %}
|
| 192 |
{%- if add_generation_prompt %}
|
| 193 |
{{- '<|im_start|>assistant\n' }}
|
| 194 |
-
{%- if enable_thinking is defined and enable_thinking is true %}
|
| 195 |
-
{{- '<think>\n' }}
|
| 196 |
-
{%- else %}
|
| 197 |
-
{{- '<think>\n\n</think>\n\n' }}
|
| 198 |
-
{%- endif %}
|
| 199 |
{%- endif %}"""
|
| 200 |
|
| 201 |
-
#
|
| 202 |
-
INIT_ERROR = ""
|
| 203 |
-
model = None
|
| 204 |
-
processor = None
|
| 205 |
-
|
| 206 |
-
try:
|
| 207 |
-
print(f"🔱 मॉडल लोड किया जा रहा है...")
|
| 208 |
-
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 209 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 210 |
-
MODEL_ID,
|
| 211 |
-
device_map="cpu",
|
| 212 |
-
torch_dtype=torch.bfloat16,
|
| 213 |
-
low_cpu_mem_usage=True,
|
| 214 |
-
trust_remote_code=True
|
| 215 |
-
)
|
| 216 |
-
print("🔱 विजय! 'वेदिका 3.5 फ्लैश' सक्रिय हो गया है!")
|
| 217 |
-
except Exception as e:
|
| 218 |
-
error_details = traceback.format_exc()
|
| 219 |
-
print(f"🔱 मॉडल लोडिंग में त्रुटि:\n{error_details}")
|
| 220 |
-
INIT_ERROR = f"त्रुटि संदेश: {str(e)}\n\nविस्तृत रिपोर्ट:\n{error_details}"
|
| 221 |
-
|
| 222 |
-
# ⚡ 5. जनरेशन इंजन
|
| 223 |
def generate_vedika_magic(message, history):
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
if model is None or processor is None:
|
| 229 |
-
yield "🔱 सिस्टम त्रुटि: मॉडल लोड नहीं हो सका।"
|
| 230 |
return
|
| 231 |
|
| 232 |
text_input = message.get("text", "")
|
| 233 |
files = message.get("files", [])
|
| 234 |
|
| 235 |
if not text_input.strip() and not files:
|
| 236 |
-
yield "कृपया कुछ प्रश्न पू
|
| 237 |
return
|
| 238 |
|
| 239 |
messages = [
|
| 240 |
-
{"role": "system", "content": "You are 'Vedika 3.5 Flash', an ultra-fast
|
| 241 |
]
|
| 242 |
|
| 243 |
-
# 🛡️ हिस्ट्री को क्रैश से बचाना
|
| 244 |
recent_history = history[-2:] if len(history) > 2 else history
|
| 245 |
for past_turn in recent_history:
|
| 246 |
u_msg, a_msg = past_turn[0], past_turn[1]
|
| 247 |
-
|
| 248 |
-
clean_u_msg = ""
|
| 249 |
-
if isinstance(u_msg, (tuple, list, dict)):
|
| 250 |
-
clean_u_msg = "[मीडिया अपलोड किया गया था]"
|
| 251 |
-
else:
|
| 252 |
-
clean_u_msg = str(u_msg)
|
| 253 |
-
|
| 254 |
messages.append({"role": "user", "content": clean_u_msg})
|
| 255 |
messages.append({"role": "assistant", "content": str(a_msg)})
|
| 256 |
|
| 257 |
-
# 🎥 चित्र और वीडियो को अलग-अलग पहचानना
|
| 258 |
user_content = []
|
| 259 |
|
| 260 |
if files:
|
| 261 |
for file_path in files:
|
| 262 |
ext = os.path.splitext(file_path)[1].lower()
|
| 263 |
-
if ext in ['.mp4', '.avi', '.mov'
|
| 264 |
user_content.append({"type": "video", "video": file_path})
|
| 265 |
else:
|
| 266 |
user_content.append({"type": "image", "image": file_path})
|
| 267 |
|
| 268 |
if text_input:
|
| 269 |
user_content.append({"type": "text", "text": text_input})
|
| 270 |
-
|
| 271 |
-
if not user_content:
|
| 272 |
-
user_content.append({"type": "text", "text": "इस मीडिया का वर्णन करें।"})
|
| 273 |
|
| 274 |
messages.append({"role": "user", "content": user_content})
|
| 275 |
|
| 276 |
try:
|
| 277 |
-
# प्रॉम्प्ट को आपके जादुई टेंप्लेट से गुजारना
|
| 278 |
text_prompt = processor.apply_chat_template(
|
| 279 |
messages,
|
| 280 |
chat_template=DIVY_CUSTOM_TEMPLATE,
|
|
@@ -282,7 +128,6 @@ def generate_vedika_magic(message, history):
|
|
| 282 |
add_generation_prompt=True
|
| 283 |
)
|
| 284 |
|
| 285 |
-
# ⚡ qwen_vl_utils का ब्रह्मास्त्र
|
| 286 |
image_inputs, video_inputs = process_vision_info(messages)
|
| 287 |
|
| 288 |
inputs = processor(
|
|
@@ -300,9 +145,7 @@ def generate_vedika_magic(message, history):
|
|
| 300 |
streamer=streamer,
|
| 301 |
max_new_tokens=512,
|
| 302 |
temperature=0.7,
|
| 303 |
-
|
| 304 |
-
do_sample=True,
|
| 305 |
-
use_cache=True
|
| 306 |
)
|
| 307 |
|
| 308 |
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
|
@@ -317,13 +160,13 @@ def generate_vedika_magic(message, history):
|
|
| 317 |
yield f"🔱 प्रसंस्करण त्रुटि (Processing Error): {str(e)}"
|
| 318 |
|
| 319 |
# ============================================================================
|
| 320 |
-
# 🔱
|
| 321 |
# ============================================================================
|
| 322 |
demo = gr.ChatInterface(
|
| 323 |
fn=generate_vedika_magic,
|
| 324 |
multimodal=True,
|
| 325 |
-
title="🔱 Vedika 3.5 Flash (
|
| 326 |
-
description="**Pioneered by Divy Patel | Bharat 🇮🇳**<br>य
|
| 327 |
textbox=gr.MultimodalTextbox(
|
| 328 |
placeholder="यहाँ प्रश्न लिखें या 📎 बटन से चित्र/वीडियो अपलोड करें...",
|
| 329 |
file_types=["image", "video"]
|
|
@@ -332,4 +175,6 @@ demo = gr.ChatInterface(
|
|
| 332 |
)
|
| 333 |
|
| 334 |
if __name__ == "__main__":
|
| 335 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
| 1 |
+
# --- 🔱 वेदिका 3.5 फ्लैश: सेफ मोड एवं डिबगिंग संस्करण ---
|
| 2 |
# रचयिता एवं मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
|
| 3 |
+
# विशेषता: 100% UI Launch Guarantee, Detailed Error Reporting
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import traceback
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
print("🔱 भारत का अजेय AI 'वेदिका 3.5 फ्लैश' डायग्नोस्टिक मोड में जागृत हो रहा है...")
|
| 10 |
|
| 11 |
# 🛡️ CPU सुरक्षा
|
| 12 |
os.environ["OMP_NUM_THREADS"] = "2"
|
|
|
|
| 13 |
|
| 14 |
MODEL_ID = "pateltraders55455/Vedika-3.5-flash"
|
| 15 |
|
| 16 |
+
# ग्लोबल चरों की घोषणा
|
| 17 |
+
model = None
|
| 18 |
+
processor = None
|
| 19 |
+
INIT_ERROR = ""
|
| 20 |
+
IS_READY = False
|
| 21 |
+
|
| 22 |
+
# 🚀 1. सुरक्षित लोडिंग तंत्र (Safe Loading Mechanism)
|
| 23 |
+
try:
|
| 24 |
+
import torch
|
| 25 |
+
from transformers import AutoModelForCausalLM, AutoProcessor, TextIteratorStreamer
|
| 26 |
+
from qwen_vl_utils import process_vision_info
|
| 27 |
+
from PIL import Image
|
| 28 |
+
from threading import Thread
|
| 29 |
+
|
| 30 |
+
torch.set_num_threads(2)
|
| 31 |
+
|
| 32 |
+
print(f"🔱 '{MODEL_ID}' की फाइलों को रिपॉजिटरी से जाँचा जा रहा है...")
|
| 33 |
+
|
| 34 |
+
# ⚡ यदि रिपॉजिटरी खाली है, तो यह यहीं एरर पकड़ लेगा
|
| 35 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 36 |
+
|
| 37 |
+
# ⚡ bfloat16 की जगह हम float32 (सुरक्षित) का उपयोग कर रहे हैं
|
| 38 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 39 |
+
MODEL_ID,
|
| 40 |
+
device_map="cpu",
|
| 41 |
+
torch_dtype=torch.float32, # यह किसी भी CPU पर क्रैश नहीं होगा
|
| 42 |
+
low_cpu_mem_usage=True,
|
| 43 |
+
trust_remote_code=True
|
| 44 |
+
)
|
| 45 |
+
IS_READY = True
|
| 46 |
+
print("🔱 विजय! 'वेदिका 3.5 फ्लैश' पूर्णतः सक्रिय हो गया है!")
|
| 47 |
+
|
| 48 |
+
except ImportError as ie:
|
| 49 |
+
error_msg = f"लाइब्रेरी गायब है (Missing Library): {ie}"
|
| 50 |
+
print(f"🔱 {error_msg}")
|
| 51 |
+
INIT_ERROR = error_msg
|
| 52 |
+
except Exception as e:
|
| 53 |
+
error_details = traceback.format_exc()
|
| 54 |
+
print(f"🔱 मॉडल की फाइलों में गंभीर त्रुटि (क्या रिपॉजिटरी में फाइलें हैं?):\n{error_details}")
|
| 55 |
+
INIT_ERROR = f"मॉडल लोड नहीं हो सका।\n\nकारण:\n{str(e)}\n\n(संभावित कारण: आपकी रिपॉजिटरी '{MODEL_ID}' खाली है या फाइलें पूरी नहीं हैं।)"
|
| 56 |
+
|
| 57 |
+
# 📜 दिव्य जी का अजेय चैट टेंप्लेट
|
| 58 |
DIVY_CUSTOM_TEMPLATE = r"""{%- set image_count = namespace(value=0) %}
|
| 59 |
{%- set video_count = namespace(value=0) %}
|
| 60 |
{%- macro render_content(content, do_vision_count, is_system_content=false) %}
|
|
|
|
| 63 |
{%- elif content is iterable and content is not mapping %}
|
| 64 |
{%- for item in content %}
|
| 65 |
{%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
{{- '<|vision_start|><|image_pad|><|vision_end|>' }}
|
| 67 |
{%- elif 'video' in item or item.type == 'video' %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
{{- '<|vision_start|><|video_pad|><|vision_end|>' }}
|
| 69 |
{%- elif 'text' in item %}
|
| 70 |
{{- item.text }}
|
|
|
|
|
|
|
| 71 |
{%- endif %}
|
| 72 |
{%- endfor %}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
{%- endif %}
|
| 74 |
{%- endmacro %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
{%- for message in messages %}
|
| 76 |
{%- set content = render_content(message.content, true)|trim %}
|
| 77 |
+
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>\n' }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
{%- endfor %}
|
| 79 |
{%- if add_generation_prompt %}
|
| 80 |
{{- '<|im_start|>assistant\n' }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
{%- endif %}"""
|
| 82 |
|
| 83 |
+
# ⚡ 2. जनरेशन इंजन
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
def generate_vedika_magic(message, history):
|
| 85 |
+
# 🔴 यदि सर्वर शुरू होते ही कोई एरर आया था, तो वह यूआई में लाल रंग में दिखेगा
|
| 86 |
+
if not IS_READY:
|
| 87 |
+
yield f"⚠️ **गंभीर सिस्टम त्रुटि (System Error):**\n\n{INIT_ERROR}\n\n**दिव्य जी के लिए सुझाव:**\nकृपया जाँचें कि क्या आपने '{MODEL_ID}' में 3-4 GB वाली `.safetensors` और `config.json` फाइलें अपलोड कर दी हैं। यदि नहीं, तो कृपया पहले 'Qwen/Qwen2-VL-2B-Instruct' को अपने नाम से Duplicate (कॉपी) करें।"
|
|
|
|
|
|
|
|
|
|
| 88 |
return
|
| 89 |
|
| 90 |
text_input = message.get("text", "")
|
| 91 |
files = message.get("files", [])
|
| 92 |
|
| 93 |
if not text_input.strip() and not files:
|
| 94 |
+
yield "कृपया कुछ प्रश्न पू��ें या कोई मीडिया अपलोड करें।"
|
| 95 |
return
|
| 96 |
|
| 97 |
messages = [
|
| 98 |
+
{"role": "system", "content": "You are 'Vedika 3.5 Flash', an ultra-fast AI model created by Divy Patel in Bharat."},
|
| 99 |
]
|
| 100 |
|
|
|
|
| 101 |
recent_history = history[-2:] if len(history) > 2 else history
|
| 102 |
for past_turn in recent_history:
|
| 103 |
u_msg, a_msg = past_turn[0], past_turn[1]
|
| 104 |
+
clean_u_msg = "[मीडिया अपलोड किया गया था]" if isinstance(u_msg, (tuple, list, dict)) else str(u_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
messages.append({"role": "user", "content": clean_u_msg})
|
| 106 |
messages.append({"role": "assistant", "content": str(a_msg)})
|
| 107 |
|
|
|
|
| 108 |
user_content = []
|
| 109 |
|
| 110 |
if files:
|
| 111 |
for file_path in files:
|
| 112 |
ext = os.path.splitext(file_path)[1].lower()
|
| 113 |
+
if ext in ['.mp4', '.avi', '.mov']:
|
| 114 |
user_content.append({"type": "video", "video": file_path})
|
| 115 |
else:
|
| 116 |
user_content.append({"type": "image", "image": file_path})
|
| 117 |
|
| 118 |
if text_input:
|
| 119 |
user_content.append({"type": "text", "text": text_input})
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
messages.append({"role": "user", "content": user_content})
|
| 122 |
|
| 123 |
try:
|
|
|
|
| 124 |
text_prompt = processor.apply_chat_template(
|
| 125 |
messages,
|
| 126 |
chat_template=DIVY_CUSTOM_TEMPLATE,
|
|
|
|
| 128 |
add_generation_prompt=True
|
| 129 |
)
|
| 130 |
|
|
|
|
| 131 |
image_inputs, video_inputs = process_vision_info(messages)
|
| 132 |
|
| 133 |
inputs = processor(
|
|
|
|
| 145 |
streamer=streamer,
|
| 146 |
max_new_tokens=512,
|
| 147 |
temperature=0.7,
|
| 148 |
+
do_sample=True
|
|
|
|
|
|
|
| 149 |
)
|
| 150 |
|
| 151 |
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
|
|
|
| 160 |
yield f"🔱 प्रसंस्करण त्रुटि (Processing Error): {str(e)}"
|
| 161 |
|
| 162 |
# ============================================================================
|
| 163 |
+
# 🔱 3. शुद्ध यूआई (यह किसी भी कीमत पर क्रैश नहीं होगा)
|
| 164 |
# ============================================================================
|
| 165 |
demo = gr.ChatInterface(
|
| 166 |
fn=generate_vedika_magic,
|
| 167 |
multimodal=True,
|
| 168 |
+
title="🔱 Vedika 3.5 Flash (Diagnostic Mode)",
|
| 169 |
+
description="**Pioneered by Divy Patel | Bharat 🇮🇳**<br>यदि मॉडल में कोई समस्या होगी, तो वह आपको स्क्रीन पर स्पष्ट रूप से बता दी जाएगी।",
|
| 170 |
textbox=gr.MultimodalTextbox(
|
| 171 |
placeholder="यहाँ प्रश्न लिखें या 📎 बटन से चित्र/वीडियो अपलोड करें...",
|
| 172 |
file_types=["image", "video"]
|
|
|
|
| 175 |
)
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|
| 178 |
+
demo.launch()
|
| 179 |
+
|
| 180 |
+
|