Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import whisper
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
from groq import Groq
|
| 6 |
+
|
| 7 |
+
# Set up Groq client (replace with your API key)
|
| 8 |
+
client = Groq(api_key=os.environ.get("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"))
|
| 9 |
+
|
| 10 |
+
# Load Whisper model
|
| 11 |
+
whisper_model = whisper.load_model("base")
|
| 12 |
+
|
| 13 |
+
# Function to process audio input
|
| 14 |
+
def process_audio_realtime(audio_file):
|
| 15 |
+
"""
|
| 16 |
+
Real-time processing of audio.
|
| 17 |
+
1. Transcribe audio with Whisper.
|
| 18 |
+
2. Process transcription using Llama.
|
| 19 |
+
3. Convert Llama output to audio using gTTS.
|
| 20 |
+
"""
|
| 21 |
+
# Step 1: Transcribe the audio to text using Whisper
|
| 22 |
+
transcription = whisper_model.transcribe(audio_file)["text"]
|
| 23 |
+
|
| 24 |
+
# Step 2: Process transcription using Llama model via Groq API
|
| 25 |
+
llama_response = client.chat.completions.create(
|
| 26 |
+
messages=[{"role": "user", "content": transcription}],
|
| 27 |
+
model="llama3-8b-8192", # Replace with your actual Llama model name
|
| 28 |
+
stream=False
|
| 29 |
+
).choices[0].message.content
|
| 30 |
+
|
| 31 |
+
# Step 3: Convert Llama response to audio using gTTS
|
| 32 |
+
tts = gTTS(text=llama_response, lang="en")
|
| 33 |
+
audio_output_path = "generated_output.mp3"
|
| 34 |
+
tts.save(audio_output_path)
|
| 35 |
+
|
| 36 |
+
return llama_response, audio_output_path
|
| 37 |
+
|
| 38 |
+
# Create Gradio interface for real-time simulation
|
| 39 |
+
interface = gr.Interface(
|
| 40 |
+
fn=process_audio_realtime,
|
| 41 |
+
inputs=gr.Audio(type="filepath", label="Input Audio"), # Removed `source` argument
|
| 42 |
+
outputs=[
|
| 43 |
+
gr.Textbox(label="Processed Text"), # Display processed text in real-time
|
| 44 |
+
gr.Audio(type="filepath", label="Generated Audio") # Output audio
|
| 45 |
+
],
|
| 46 |
+
live=True, # Enable real-time behavior
|
| 47 |
+
title="Real-Time Audio-to-Audio Application"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Launch Gradio app
|
| 51 |
+
interface.launch()
|