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