adnaan05's picture
Update app.py
4846288 verified
raw
history blame contribute delete
No virus
1.7 kB
import os
os.environ["GROQ_API_KEY"] = "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"
import os
import gradio as gr
import whisper
from gtts import gTTS
import io
from groq import Groq
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
model = whisper.load_model("base")
def process_audio(file_path):
try:
audio = whisper.load_audio(file_path)
result = model.transcribe(audio)
text = result["text"]
chat_completion = client.chat.completions.create(
messages=[{"role": "user", "content": text}],
model="llama3-8b-8192",
)
response_message = chat_completion.choices[0].message.content.strip()
tts = gTTS(response_message)
response_audio_io = io.BytesIO()
tts.write_to_fp(response_audio_io) # Save the audio to the BytesIO object
response_audio_io.seek(0)
with open("response.mp3", "wb") as audio_file:
audio_file.write(response_audio_io.getvalue())
return response_message, "response.mp3"
except Exception as e:
return f"An error occurred: {e}", None
title = " Voice-to-Voice Chatbot Application"
description = "Developed by [Adnan Tariq](https://www.linkedin.com/in/adnaantariq/) with ❤️"
article = "### Instructions\n1. Upload an audio file.\n2. Wait for the transcription.\n3. Listen to the chatbot's response."
iface = gr.Interface(
fn=process_audio,
inputs=gr.Audio(type="filepath"), # Use type="filepath"
outputs=[gr.Textbox(label="Response Text"), gr.Audio(label="Response Audio")],
live=True,
title=title,
description=description,
theme="dark",
article=article
)
iface.launch()