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'''
{brand_info[
''') 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()