Spaces:
Sleeping
Sleeping
Create groq-gradio.py
Browse files- groq-gradio.py +120 -0
groq-gradio.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import groq
|
3 |
+
import io
|
4 |
+
import numpy as np
|
5 |
+
import soundfile as sf
|
6 |
+
|
7 |
+
def transcribe_audio(audio, api_key):
|
8 |
+
if audio is None:
|
9 |
+
return ""
|
10 |
+
|
11 |
+
client = groq.Client(api_key=api_key)
|
12 |
+
|
13 |
+
# Convert audio to the format expected by the model
|
14 |
+
# The model supports mp3, mp4, mpeg, mpga, m4a, wav, and webm file types
|
15 |
+
audio_data = audio[1] # Get the numpy array from the tuple
|
16 |
+
buffer = io.BytesIO()
|
17 |
+
sf.write(buffer, audio_data, audio[0], format='wav')
|
18 |
+
buffer.seek(0)
|
19 |
+
|
20 |
+
bytes_audio = io.BytesIO()
|
21 |
+
np.save(bytes_audio, audio_data)
|
22 |
+
bytes_audio.seek(0)
|
23 |
+
|
24 |
+
try:
|
25 |
+
# Use Distil-Whisper English powered by Groq for transcription
|
26 |
+
completion = client.audio.transcriptions.create(
|
27 |
+
model="distil-whisper-large-v3-en",
|
28 |
+
file=("audio.wav", buffer),
|
29 |
+
response_format="text"
|
30 |
+
)
|
31 |
+
return completion
|
32 |
+
except Exception as e:
|
33 |
+
return f"Error in transcription: {str(e)}"
|
34 |
+
|
35 |
+
def generate_response(transcription, api_key):
|
36 |
+
if not transcription:
|
37 |
+
return "No transcription available. Please try speaking again."
|
38 |
+
|
39 |
+
client = groq.Client(api_key=api_key)
|
40 |
+
|
41 |
+
try:
|
42 |
+
# Use Llama 3.1 70B powered by Groq for text generation
|
43 |
+
completion = client.chat.completions.create(
|
44 |
+
model="llama-3.1-70b-versatile",
|
45 |
+
messages=[
|
46 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
47 |
+
{"role": "user", "content": transcription}
|
48 |
+
],
|
49 |
+
max_tokens=150,
|
50 |
+
temperature=0.7
|
51 |
+
)
|
52 |
+
return completion.choices[0].message.content
|
53 |
+
except Exception as e:
|
54 |
+
return f"Error in response generation: {str(e)}"
|
55 |
+
|
56 |
+
def process_audio(audio, api_key):
|
57 |
+
if not api_key:
|
58 |
+
return "Please enter your Groq API key.", "API key is required."
|
59 |
+
transcription = transcribe_audio(audio, api_key)
|
60 |
+
response = generate_response(transcription, api_key)
|
61 |
+
return transcription, response
|
62 |
+
|
63 |
+
# Custom CSS for the Groq badge and color scheme (feel free to edit however you wish)
|
64 |
+
custom_css = """
|
65 |
+
.gradio-container {
|
66 |
+
background-color: #f5f5f5;
|
67 |
+
}
|
68 |
+
.gr-button-primary {
|
69 |
+
background-color: #f55036 !important;
|
70 |
+
border-color: #f55036 !important;
|
71 |
+
}
|
72 |
+
.gr-button-secondary {
|
73 |
+
color: #f55036 !important;
|
74 |
+
border-color: #f55036 !important;
|
75 |
+
}
|
76 |
+
#groq-badge {
|
77 |
+
position: fixed;
|
78 |
+
bottom: 20px;
|
79 |
+
right: 20px;
|
80 |
+
z-index: 1000;
|
81 |
+
}
|
82 |
+
"""
|
83 |
+
|
84 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
85 |
+
gr.Markdown("# 🎙️ Groq x Gradio Voice-Powered AI Assistant")
|
86 |
+
|
87 |
+
api_key_input = gr.Textbox(type="password", label="Enter your Groq API Key")
|
88 |
+
|
89 |
+
with gr.Row():
|
90 |
+
audio_input = gr.Audio(label="Speak!", type="numpy")
|
91 |
+
|
92 |
+
with gr.Row():
|
93 |
+
transcription_output = gr.Textbox(label="Transcription")
|
94 |
+
response_output = gr.Textbox(label="AI Assistant Response")
|
95 |
+
|
96 |
+
submit_button = gr.Button("Process", variant="primary")
|
97 |
+
|
98 |
+
# Add the Groq badge
|
99 |
+
gr.HTML("""
|
100 |
+
<div id="groq-badge">
|
101 |
+
<div style="color: #f55036; font-weight: bold;">POWERED BY GROQ</div>
|
102 |
+
</div>
|
103 |
+
""")
|
104 |
+
|
105 |
+
submit_button.click(
|
106 |
+
process_audio,
|
107 |
+
inputs=[audio_input, api_key_input],
|
108 |
+
outputs=[transcription_output, response_output]
|
109 |
+
)
|
110 |
+
|
111 |
+
gr.Markdown("""
|
112 |
+
## How to use this app:
|
113 |
+
1. Enter your Groq API Key in the provided field.
|
114 |
+
2. Click on the microphone icon and speak your message (or forever hold your peace)! You can also provide a supported audio file. Supported audio files include mp3, mp4, mpeg, mpga, m4a, wav, and webm file types.
|
115 |
+
3. Click the "Process" button to transcribe your speech and generate a response from our AI assistant.
|
116 |
+
4. The transcription and AI assistant response will appear in the respective text boxes.
|
117 |
+
|
118 |
+
""")
|
119 |
+
|
120 |
+
demo.launch()
|