File size: 2,143 Bytes
e18a571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()