import gradio as gr from lunacode import smart_luna_answer, text_to_speech_luna def respond(message, history, system_message, max_tokens): try: result = smart_luna_answer(message, max_tokens=max_tokens) return result except Exception as e: return f"Error: {str(e)}" 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 "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 # Voice tab voice_interface = gr.Interface( fn=voice_chat, inputs=[ gr.Audio(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", description="Speak to Luna and hear her response!", ) # Text chat chat_interface = gr.ChatInterface( fn=respond, additional_inputs=[ gr.Textbox(value="You are a helpful AI assistant.", label="System message"), gr.Slider(minimum=128, maximum=2048, value=512, step=1, label="Max new tokens"), ], title="Luna AI — Code & Web Enhanced Assistant", description="Ask Luna questions, generate code, and explore smart AI-powered answers with web context!", ) # Tabs demo = gr.TabbedInterface( [chat_interface, voice_interface], ["Chat with Luna", "Voice with Luna"] ) if __name__ == "__main__": demo.launch()