8_Final / app.py
NAVEENKUMAR1925's picture
Upload 7 files
e18a571 verified
import gradio as gr
import os
import json
from chatbotmem import ai_chatbot
from voice_utils import speech_to_text, text_to_speech
# -----------------------------
# Chatbot Function (Text + Voice)
# -----------------------------
def voice_enabled_chat(user_input, audio_input, history):
# If voice input exists, transcribe
if audio_input is not None:
user_input = speech_to_text(audio_input)
# Get chatbot reply
ai_reply = ai_chatbot(user_input, history)
# Generate voice output
audio_output = text_to_speech(ai_reply)
# Append to chat history in "messages" format
history.append({"role": "user", "content": user_input})
history.append({"role": "assistant", "content": ai_reply})
return history, ai_reply, audio_output
# -----------------------------
# Branding
# -----------------------------
branding_path = os.path.join(os.path.dirname(__file__), 'branding.json')
with open(os.path.abspath(branding_path), "r") as f:
brand_info = json.load(f)["brand"]
# -----------------------------
# Gradio UI
# -----------------------------
with gr.Blocks(title=brand_info["organizationName"]) as demo:
gr.HTML(f'''
<div style="display: flex; justify-content: center; margin-bottom: 20px;">
<img src="{brand_info["logo"]["title"]}"
alt="{brand_info["organizationName"]} Logo"
style="height: 100px;">
</div>
''')
chatbot = gr.Chatbot(type="messages")
with gr.Row():
txt = gr.Textbox(label="Type your message")
mic = gr.Audio(sources=["microphone"], type="filepath", label="🎀 Speak")
output_text = gr.Textbox(label="SIST AI Reply")
output_audio = gr.Audio(label="SIST AI Voice", type="filepath")
submit_btn = gr.Button("Send")
def chat_wrapper(user_text, mic_audio, chat_history):
return voice_enabled_chat(user_text, mic_audio, chat_history)
submit_btn.click(
chat_wrapper,
inputs=[txt, mic, chatbot],
outputs=[chatbot, output_text, output_audio]
)
demo.launch()