Vedika commited on
Commit
b2f9ec8
·
verified ·
1 Parent(s): f080558

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -60
app.py CHANGED
@@ -1,82 +1,120 @@
 
 
 
 
1
  import gradio as gr
2
  import torch
3
- from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
 
4
  import os
 
 
5
 
6
- # --- 🔱 वेदिक टेक टू ीच: चयित दिवय पटे 🔱 ---
7
 
8
- MODEL_ID = "Pateltraders55455/Vedika-text-to-speech-TTS"
 
 
9
 
10
- print("वेदिक मॉडल लोड हो रहा है... धैर्य रखें।")
11
 
12
  try:
13
- # फ्री स्पेस की रैम बचाने के िए 4-bit ्वंटाइेशन उपयोग
14
- # यह 4B मॉडल को मात्र 3-4GB रैम में समेट देगा
15
  model = AutoModelForCausalLM.from_pretrained(
16
  MODEL_ID,
17
- device_map="auto",
18
- load_in_4bit=True, # अजेय क्वांटाइजेशन
19
  torch_dtype=torch.bfloat16,
20
  low_cpu_mem_usage=True,
21
  trust_remote_code=True
22
  )
23
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
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
- tts_engine = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- def generate_vedic_speech(text):
34
- if not text:
35
- return None, "कृपया कुछ शब्द लिखें।"
36
-
37
- if tts_engine is None:
38
- return None, "मॉडल लोड करने में समस्या आई है।"
39
 
40
  try:
41
- # ऑडियो जनरेशन
42
- output = tts_engine(text)
43
- # आउटपुट से ऑडियो डेटा और सैंपलिंग रेट निकालना
44
- audio_data = output["audio"]
45
- sampling_rate = output["sampling_rate"]
46
-
47
- return (sampling_rate, audio_data), "सफलतापूर्वक जनरेट किया गया! जय माँ भारती।"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  except Exception as e:
49
- return None, f"प्र करने में त्रुटि: {str(e)}"
50
-
51
- # --- 🚩 शानदार वेदिक इंटरफेस (UI) 🚩 ---
52
-
53
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
54
- gr.Markdown(f"""
55
- # 🔱 वेदिक टेक्स टू स्पीच (Vedika TTS) 🔱
56
- **रचयिता:** दिव्य पटेल जी | **मॉडल:** 4B पैरामीटर (Mistral आधारित)
57
-
58
- यह मॉडल भारत की आवाज़ को तकनीक के साथ जोड़ता है।
59
- """)
60
-
61
  with gr.Row():
62
- with gr.Column():
63
- input_text = gr.Textbox(
64
- label="अपना संदेश यहाँ लिखें",
65
- placeholder="जैसे: प्रणाम! आयुर्वेद जीवन का आधार है।",
66
- lines=5
67
- )
68
- submit_btn = gr.Button("आवाज़ उत्पन्न करें 🚩", variant="primary")
69
-
70
- with gr.Column():
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
- gr.Markdown("--- \n *Made with Pride in Bharat 🇮🇳*")
81
 
82
- demo.launch()
 
 
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()