| import gradio as gr |
| import openai |
| import whisper |
| import os |
| from gtts import gTTS |
| import tempfile |
| import groq |
|
|
| |
| model = whisper.load_model("base") |
|
|
| |
| openai.api_key = "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" |
| groq_api_key = "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" |
|
|
| |
| def speech_to_text(audio_file): |
| audio = whisper.load_audio(audio_file) |
| audio = whisper.pad_or_trim(audio) |
| mel = whisper.log_mel_spectrogram(audio).to(model.device) |
| options = whisper.DecodingOptions(fp16=False) |
| result = whisper.decode(model, mel, options) |
| return result.text |
|
|
| |
| def chatbot_response(text): |
| response = openai.ChatCompletion.create( |
| model="gpt-4", |
| messages=[{"role": "user", "content": text}] |
| ) |
| return response["choices"][0]["message"]["content"] |
|
|
| |
| def text_to_speech(text): |
| tts = gTTS(text) |
| temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") |
| tts.save(temp_file.name) |
| return temp_file.name |
|
|
| |
| def process_audio(audio_file): |
| text = speech_to_text(audio_file) |
| response = chatbot_response(text) |
| audio_response = text_to_speech(response) |
| return response, audio_response |
|
|
| |
| def create_ui(): |
| with gr.Blocks(css="body { background-color: #f4f4f4; font-family: Arial, sans-serif; }") as demo: |
| gr.Markdown(""" |
| # ποΈ Audio-to-Audio Chatbot |
| **Talk to the AI and get voice responses in real-time!** |
| """) |
| |
| with gr.Row(): |
| audio_input = gr.Audio(source="microphone", type="filepath", label="π€ Speak Here") |
| submit_button = gr.Button("Send", variant="primary") |
| |
| with gr.Row(): |
| text_output = gr.Textbox(label="π¬ Chatbot Response", interactive=False) |
| audio_output = gr.Audio(label="π AI Voice Reply") |
| |
| submit_button.click(fn=process_audio, inputs=audio_input, outputs=[text_output, audio_output]) |
| |
| return demo |
|
|
| if __name__ == "__main__": |
| ui = create_ui() |
| ui.launch() |
|
|