Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import tempfile
|
| 5 |
+
from groq import Groq
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 9 |
+
|
| 10 |
+
# Initialize the Groq client
|
| 11 |
+
client = Groq(api_key=api_key)
|
| 12 |
+
|
| 13 |
+
def transcribe_audio(filepath):
|
| 14 |
+
with open(filepath, "rb") as file:
|
| 15 |
+
transcription = client.audio.transcriptions.create(
|
| 16 |
+
file=(filepath, file.read()),
|
| 17 |
+
model="whisper-large-v3",
|
| 18 |
+
response_format="text"
|
| 19 |
+
)
|
| 20 |
+
return transcription.text
|
| 21 |
+
|
| 22 |
+
def generate_response(prompt):
|
| 23 |
+
chat_completion = client.chat.completions.create(
|
| 24 |
+
messages=[
|
| 25 |
+
{
|
| 26 |
+
"role": "system",
|
| 27 |
+
"content": "You are an AI assistant helping with interview questions. Provide concise and relevant answers."
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"role": "user",
|
| 31 |
+
"content": prompt
|
| 32 |
+
}
|
| 33 |
+
],
|
| 34 |
+
model="llama-3.1-70b-versatile"
|
| 35 |
+
)
|
| 36 |
+
return chat_completion.choices[0].message.content
|
| 37 |
+
|
| 38 |
+
def interview_helper(audio):
|
| 39 |
+
# Save the recorded audio to a temporary file
|
| 40 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
|
| 41 |
+
temp_audio.write(audio)
|
| 42 |
+
temp_audio_path = temp_audio.name
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
# Transcribe the audio
|
| 46 |
+
transcription = transcribe_audio(temp_audio_path)
|
| 47 |
+
|
| 48 |
+
# Generate response using LLM
|
| 49 |
+
response = generate_response(transcription)
|
| 50 |
+
|
| 51 |
+
return transcription, response
|
| 52 |
+
finally:
|
| 53 |
+
# Clean up the temporary file
|
| 54 |
+
os.unlink(temp_audio_path)
|
| 55 |
+
|
| 56 |
+
# Create Gradio interface
|
| 57 |
+
demo = gr.Interface(
|
| 58 |
+
fn=interview_helper,
|
| 59 |
+
inputs=gr.Audio(sources=['microphone'], type="filepath"),
|
| 60 |
+
outputs=[
|
| 61 |
+
gr.Textbox(label="Transcribed Question"),
|
| 62 |
+
gr.Textbox(label="AI Response")
|
| 63 |
+
],
|
| 64 |
+
title="Interview Helper",
|
| 65 |
+
description="Speak a question, and get an AI-generated response."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Launch the app
|
| 69 |
+
demo.launch()
|