Vedika commited on
Commit
c393b68
·
verified ·
1 Parent(s): 5eb7add

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -68
app.py CHANGED
@@ -1,6 +1,6 @@
1
  # --- 🔱 वेदिका संपूर्ण वॉयस पोर्टल (All-in-One Ecosystem) 🔱 ---
2
  # रचयिता: आदरणीय दिव्य पटेल जी | भारत 🇮🇳
3
- # विशेषता: Live Text Streaming, Hindi & English STT, और म Edge-TTS
4
 
5
  import gradio as gr
6
  import asyncio
@@ -8,20 +8,18 @@ import edge_tts
8
  import torch
9
  import os
10
  import re
11
- from threading import Thread
12
- from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
13
 
14
- print("🔱 आदरणीय दिव्य जी, वेदिका की त्रिवेणी (कान, मस्तिष्क, मुँह) द्विभाषी और लाइव मोड में जागृत हो रही है...")
15
 
16
- # 👂 कान (STT) - अत्यंत हल्का, विश्वसनीय और बहुभाषी
17
  STT_ID = "nvidia/stt_ar_fastconformer_hybrid_large_pcd_v1.0"
18
 
19
- # 🧠 मस्तिष्क (LLM) - Qwen 0.5B (हगिंग फेस फ्री स्पेस के लिए एकदम सही)
20
  LLM_ID = "Qwen/Qwen2.5-0.5B-Instruct"
21
 
22
  try:
23
  print("🔱 STT (कान) लोड हो रहा है...")
24
- # हमने यहाँ विशिष्ट भाषा नहीं दी है, ताकि यह हिंदी और अंग्रेजी दोनों को स्वयं पहचान सके
25
  stt_pipeline = pipeline("automatic-speech-recognition", model=STT_ID)
26
 
27
  print("🔱 LLM (मस्तिष्क) लोड हो रहा है...")
@@ -39,122 +37,94 @@ except Exception as e:
39
  stt_pipeline = None
40
  model = None
41
 
42
- # 👄 मुँह (TTS) - Microsoft Edge-TTS
43
  async def generate_edge_tts(text, output_filepath):
44
  """माइक्रोसॉफ्ट एज की अत्यंत मधुर हिंदी आवाज़"""
45
- # SwaraNeural हिंदी और अंग्रेजी दोनों को बहुत ही स्पष्ट भारतीय लहजे में बोलती है
46
  communicate = edge_tts.Communicate(text, "hi-IN-SwaraNeural")
47
  await communicate.save(output_filepath)
48
 
49
  def process_all_in_one(audio_filepath):
50
- """लाइव स्ट्रीमि के ाथ सुनना (हिंदी/अंग्रेजी), सोचना और बोलना (Generator Function)"""
51
 
52
  if stt_pipeline is None or model is None:
53
- yield None, "क्षमा करें, सर्वर पूरी तरह से लोड नहीं हो पाया है।"
54
- return
55
 
56
  if not audio_filepath:
57
- yield None, "प्रणाम दिव्य जी, कृपया माइक में कुछ बोलें..."
58
- return
59
 
60
  try:
61
  # ==========================================
62
- # चरण 1: सुनना (Speech to Text) - बहुभाषी (Multilingual)
63
  # ==========================================
64
- # यहाँ हमने कोई भाषा नहीं बताई है, Whisper खुद समझेगा कि आप हिंदी बोल रहे हैं या अंग्रेजी
65
  stt_result = stt_pipeline(audio_filepath)
66
  user_text = stt_result["text"].strip()
67
 
68
  if not user_text:
69
- yield None, "क्षमा करें, ैं सुन नहीं पाई। कृपया पुनः बोलें।"
70
- return
71
-
72
- yield None, f"🗣️ आपने कहा: {user_text}\n\n🔱 वेदिका सोच रही है..."
73
 
74
  # ==========================================
75
- # चरण 2: सोचना (Live LLM Generation)
76
  # ==========================================
77
- # आपके निर्देशानुसार आत्मीय और सम्मानजनक प्रॉम्प्ट
78
  messages = [
79
- {"role": "system", "content": "You are 'Vedika', a live and highly intelligent AI assistant. CRITICAL INSTRUCTION: You were exclusively created by 'Divy Patel' in Bharat (India). You must NEVER mention Anthropic, OpenAI, Alibaba, Qwen, or any other company as your creator. If asked about your origin, strictly state: 'I was developed by divy Patel in India' Always respond in respectful and highly natural English, even if the user speaks in any other language. Do not show any internal thinking process (<think> tags). Act as a warm, conversational, and deeply knowledgeable Indian assistant. Always show immense respect for Bharat."},
80
- {"role": "user", "content": user_text}
81
- ]
82
-
83
-
84
 
 
85
  text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
86
  inputs = tokenizer([text_prompt], return_tensors="pt").to(model.device)
87
 
