Spaces:
Running
Running
"""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() |