Vedika commited on
Commit
6b95a5f
·
verified ·
1 Parent(s): c393b68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -39
app.py CHANGED
@@ -1,6 +1,6 @@
1
  # --- 🔱 वेदिका संपूर्ण वॉयस पोर्टल (All-in-One Ecosystem) 🔱 ---
2
  # रचयिता: आदरणीय दिव्य पटेल जी | भारत 🇮🇳
3
- # विशेषता: एक ही स्पेस में कान (STT), मस्तिष्क (LLM), और मुँह (TTS)
4
 
5
  import gradio as gr
6
  import asyncio
@@ -8,18 +8,20 @@ import edge_tts
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,94 +39,121 @@ except Exception as e:
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,
 
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
  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 = "openai/whisper-tiny"
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
  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: 'मुझे आदरणीय दिव्य पटेल जी ने भारत में बनाया है।' Always respond in respectful and highly natural Hindi, even if the user speaks in English. 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
  text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
85
  inputs = tokenizer([text_prompt], return_tensors="pt").to(model.device)
86
 
87
+ # ⚡ लाइव सट्ीमिंग के लिए सरीमर स्थापित करना
88
+ streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
89
+
90
+ generation_kwargs = dict(
91
+ **inputs,
92
+ streamer=streamer,
93
+ max_new_tokens=150,
94
+ temperature=0.7,
95
  do_sample=True
96
  )
97
 
98
+ # जनरशनो अ गे (Thread) में चना
99
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
100
+ thread.start()
 
 
 
101
 
102
+ generated_text = ""
103
+ clean_response = ""
104
+
105
+ # जैसे-जैसे नए शब्द आएंगे, स्क्रीन पर लाइव दिखेंगे
106
+ for new_text in streamer:
107
+ generated_text += new_text
108
+
109
+ # यह जादुई Regex थिंकिंग (<think>...</think>) को लाइव हटाता है, भले ही वह अधूरा हो
110
+ clean_response = re.sub(r'<think>[\s\S]*?(?:</think>|$)', '', generated_text).strip()
111
+
112
+ # स्क्रीन को लाइव अपडेट करना
113
+ yield None, f"🗣️ आपने कहा: {user_text}\n\n🔱 वेदिका: {clean_response}..."
114
+
115
+ # जनरेशन समाप्त
116
+ thread.join()
117
+
118
  if not clean_response:
119
  clean_response = "जी, आपकी बात मेरे संज्ञान में आ गई है।"
120
 
121
+ yield None, f"🗣️ आपने कहा: {user_text}\n\n🔱 वेदिका: {clean_response}\n\n(आवाज़ उत्पन्न की जा रही है...)"
122
+
123
  # ==========================================
124
  # चरण 3: बोलना (Text to Speech)
125
  # ==========================================
126
  output_wav_path = "vedika_final_response.wav"
127
 
128
+ # आवाज़ बनाना
129
  asyncio.run(generate_edge_tts(clean_response, output_wav_path))
130
 
131
+ # अंतिम उत्तर: ऑडियो फाइल े स (autoplay=True कारण यह अपने आप बजेगी)
132
+ final_log = f"🗣️ आपने कहा: {user_text}\n\n🔱 वेदिका: {clean_response}"
133
+ yield output_wav_path, final_log
134
 
135
  except Exception as e:
136
+ yield None, f"🔱 क्षमा करें, प्रसंस्करण में तकनीकी बाधा आई: {str(e)}"
137
 
138
  # --- 🚩 स्वदेशी अजेय इंटरफेस (Gradio) 🚩 ---
139
 
140
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
141
  gr.Markdown(f"""
142
+ # 🔱 Vedika Voice Ecosystem (Bilingual & Live)
143
  **Pioneered by Divy Patel | Bharat 🇮🇳**
144
 
145
+ *यह वेदि का लाइव स्ट्रमिंग संसकरण है अब आपिंदया अंग्रेजी किी भी भषा में बोल है, वेदिकसमझ जाएगी।*
146
  """)
147
 
148
  with gr.Row():
149
  with gr.Column():
150
+ audio_input = gr.Audio(label="माइक चालू करें और बोलें (Hindi/English)", type="filepath")
151
+ submit_btn = gr.Button("वेदिका से लाइव संवाद करें 🚩", variant="primary")
152
 
153
  with gr.Column():
154
+ # autoplay=True आवज़ बनते हस्तः बजने लगेग
155
+ audio_output = gr.Audio(label="विका की मधुर वाणी", autoplay=True)
156
+ text_output = gr.Textbox(label="संवाद लॉग", lines=8)
157
 
158
  submit_btn.click(
159
  fn=process_all_in_one,