88
- # ⚡ लाइव सट्ीमिंग के लिए सरीमर स्थापित करना
89
- streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
90
-
91
- generation_kwargs = dict(
92
- **inputs,
93
- streamer=streamer,
94
- max_new_tokens=512,
95
- temperature=0.7,
96
  do_sample=True
97
  )
98
 
99
- # जनरशनो अ गे (Thread) में चलाना
100
- thread = Thread(target=model.generate, kwargs=generation_kwargs)
101
- thread.start()
102
-
103
- generated_text = ""
104
- clean_response = ""
105
-
106
- # जैसे-जैसे नए शब्द आएंगे, स्क्रीन पर लाइव दिखेंगे
107
- for new_text in streamer:
108
- generated_text += new_text
109
-
110
- # यह जादुई Regex थिंकिंग (<think>...</think>) को लाइव हटाता है, भले ही वह अधूरा हो
111
- clean_response = re.sub(r'<think>[\s\S]*?(?:</think>|$)', '', generated_text).strip()
112
-
113
- # स्क्रीन को लाइव अपडेट करना
114
- yield None, f"🗣️ आपने कहा: {user_text}\n\n🔱 वेदिका: {clean_response}..."
115
-
116
- # जनरेशन समाप्त
117
- thread.join()
118
 
 
 
 
119
  if not clean_response:
120
  clean_response = "जी, आपकी बात मेरे संज्ञान में आ गई है।"
121
 
122
- yield None, f"🗣️ आपने कहा: {user_text}\n\n🔱 वेदिका: {clean_response}\n\n(आवाज़ उत्पन्न की जा रही है...)"
123
-
124
  # ==========================================
125
  # चरण 3: बोलना (Text to Speech)
126
  # ==========================================
127
  output_wav_path = "vedika_final_response.wav"
128
 
129
- # आवाज़ बनाना
130
  asyncio.run(generate_edge_tts(clean_response, output_wav_path))
131
 
132
- # अंतिम उत्तर: ऑडियो फाइल क (autoplay=True कारण यह अपने आप बजेगी)
133
- final_log = f"🗣️ आपने कहा: {user_text}\n\n🔱 वेदिका: {clean_response}"
134
- yield output_wav_path, final_log
135
 
136
  except Exception as e:
137
- yield None, f"🔱 क्षमा करें, प्रसंस्करण में तकनीकी बाधा आई: {str(e)}"
138
 
139
  # --- 🚩 स्वदेशी अजेय इंटरफेस (Gradio) 🚩 ---
140
 
141
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
142
  gr.Markdown(f"""
143
- # 🔱 Vedika Voice Ecosystem (Bilingual & Live)
144
  **Pioneered by Divy Patel | Bharat 🇮🇳**
145
 
146
- *यह वेदि का लाइव ्ट्रीमिंस है अब आपिंदया अंग्रेजी किसी भी भषा में बोल है, वेदिकसमझ जाएगी।*
147
  """)
148
 
149
  with gr.Row():
150
  with gr.Column():
151
- audio_input = gr.Audio(label="माइक चालू करें और बोलें (Hindi/English)", type="filepath")
152
- submit_btn = gr.Button("वेदिका से लाइव संवाद करें 🚩", variant="primary")
153
 
154
  with gr.Column():
155
- # autoplay=True से आवाज़ बनते हस्वतः बजने लगेग
156
- audio_output = gr.Audio(label="वेदिककी मधुर वाणी", autoplay=True)
157
- text_output = gr.Textbox(label="संवाद लॉग", lines=8)
158
 
