Spaces:
Running
Running
File size: 1,829 Bytes
a9aecd6 883f725 8536ecc a9aecd6 8536ecc 883f725 8536ecc 883f725 8536ecc 883f725 8536ecc 883f725 8536ecc 883f725 8536ecc 883f725 a9aecd6 |
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 |
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()
|