File size: 2,255 Bytes
5cc1949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Script to run the Gradio chatbot application"""

import sys
import os
from dotenv import load_dotenv

# Load environment variables from .env file if available
load_dotenv()

# Ensure the correct path is added to the system to avoid import issues
sys.path.append(os.path.abspath(os.path.dirname(__file__)))

import gradio as gr
from genai_voice.bots.chatbot import ChatBot
from genai_voice.logger.log_utils import log, LogLevels

# Ensure API key is set
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
    raise ValueError("Missing OpenAI API key. Please set the environment variable OPENAI_API_KEY.")

# Function to run the chatbot in microphone mode
def run():
    """Run Chatbot app"""
    chatbot = ChatBot(enable_speakers=True, threaded=True)
    history = []

    def get_response(audio):
        """Get audio response from Chatbot"""
        if not audio:
            raise ValueError("No audio file provided.")
        prompt = chatbot.get_prompt_from_gradio_audio(audio)
        log(f"Transcribed prompt: {prompt}", log_level=LogLevels.ON)
        response = chatbot.respond(prompt, history)
        history.append([prompt, response])
        return response

    demo = gr.Interface(
        fn=get_response,
        inputs=gr.Audio(sources="microphone"),
        outputs="text",
        title="Wanderwise Travel Assistant",
    )
    demo.launch(share=True)


# Function to run chatbot with file support
def run_with_file_support():
    """Run Chatbot app and save files to disk"""
    chatbot = ChatBot(enable_speakers=True, threaded=True)
    history = []

    def get_response_from_file(file):
        """Process file input and generate response"""
        prompt = chatbot.get_prompt_from_file(file)
        response = chatbot.respond(prompt, history)
        history.append([prompt, response])
        return response

    demo = gr.Interface(
        fn=get_response_from_file,
        inputs=gr.Audio(sources="microphone", type="filepath"),
        outputs="text",
    )
    demo.launch(share=True)


# Entry point to run the chatbot
if __name__ == "__main__":
    mode = os.getenv("RUN_MODE", "mic")  # Set RUN_MODE=microphone or file in environment
    if mode == "file":
        run_with_file_support()
    else:
        run()