Luna / app.py
cosmosai471's picture
Update app.py
8536ecc verified
raw
history blame
1.83 kB
import gradio as gr
from lunacode import smart_luna_answer, text_to_speech_luna
# TEXT CHAT
def text_chat(user_input, max_tokens, system_message, history):
response = smart_luna_answer(user_input, max_tokens=max_tokens)
history.append((user_input, response))
return history, history
# VOICE CHAT
def voice_chat(audio, max_tokens):
import speech_recognition as sr
recognizer = sr.Recognizer()
with sr.AudioFile(audio) as source:
audio_data = recognizer.record(source)
try:
text = recognizer.recognize_google(audio_data)
except:
return "Sorry, could not understand audio", None
response_text = smart_luna_answer(text, max_tokens=max_tokens)
audio_path = text_to_speech_luna(response_text)
return response_text, audio_path
# INTERFACE 1: CHAT
chat_interface = gr.Interface(
fn=text_chat,
inputs=[
gr.Textbox(label="Your Message"),
gr.Slider(minimum=128, maximum=2048, value=512, step=1, label="Max new tokens"),
gr.Textbox(value="You are a helpful AI assistant.", label="System message"),
gr.State([]) # chat history
],
outputs=[
gr.Chatbot(label="Luna Chat"),
gr.State([])
],
title="Luna Chat Assistant"
)
# INTERFACE 2: VOICE
voice_interface = gr.Interface(
fn=voice_chat,
inputs=[
gr.Audio(source="microphone", type="filepath", label="Speak to Luna"),
gr.Slider(minimum=128, maximum=2048, value=512, step=1, label="Max new tokens")
],
outputs=[
gr.Textbox(label="Luna's Response"),
gr.Audio(label="Luna Speaks")
],
title="Luna Voice Assistant"
)
# TABS
demo = gr.TabbedInterface(
[chat_interface, voice_interface],
["Chat with Luna", "Voice with Luna"]
)
if __name__ == "__main__":
demo.launch()