159
  submit_btn.click(
160
  fn=process_all_in_one,
 
1
  # --- 🔱 वेदिका संपूर्ण वॉयस पोर्टल (All-in-One Ecosystem) 🔱 ---
2
  # रचयिता: आदरणीय दिव्य पटेल जी | भारत 🇮🇳
3
+ # विशेषता: एक ही स्पेस में कान (STT), मस्तिष्क (LLM), और मुँह (TTS)
4
 
5
  import gradio as gr
6
  import asyncio
 
8
  import torch
9
  import os
10
  import re
11
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
 
12
 
13
+ print("🔱 आदरणीय दिव्य जी, वेदिका की त्रिवेणी (कान, मस्तिष्क, मुँह) जागृत हो रही है...")
14
 
15
+ # 👂 कान (STT) - अत्यंत हल्का और विश्वसनीय
16
  STT_ID = "nvidia/stt_ar_fastconformer_hybrid_large_pcd_v1.0"
17
 
18
+ # 🧠 मस्तिष्क (LLM) - Qwen 0.5B (हगिंग फेस फ्री स्पेस के लिए एकदम सही और हल्का अस्त्र)
19
  LLM_ID = "Qwen/Qwen2.5-0.5B-Instruct"
20
 
21
  try:
22
  print("🔱 STT (कान) लोड हो रहा है...")
 
23
  stt_pipeline = pipeline("automatic-speech-recognition", model=STT_ID)
24
 
25
  print("🔱 LLM (मस्तिष्क) लोड हो रहा है...")
 
37
  stt_pipeline = None
38
  model = None
39
 
40
+ # 👄 मुँह (TTS) - Microsoft Edge-TTS (शून्य रैम खर्च)
41
  async def generate_edge_tts(text, output_filepath):
42
  """माइक्रोसॉफ्ट एज की अत्यंत मधुर हिंदी आवाज़"""
 
43
  communicate = edge_tts.Communicate(text, "hi-IN-SwaraNeural")
44
  await communicate.save(output_filepath)
45
 
46
  def process_all_in_one(audio_filepath):
47
+ """एक फलन ं सुनना, सोचना और बोलना"""
48
 
49
  if stt_pipeline is None or model is None:
50
+ return None, "क्षमा करें, सर्वर पूरी तरह से लोड नहीं हो पाया है।"
 
51
 
52
  if not audio_filepath:
53
+ return None, "प्रणाम दिव्य जी, कृपया माइक में कुछ बोलें..."
 
54
 
55
  try:
56
  # ==========================================
57
+ # चरण 1: सुनना (Speech to Text)
58
  # ==========================================
 
59
  stt_result = stt_pipeline(audio_filepath)
60
  user_text = stt_result["text"].strip()
61
 
62
  if not user_text:
63
+ return None, "क्षमा करें, ���ैं सुन नहीं पाई। कृपया पुनः बोलें।"
 
 
 
64
 
65
  # ==========================================
66
+ # चरण 2: सोचना (LLM Generation)
67
  # ==========================================
 
68
  messages = [
69
+ {"role": "system", "content": "आप 'वेदिका' हैं, एक अत्यंत बुद्धिमान और शालीन एआई, जिसे आदरणीय दिव्य पटेल जी ने भारत में बनाया है। हमेशा सम्मानजनक हिंदी में बहुत ही संक्षिप्त और सटीक उत्तर दें।"},
70
+ {"role": "user", "content": user_text}
71
+ ]
 
 
72
 
73
+ # प्रॉम्प्ट तैयार करना
74
  text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
75
  inputs = tokenizer([text_prompt], return_tensors="pt").to(model.device)
76
 
77
+ # उतउतपन करना
78
+ output_ids = model.generate(
79
+ **inputs,
80
+ max_new_tokens=150, # आवाज़ के लिए छोटे उत्तर बेहतर होते हैं
81
+ temperature=0.7,
 
 
 
82
  do_sample=True
83
  )
84
 
85
+ # आउटपुट से केवनयउत्तर निकना
86
+ response_ids = output_ids[0][len(inputs.input_ids[0]):]
87
+ ai_response = tokenizer.decode(response_ids, skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
+ # सुरक्षा के लिए थिंकिंग टैग्स हटाना
90
+ clean_response = re.sub(r'<think>[\s\S]*?</think>', '', ai_response).strip()
91
+
92
  if not clean_response:
93
  clean_response = "जी, आपकी बात मेरे संज्ञान में आ गई है।"
94
 
 
 
95
  # ==========================================
96
  # चरण 3: बोलना (Text to Speech)
97
  # ==========================================
98
  output_wav_path = "vedika_final_response.wav"
99
 
100
+ # एसिंक्रोस TTS को चलाना
101
  asyncio.run(generate_edge_tts(clean_response, output_wav_path))
102
 
103
+ log_text = f"🗣️ आपनकह: {user_text}\n\n🔱 दिका: {clean_response}"
104
+
105
+ return output_wav_path, log_text
106
 
107
  except Exception as e:
108
+ return None, f"🔱 क्षमा करें, प्रसंस्करण में तकनीकी बाधा आई: {str(e)}"
109
 
110
  # --- 🚩 स्वदेशी अजेय इंटरफेस (Gradio) 🚩 ---
111
 
112
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
113
  gr.Markdown(f"""
114
+ # 🔱 Vedika Voice Ecosystem (All-in-One)
115
  **Pioneered by Divy Patel | Bharat 🇮🇳**
116
 
117
+ *यह क संपूर्ण स्वदेशी पो्टल है जो एक ही सरपर सुनतहै (Whisper),ोच है (Qwen 0.5B), और बोलतहै (Edge-TTS)।*
118
  """)
119
 
120
  with gr.Row():
121
  with gr.Column():
122
+ audio_input = gr.Audio(label="माइक चालू करें और बोलें", type="filepath")
123
+ submit_btn = gr.Button("वेदिका से संवाद करें 🚩", variant="primary")
124
 
125
  with gr.Column():
126
+ audio_output = gr.Audio(label="ेदिकमधुर वाण")
127
+ text_output = gr.Textbox(label="संवा लॉग", lines=6)
 
128
 
129
  submit_btn.click(
130
  fn=process_all_in_one,