Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install the required libraries (run this on Colab)
|
| 2 |
+
!pip install git+https://github.com/openai/whisper.git
|
| 3 |
+
!pip install gradio
|
| 4 |
+
!pip install gTTS # You can use other TTS libraries if you prefer
|
| 5 |
+
!pip install groq
|
| 6 |
+
!pip install --upgrade pip setuptools wheel
|
| 7 |
+
!pip install git+https://github.com/openai/whisper.git --no-cache-dir
|
| 8 |
+
!pip install gradio --upgrade
|
| 9 |
+
!pip install gTTS --upgrade
|
| 10 |
+
!pip install groq
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
import whisper
|
| 14 |
+
import numpy as np
|
| 15 |
+
from groq import Groq
|
| 16 |
+
import os
|
| 17 |
+
from gtts import gTTS
|
| 18 |
+
import gradio as gr
|
| 19 |
+
|
| 20 |
+
# Initialize Whisper model
|
| 21 |
+
model = whisper.load_model("base")
|
| 22 |
+
|
| 23 |
+
# Function to transcribe audio using Whisper
|
| 24 |
+
def transcribe_audio(audio_file):
|
| 25 |
+
result = model.transcribe(audio_file)
|
| 26 |
+
return result['text']
|
| 27 |
+
|
| 28 |
+
# Function to interact with Groq LLM
|
| 29 |
+
def generate_response(transcription):
|
| 30 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 31 |
+
chat_completion = client.chat.completions.create(
|
| 32 |
+
messages=[{"role": "user", "content": transcription}],
|
| 33 |
+
model="llama3-groq-8b-8192-tool-use-preview",
|
| 34 |
+
)
|
| 35 |
+
response = chat_completion.choices[0].message.content
|
| 36 |
+
return response
|
| 37 |
+
|
| 38 |
+
# Function to convert text into speech using gTTS
|
| 39 |
+
def text_to_speech(text):
|
| 40 |
+
tts = gTTS(text)
|
| 41 |
+
tts.save("output.mp3")
|
| 42 |
+
return "output.mp3"
|
| 43 |
+
|
| 44 |
+
# Main function to handle the chatbot interaction
|
| 45 |
+
def chatbot_interaction(audio):
|
| 46 |
+
# Transcribe the uploaded audio to text
|
| 47 |
+
transcription = transcribe_audio(audio)
|
| 48 |
+
print(f"Transcription: {transcription}")
|
| 49 |
+
|
| 50 |
+
# Generate a response from Groq's LLM
|
| 51 |
+
response = generate_response(transcription)
|
| 52 |
+
print(f"LLM Response: {response}")
|
| 53 |
+
|
| 54 |
+
# Convert the LLM response to speech
|
| 55 |
+
output_audio = text_to_speech(response)
|
| 56 |
+
return output_audio
|
| 57 |
+
|
| 58 |
+
# Gradio interface to deploy the chatbot
|
| 59 |
+
def chatbot_ui():
|
| 60 |
+
# Input: Audio file upload, Output: Audio file with the response
|
| 61 |
+
gr.Interface(fn=chatbot_interaction, inputs="audio", outputs="audio").launch()
|
| 62 |
+
|
| 63 |
+
# Run the chatbot UI
|
| 64 |
+
chatbot_ui()
|