Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from groq import Groq
|
| 5 |
+
|
| 6 |
+
# Load API key from Hugging Face Secret
|
| 7 |
+
api_key = os.getenv("GroqApiKey")
|
| 8 |
+
|
| 9 |
+
# Initialize Groq client
|
| 10 |
+
client = Groq(api_key=api_key)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def ask_ai(audio_file):
|
| 14 |
+
try:
|
| 15 |
+
# 1. Speech-to-Text
|
| 16 |
+
with open(audio_file, "rb") as file:
|
| 17 |
+
transcription = client.audio.transcriptions.create(
|
| 18 |
+
file=("user_input.wav", file.read()),
|
| 19 |
+
model="whisper-large-v3",
|
| 20 |
+
response_format="verbose_json",
|
| 21 |
+
)
|
| 22 |
+
user_text = transcription.text
|
| 23 |
+
|
| 24 |
+
# 2. LLM Completion
|
| 25 |
+
completion = client.chat.completions.create(
|
| 26 |
+
model="llama-3.1-8b-instant",
|
| 27 |
+
messages=[{"role": "user", "content": user_text}],
|
| 28 |
+
temperature=1,
|
| 29 |
+
max_completion_tokens=512,
|
| 30 |
+
top_p=1,
|
| 31 |
+
)
|
| 32 |
+
answer_text = completion.choices[0].message.content
|
| 33 |
+
|
| 34 |
+
# 3. Text-to-Speech
|
| 35 |
+
speech_file_path = Path("answer.wav")
|
| 36 |
+
response = client.audio.speech.create(
|
| 37 |
+
model="playai-tts",
|
| 38 |
+
voice="Calum-PlayAI",
|
| 39 |
+
response_format="wav",
|
| 40 |
+
input=answer_text,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Save audio file
|
| 44 |
+
with open(speech_file_path, "wb") as f:
|
| 45 |
+
for chunk in response.iter_bytes():
|
| 46 |
+
f.write(chunk)
|
| 47 |
+
|
| 48 |
+
return user_text, answer_text, str(speech_file_path)
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return "Error processing your request.", str(e), None
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Gradio Interface
|
| 55 |
+
ui = gr.Interface(
|
| 56 |
+
fn=ask_ai,
|
| 57 |
+
inputs=gr.Audio(
|
| 58 |
+
sources=["microphone", "upload"],
|
| 59 |
+
type="filepath",
|
| 60 |
+
label="Ask me a question (record or upload audio)"
|
| 61 |
+
),
|
| 62 |
+
outputs=[
|
| 63 |
+
gr.Textbox(label="Transcribed Question"),
|
| 64 |
+
gr.Textbox(label="AI Answer"),
|
| 65 |
+
gr.Audio(label="Answer Audio")
|
| 66 |
+
],
|
| 67 |
+
title="🎤 Voice Q&A with Groq AI",
|
| 68 |
+
description="Record or upload an audio file, get an AI-generated spoken answer.",
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
ui.launch()
|