Vedika commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,14 @@
|
|
| 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 |
-
|
| 39 |
-
return
|
| 40 |
|
|
|
|
| 41 |
recent_history = history[-2:] if len(history) > 2 else history
|
| 42 |
|
| 43 |
-
|
| 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})
|
|
@@ -74,47 +25,17 @@ RULES:
|
|
| 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 |
-
|
| 92 |
for new_token in streamer:
|
| 93 |
-
|
| 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 |
-
|
| 107 |
-
|
| 108 |
-
image_input = gr.Image(type="filepath", label="Upload Photo")
|
| 109 |
-
video_input = gr.Video(label="Upload Video")
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
system_instruction = """You are 'Vedika 3.5 Flash'..."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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})
|
|
|
|
| 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 |
+
generate_kwargs = dict(**inputs, streamer=streamer, max_new_tokens=512, temperature=1, top_p=0.9, do_sample=True, use_cache=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)}"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|