Vedika commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def generate_vedika_magic(message, history, image=None, video=None):
|
| 2 |
if model is None or tokenizer is None:
|
| 3 |
-
return history + [{"role": "assistant", "content": "🔱 सिस्टम त्रुटि: म
|
| 4 |
|
| 5 |
-
# हिस्ट्री को सुरक्षित रखें
|
| 6 |
recent_history = history[-2:] if len(history) > 2 else history
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
messages = [{"role": "system", "content": system_instruction}]
|
|
|
|
| 12 |
for user_msg, ai_msg in recent_history:
|
| 13 |
messages.append({"role": "user", "content": user_msg})
|
| 14 |
messages.append({"role": "assistant", "content": ai_msg})
|
| 15 |
-
|
| 16 |
if image is not None:
|
| 17 |
messages.append({"role": "user", "content": "Describe this image."})
|
| 18 |
if video is not None:
|
| 19 |
messages.append({"role": "user", "content": "Describe this video."})
|
| 20 |
-
|
| 21 |
messages.append({"role": "user", "content": message})
|
| 22 |
-
|
| 23 |
try:
|
| 24 |
text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 25 |
inputs = tokenizer([text_prompt], return_tensors="pt").to(model.device)
|
| 26 |
-
|
| 27 |
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 31 |
t.start()
|
| 32 |
|
|
|
|
| 33 |
response_text = ""
|
| 34 |
for new_token in streamer:
|
| 35 |
response_text += new_token
|
| 36 |
|
| 37 |
-
#
|
| 38 |
return history + [{"role": "assistant", "content": response_text}]
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
-
return history + [{"role": "assistant", "content": f"🔱 प्रसंस्करण त्रुटि: {str(e)}"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# --- 🔱 वेदिका 3.5 फ्लैश: भारत का अपना 2B AI (Super Fast Version) ---
|
| 2 |
+
# रचयिता एवं मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 7 |
+
from threading import Thread
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import os
|
| 10 |
+
import cv2
|
| 11 |
+
|
| 12 |
+
print("🔱 भारत का अजेय AI 'वेदिका 3.5 फ्लैश' सुपर-फास्ट मोड में जागृत हो रहा है...")
|
| 13 |
+
|
| 14 |
+
# CPU Optimization
|
| 15 |
+
os.environ["OMP_NUM_THREADS"] = "2"
|
| 16 |
+
torch.set_num_threads(2)
|
| 17 |
+
|
| 18 |
+
MODEL_ID = "pateltraders55455/Vedika-3.5-flash"
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
print(f"🔱 '{MODEL_ID}' (2B) लोड किया जा रहा है...")
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
+
MODEL_ID,
|
| 25 |
+
device_map="cpu",
|
| 26 |
+
torch_dtype=torch.bfloat16,
|
| 27 |
+
low_cpu_mem_usage=True,
|
| 28 |
+
trust_remote_code=True
|
| 29 |
+
)
|
| 30 |
+
print("🔱 विजय! 'वेदिका 3.5 फ्लैश' तैयार है!")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"🔱 मॉडल लोडिंग में त्रुटि: {e}")
|
| 33 |
+
model, tokenizer = None, None
|
| 34 |
+
|
| 35 |
def generate_vedika_magic(message, history, image=None, video=None):
|
| 36 |
if model is None or tokenizer is None:
|
| 37 |
+
return history + [{"role": "assistant", "content": "🔱 सिस्टम त्रुटि: मोडल लोड नहीं हो सका।"}]
|
| 38 |
|
|
|
|
| 39 |
recent_history = history[-2:] if len(history) > 2 else history
|
| 40 |
+
|
| 41 |
+
system_instruction = """You are 'Vedika 3.5 Flash'...
|
| 42 |
+
|
| 43 |
+
<think>
|
| 44 |
+
1. Analyze the user's query carefully.
|
| 45 |
+
2. Break down the problem into smaller logical steps.
|
| 46 |
+
3. Consider different solutions or facts.
|
| 47 |
+
4. Formulate the best response.
|
| 48 |
+
</think>
|
| 49 |
+
|
| 50 |
+
[Your final, polished answer goes here, OUTSIDE the think tags.]
|
| 51 |
+
"""
|
| 52 |
|
| 53 |
messages = [{"role": "system", "content": system_instruction}]
|
| 54 |
+
|
| 55 |
for user_msg, ai_msg in recent_history:
|
| 56 |
messages.append({"role": "user", "content": user_msg})
|
| 57 |
messages.append({"role": "assistant", "content": ai_msg})
|
| 58 |
+
|
| 59 |
if image is not None:
|
| 60 |
messages.append({"role": "user", "content": "Describe this image."})
|
| 61 |
if video is not None:
|
| 62 |
messages.append({"role": "user", "content": "Describe this video."})
|
| 63 |
+
|
| 64 |
messages.append({"role": "user", "content": message})
|
| 65 |
+
|
| 66 |
try:
|
| 67 |
text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 68 |
inputs = tokenizer([text_prompt], return_tensors="pt").to(model.device)
|
| 69 |
+
|
| 70 |
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 71 |
+
|
| 72 |
+
generate_kwargs = dict(
|
| 73 |
+
**inputs,
|
| 74 |
+
streamer=streamer,
|
| 75 |
+
max_new_tokens=512,
|
| 76 |
+
temperature=1,
|
| 77 |
+
top_p=0.9,
|
| 78 |
+
do_sample=True,
|
| 79 |
+
use_cache=True
|
| 80 |
+
)
|
| 81 |
|
| 82 |
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 83 |
t.start()
|
| 84 |
|
| 85 |
+
# पिछले स्ट्रिमर लूप को जारी रखते हैं
|
| 86 |
response_text = ""
|
| 87 |
for new_token in streamer:
|
| 88 |
response_text += new_token
|
| 89 |
|
| 90 |
+
# सही फॉर्मेट में परिणाम को लौटाएँ ताकि Gradio को समस्या न हो
|
| 91 |
return history + [{"role": "assistant", "content": response_text}]
|
| 92 |
|
| 93 |
except Exception as e:
|
| 94 |
+
return history + [{"role": "assistant", "content": f"🔱 प्रसंस्करण त्रुटि: {str(e)}"}]
|
| 95 |
+
|
| 96 |
+
# =============================================================================
|
| 97 |
+
# 🔱 वेदिका 3.5 फ्लैश का मल्टीमीडिया UI + Send बटन
|
| 98 |
+
# ============================================================================
|
| 99 |
+
|
| 100 |
+
with gr.Blocks() as demo:
|
| 101 |
+
gr.Markdown("## 🔱 Vedika 3.5 Flash (Super Fast)")
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
text_input = gr.Textbox(placeholder="वेदिका 3.5 फ्लैश से कुछ भी पूछें...")
|
| 105 |
+
image_input = gr.Image(type="filepath", label="Upload Photo")
|
| 106 |
+
video_input = gr.Video(label="Upload Video")
|
| 107 |
+
|
| 108 |
+
send_btn = gr.Button("Send")
|
| 109 |
+
chat_output = gr.Chatbot()
|
| 110 |
+
|
| 111 |
+
def chat_fn(message, history, image, video):
|
| 112 |
+
return generate_vedika_magic(message, history, image, video)
|
| 113 |
+
|
| 114 |
+
send_btn.click(chat_fn, [text_input, chat_output, image_input, video_input], chat_output)
|
| 115 |
+
|
| 116 |
+
if __name__ == "__main__":
|
| 117 |
+
demo.launch()
|
| 118 |
+
|