Ahsan-Shah commited on
Commit
5fbc551
·
verified ·
1 Parent(s): 277938b

Upload 2 files

Browse files
Files changed (2) hide show
  1. app (1).py +77 -0
  2. requirements.txt +10 -0
app (1).py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import gradio as gr
4
+ import tempfile
5
+ from groq import Groq
6
+ import whisper
7
+ from datetime import datetime
8
+ import torch
9
+ from TTS.api import TTS
10
+
11
+ # Load environment variable for Groq API key
12
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
13
+ if not GROQ_API_KEY:
14
+ raise ValueError("❌ GROQ_API_KEY not found in environment.")
15
+
16
+ # Initialize Groq client
17
+ client = Groq(api_key=GROQ_API_KEY)
18
+
19
+ # Load Whisper model
20
+ whisper_model = whisper.load_model("base")
21
+
22
+ # Load Coqui TTS model
23
+ tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=torch.cuda.is_available())
24
+
25
+ # Chat history for saving
26
+ chat_history = []
27
+
28
+ # Supported languages
29
+ languages = {"English": "en", "Urdu": "ur", "Pashto": "ps"}
30
+
31
+ # Voice-to-voice processing
32
+ def voice_to_voice(audio_input, selected_lang):
33
+ global chat_history
34
+
35
+ # Step 1: Transcribe
36
+ transcription = whisper_model.transcribe(audio_input)['text']
37
+ chat_history.append(("User", transcription))
38
+
39
+ # Step 2: Groq API response
40
+ chat_completion = client.chat.completions.create(
41
+ messages=[{"role": "user", "content": transcription}],
42
+ model="llama3-8b-8192",
43
+ stream=False,
44
+ )
45
+ response_text = chat_completion.choices[0].message.content
46
+ chat_history.append(("Bot", response_text))
47
+
48
+ # Step 3: TTS with Coqui
49
+ temp_wav = tempfile.mktemp(suffix=".wav")
50
+ tts.tts_to_file(text=response_text, file_path=temp_wav)
51
+
52
+ # Step 4: Save chat text file
53
+ chat_text = "\n".join([f"{role}: {msg}" for role, msg in chat_history])
54
+ filename = f"chat_history_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
55
+ with open(filename, "w", encoding="utf-8") as f:
56
+ f.write(chat_text)
57
+
58
+ return temp_wav, chat_text, filename
59
+
60
+ # Gradio UI
61
+ with gr.Blocks() as demo:
62
+ gr.Markdown("## 🎤 Real-time Voice Chatbot (Groq + Whisper + Coqui TTS)")
63
+
64
+ lang = gr.Dropdown(choices=list(languages.keys()), value="English", label="Select Language")
65
+
66
+ with gr.Row():
67
+ input_audio = gr.Audio(type="filepath", label="🎙️ Record Voice")
68
+ output_audio = gr.Audio(label="🤖 Bot Reply")
69
+
70
+ chatbox = gr.Textbox(label="📝 Conversation", lines=10)
71
+ download_btn = gr.File(label="📥 Download History")
72
+
73
+ btn = gr.Button("Talk")
74
+
75
+ btn.click(fn=voice_to_voice, inputs=[input_audio, lang], outputs=[output_audio, chatbox, download_btn])
76
+
77
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ gradio
3
+ groq
4
+ openai-whisper
5
+ torch<=2.5.1
6
+ scipy
7
+ ffmpeg-python
8
+ transformers
9
+ TTS
10
+ numpy