Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import whisper
|
4 |
+
from gtts import gTTS
|
5 |
+
import io
|
6 |
+
from groq import Groq
|
7 |
+
|
8 |
+
# Initialize the Groq client
|
9 |
+
GROQ_API_KEY = "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"
|
10 |
+
|
11 |
+
# Load the Whisper model
|
12 |
+
model = whisper.load_model("base") # You can choose other models like "small", "medium", "large"
|
13 |
+
|
14 |
+
def process_audio(file_path):
|
15 |
+
try:
|
16 |
+
# Load the audio file
|
17 |
+
audio = whisper.load_audio(file_path)
|
18 |
+
|
19 |
+
# Transcribe the audio using Whisper
|
20 |
+
result = model.transcribe(audio)
|
21 |
+
text = result["text"]
|
22 |
+
|
23 |
+
# Generate a response using Groq
|
24 |
+
chat_completion = client.chat.completions.create(
|
25 |
+
messages=[{"role": "user", "content": text}],
|
26 |
+
model="llama3-8b-8192", # Replace with the correct model if necessary
|
27 |
+
)
|
28 |
+
|
29 |
+
# Access the response using dot notation
|
30 |
+
response_message = chat_completion.choices[0].message.content.strip()
|
31 |
+
|
32 |
+
# Convert the response text to speech
|
33 |
+
tts = gTTS(response_message)
|
34 |
+
response_audio_io = io.BytesIO()
|
35 |
+
tts.write_to_fp(response_audio_io) # Save the audio to the BytesIO object
|
36 |
+
response_audio_io.seek(0)
|
37 |
+
|
38 |
+
# Save audio to a file to ensure it's generated correctly
|
39 |
+
with open("response.mp3", "wb") as audio_file:
|
40 |
+
audio_file.write(response_audio_io.getvalue())
|
41 |
+
|
42 |
+
# Return the response text and the path to the saved audio file
|
43 |
+
return response_message, "response.mp3"
|
44 |
+
|
45 |
+
except Exception as e:
|
46 |
+
return f"An error occurred: {e}", None
|
47 |
+
|
48 |
+
# Define custom CSS for improved appearance
|
49 |
+
custom_css = """
|
50 |
+
.gradio-container {
|
51 |
+
background-color: #87ceeb; /* Sky blue background */
|
52 |
+
color: #333; /* Dark text color for contrast */
|
53 |
+
font-family: Arial, sans-serif; /* Better font */
|
54 |
+
text-align: center; /* Center align text */
|
55 |
+
}
|
56 |
+
|
57 |
+
.gradio-title {
|
58 |
+
color: #333; /* Title color */
|
59 |
+
font-size: 36px; /* Large font size */
|
60 |
+
font-weight: bold; /* Bold text */
|
61 |
+
margin-bottom: 20px; /* Space below the title */
|
62 |
+
}
|
63 |
+
|
64 |
+
.gradio-input, .gradio-output {
|
65 |
+
background-color: #e0f7fa; /* Slightly lighter background for inputs/outputs */
|
66 |
+
color: #333; /* Text color for inputs/outputs */
|
67 |
+
border: 1px solid #80deea; /* Border color */
|
68 |
+
border-radius: 5px; /* Rounded corners */
|
69 |
+
padding: 10px; /* Padding */
|
70 |
+
}
|
71 |
+
|
72 |
+
.gradio-button {
|
73 |
+
background-color: #4fc3f7; /* Button background color */
|
74 |
+
color: white; /* Button text color */
|
75 |
+
border: none; /* Remove default border */
|
76 |
+
border-radius: 5px; /* Rounded corners */
|
77 |
+
padding: 10px 20px; /* Padding */
|
78 |
+
cursor: pointer; /* Pointer cursor on hover */
|
79 |
+
}
|
80 |
+
|
81 |
+
.gradio-button:hover {
|
82 |
+
background-color: #29b6f6; /* Button hover color */
|
83 |
+
}
|
84 |
+
"""
|
85 |
+
|
86 |
+
# Define the Gradio interface with custom CSS
|
87 |
+
iface = gr.Interface(
|
88 |
+
fn=process_audio,
|
89 |
+
inputs=gr.Audio(type="filepath"), # Use type="filepath"
|
90 |
+
outputs=[gr.Textbox(label="Response Text"), gr.Audio(label="Response Audio")],
|
91 |
+
live=True,
|
92 |
+
title="Audio to Audio ChatBot", # Add title here
|
93 |
+
description="Convert audio input to text, generate a response, and convert it back to audio.",
|
94 |
+
css=custom_css # Apply custom CSS
|
95 |
+
)
|
96 |
+
|
97 |
+
iface.launch()
|