Vedika commited on
Commit
f4a4d6b
·
verified ·
1 Parent(s): ee0dd5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -36
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 = "openai/whisper-tiny"
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,120 @@ 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": "You are Vedika live an intelligent AI assistant. That talk live with the user so behave as a live assistant. Don't say you are a text based model. You can speak to user and solve their complex problems just by talking. You are entirely created by Divy Patel in India provide very fast and accurate with logical thinking answer to user don't show your thinking to user. According to previous question of user ask about their needs"},
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": "आप 'वेदिका' हैं, एक अत्यंत बुद्धिमान एआई असिस्टेंट। आप लाइव बातचीत कर रही हैं। आप भारत में आदरणीय दिव्य पटेल जी द्वारा बनाई गई हैं। आप हमेशा हिंदी में उत्तर देंगी, चाहे प्रश्न अंग्रेजी में ही क्यों पूछा जाए। हमेशा बहुत सम्मानपूर्वक बात करें और भारत का सदा आदर करें। अपने सोचने की प्रक्रिया (Thinking) उपयोगकर्ता को दिखाएं।"},
80
  {"role": "user", "content": user_text}
81
  ]
82
 
 
83
  text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
84
  inputs = tokenizer([text_prompt], return_tensors="pt").to(model.device)
85
 
86
+ # ⚡ लाइव सट्ीमिंग के लिए सरीमर स्थापित करना
87
+ streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
88
+
89
+ generation_kwargs = dict(
90
+ **inputs,
91
+ streamer=streamer,
92
+ max_new_tokens=150,
93
+ temperature=0.7,
94
  do_sample=True
95
  )
96
 
97
+ # जनरशनो अ गे (Thread) में चना
98
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
99
+ thread.start()
 
 
 
100
 
101
+ generated_text = ""
102
+ clean_response = ""
103
+
104
+ # जैसे-जैसे नए शब्द आएंगे, स्क्रीन पर लाइव दिखेंगे
105
+ for new_text in streamer:
106
+ generated_text += new_text
107
+
108
+ # यह जादुई Regex थिंकिंग (<think>...</think>) को लाइव हटाता है, भले ही वह अधूरा हो
109
+ clean_response = re.sub(r'<think>[\s\S]*?(?:</think>|$)', '', generated_text).strip()
110
+
111
+ # स्क्रीन को लाइव अपडेट करना
112
+ yield None, f"🗣️ आपने कहा: {user_text}\n\n🔱 वेदिका: {clean_response}..."
113
+
114
+ # जनरेशन समाप्त
115
+ thread.join()
116
+
117
  if not clean_response:
118
  clean_response = "जी, आपकी बात मेरे संज्ञान में आ गई है।"
119
 
120
+ yield None, f"🗣️ आपने कहा: {user_text}\n\n🔱 वेदिका: {clean_response}\n\n(आवाज़ उत्पन्न की जा रही है...)"
121
+
122
  # ==========================================
123
  # चरण 3: बोलना (Text to Speech)
124
  # ==========================================
125
  output_wav_path = "vedika_final_response.wav"
126
 
127
+ # आवाज़ बनाना
128
  asyncio.run(generate_edge_tts(clean_response, output_wav_path))
129
 
130
+ # अंतिम उत्तर: ऑडियो फाइल े स (autoplay=True कारण यह अपने आप बजेगी)
131
+ final_log = f"🗣️ आपने कहा: {user_text}\n\n🔱 वेदिका: {clean_response}"
132
+ yield output_wav_path, final_log
133
 
134
  except Exception as e:
135
+ yield None, f"🔱 क्षमा करें, प्रसंस्करण में तकनीकी बाधा आई: {str(e)}"
136
 
137
  # --- 🚩 स्वदेशी अजेय इंटरफेस (Gradio) 🚩 ---
138
 
139
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
140
  gr.Markdown(f"""
141
+ # 🔱 Vedika Voice Ecosystem (Bilingual & Live)
142
  **Pioneered by Divy Patel | Bharat 🇮🇳**
143
 
144
+ *यह वेदि का लाइव स्ट्रमिंग संसकरण है अब आपिंदया अंग्रेजी किी भी भषा में बोल है, वेदिकसमझ जाएगी।*
145
  """)
146
 
147
  with gr.Row():
148
  with gr.Column():
149
+ audio_input = gr.Audio(label="माइक चालू करें और बोलें (Hindi/English)", type="filepath")
150
+ submit_btn = gr.Button("वेदिका से लाइव संवाद करें 🚩", variant="primary")
151
 
152
  with gr.Column():
153
+ # autoplay=True आवज़ बनते हस्तः बजने लगेग
154
+ audio_output = gr.Audio(label="विका की मधुर वाणी", autoplay=True)
155
+ text_output = gr.Textbox(label="संवाद लॉग", lines=8)
156
 
157
  submit_btn.click(
158
  fn=process_all_in_one,