Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import whisper
|
3 |
+
import gradio as gr
|
4 |
+
from groq import Groq
|
5 |
+
from gtts import gTTS
|
6 |
+
|
7 |
+
# β
Set API Key (Replace with your actual key)
|
8 |
+
os.environ["GROQ_API_KEY"] = "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"
|
9 |
+
|
10 |
+
# β
Load Whisper Model securely
|
11 |
+
model = whisper.load_model("small")
|
12 |
+
|
13 |
+
def speech_to_text(audio_path):
|
14 |
+
"""Convert speech from audio file to text using Whisper."""
|
15 |
+
result = model.transcribe(audio_path)
|
16 |
+
return result["text"]
|
17 |
+
|
18 |
+
def generate_text(prompt):
|
19 |
+
"""Generate AI response using Groq API."""
|
20 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
21 |
+
|
22 |
+
chat_completion = client.chat.completions.create(
|
23 |
+
messages=[{"role": "user", "content": prompt}],
|
24 |
+
model="llama-3.3-70b-versatile",
|
25 |
+
)
|
26 |
+
|
27 |
+
return chat_completion.choices[0].message.content
|
28 |
+
|
29 |
+
def text_to_speech(text):
|
30 |
+
"""Convert text response to speech using Google TTS."""
|
31 |
+
tts = gTTS(text=text, lang="en")
|
32 |
+
output_path = "output.mp3"
|
33 |
+
tts.save(output_path)
|
34 |
+
return output_path # Return file path for Gradio
|
35 |
+
|
36 |
+
def chat_process(history, text_input, audio_path):
|
37 |
+
"""Main pipeline: Handles both Text and Speech inputs."""
|
38 |
+
|
39 |
+
# Check if user provided text or voice input
|
40 |
+
if audio_path:
|
41 |
+
user_input = speech_to_text(audio_path) # Convert audio to text
|
42 |
+
elif text_input:
|
43 |
+
user_input = text_input # Use text input
|
44 |
+
else:
|
45 |
+
return history, None # No input received
|
46 |
+
|
47 |
+
# Generate response
|
48 |
+
response_text = generate_text(user_input)
|
49 |
+
|
50 |
+
# Convert response to speech
|
51 |
+
tts_output = text_to_speech(response_text)
|
52 |
+
|
53 |
+
# Append conversation history
|
54 |
+
history.append({"role": "user", "content": user_input})
|
55 |
+
history.append({"role": "assistant", "content": response_text})
|
56 |
+
|
57 |
+
return history, tts_output
|
58 |
+
|
59 |
+
def reset_chat():
|
60 |
+
return []
|
61 |
+
|
62 |
+
# β
Updated Gradio Chat UI
|
63 |
+
with gr.Blocks() as chat_ui:
|
64 |
+
gr.Markdown("## πποΈ @HilalSeek AI Assistant")
|
65 |
+
|
66 |
+
chatbot = gr.Chatbot(label="Conversation", type="messages") # Chat display
|
67 |
+
with gr.Row():
|
68 |
+
text_input = gr.Textbox(placeholder="Type your message here...", interactive=True)
|
69 |
+
audio_input = gr.Audio(type="filepath", format="wav") # β
Fixed Mic input
|
70 |
+
submit_button = gr.Button("Submit")
|
71 |
+
|
72 |
+
clear_button = gr.Button("Clear Chat")
|
73 |
+
|
74 |
+
submit_button.click(chat_process, [chatbot, text_input, audio_input], [chatbot, gr.Audio(type="filepath")])
|
75 |
+
clear_button.click(reset_chat, [], chatbot)
|
76 |
+
|
77 |
+
# β
Launch Gradio App
|
78 |
+
chat_ui.launch(share=True)
|