Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
import whisper
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from gtts import gTTS
|
| 6 |
+
|
| 7 |
+
from groq import Groq
|
| 8 |
+
|
| 9 |
+
# Set up Groq API key
|
| 10 |
+
os.environ['GROQ_API_KEY'] = 'your_groq_api_key_here' # Replace with your actual Groq API key
|
| 11 |
+
groq_client = Groq(api_key=os.environ.get('GROQ_API_KEY'))
|
| 12 |
+
|
| 13 |
+
# Load Whisper model
|
| 14 |
+
whisper_model = whisper.load_model("base")
|
| 15 |
+
|
| 16 |
+
def process_audio(audio_file):
|
| 17 |
+
try:
|
| 18 |
+
# Transcribe audio using Whisper
|
| 19 |
+
result = whisper_model.transcribe(audio_file)
|
| 20 |
+
user_text = result['text']
|
| 21 |
+
|
| 22 |
+
# Generate response using Llama 8b model with Groq API
|
| 23 |
+
chat_completion = groq_client.chat.completions.create(
|
| 24 |
+
messages=[
|
| 25 |
+
{
|
| 26 |
+
"role": "user",
|
| 27 |
+
"content": user_text,
|
| 28 |
+
}
|
| 29 |
+
],
|
| 30 |
+
model="llama3-8b-8192",
|
| 31 |
+
)
|
| 32 |
+
response_text = chat_completion.choices[0].message.content
|
| 33 |
+
|
| 34 |
+
# Convert response text to speech using gTTS
|
| 35 |
+
tts = gTTS(text=response_text, lang='en')
|
| 36 |
+
audio_file_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3').name
|
| 37 |
+
tts.save(audio_file_path)
|
| 38 |
+
|
| 39 |
+
return response_text, audio_file_path
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return str(e), None
|
| 42 |
+
|
| 43 |
+
# Define custom CSS for the Gradio interface
|
| 44 |
+
css = """
|
| 45 |
+
/* General body styling */
|
| 46 |
+
body {
|
| 47 |
+
background: #f0f2f5; /* Light background for the app */
|
| 48 |
+
font-family: 'Arial', sans-serif;
|
| 49 |
+
color: #333;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
/* Container styling */
|
| 53 |
+
.gradio-container {
|
| 54 |
+
background: linear-gradient(135deg, #6e45e2, #88d3ce);
|
| 55 |
+
border-radius: 15px;
|
| 56 |
+
box-shadow: 0px 4px 20px rgba(0, 0, 0, 0.1);
|
| 57 |
+
padding: 20px;
|
| 58 |
+
margin: auto; /* Centering container */
|
| 59 |
+
width: 80%; /* Adjusted width for responsiveness */
|
| 60 |
+
max-width: 700px; /* Max width for larger screens */
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
/* Title styling */
|
| 64 |
+
.gradio-title {
|
| 65 |
+
color: #0056b3; /* Deep blue color for the title */
|
| 66 |
+
text-align: center;
|
| 67 |
+
margin-bottom: 20px;
|
| 68 |
+
font-size: 28px;
|
| 69 |
+
font-weight: bold;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
/* Input and output styling */
|
| 73 |
+
.gradio-input, .gradio-output {
|
| 74 |
+
border-radius: 10px;
|
| 75 |
+
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.15);
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
/* Textbox styling */
|
| 79 |
+
.gradio-textbox {
|
| 80 |
+
border: 2px solid #6e45e2;
|
| 81 |
+
background: #fff;
|
| 82 |
+
color: #333;
|
| 83 |
+
padding: 10px;
|
| 84 |
+
font-size: 16px;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
/* Number component styling */
|
| 88 |
+
.gradio-number {
|
| 89 |
+
border: 2px solid #6e45e2;
|
| 90 |
+
background: #fff;
|
| 91 |
+
color: #333;
|
| 92 |
+
border-radius: 8px;
|
| 93 |
+
padding: 10px;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
/* Button styling */
|
| 97 |
+
.gradio-button {
|
| 98 |
+
background: #6e45e2; /* Deep blue color for button */
|
| 99 |
+
color: #fff;
|
| 100 |
+
border: none;
|
| 101 |
+
border-radius: 8px;
|
| 102 |
+
padding: 12px 24px;
|
| 103 |
+
font-size: 18px;
|
| 104 |
+
cursor: pointer;
|
| 105 |
+
transition: background 0.3s, transform 0.3s;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
.gradio-button:hover {
|
| 109 |
+
background: #5a3d9c; /* Darker blue on hover */
|
| 110 |
+
transform: scale(1.05); /* Slightly enlarge on hover */
|
| 111 |
+
}
|
| 112 |
+
"""
|
| 113 |
+
|
| 114 |
+
# Create Gradio interface with custom CSS
|
| 115 |
+
iface = gr.Interface(
|
| 116 |
+
fn=process_audio,
|
| 117 |
+
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 118 |
+
outputs=[gr.Textbox(label="Response Text"), gr.Audio(label="Response Audio")],
|
| 119 |
+
live=True,
|
| 120 |
+
css=css,
|
| 121 |
+
title="Audio Transcription and Response Generator",
|
| 122 |
+
description="Upload an audio file to get a transcription and a response generated by the Llama 8b model.",
|
| 123 |
+
article="<h3>How to Use:</h3><ul><li>Upload an audio file.</li><li>Receive a transcribed text and response.</li></ul>"
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# Launch the Gradio app
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
iface.launch()
|
| 129 |
+
|