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