Spaces:
Sleeping
Sleeping
Update app.py
Browse files17 unmodified lines181920212223247 unmodified lines323334353637383940414243444546474849505152535455565758596061626364656667686970713 unmodified lines7576777879808182838485862 unmodified lines899091929394959697989917 unmodified linestts_client = safe_client("E-motionAssistant/Space3")def main_orchestrator(audio_input, text_input, history):# If history is None, initialize itif history is None:history = []7 unmodified linesexcept:emotion = "Neutral"# 2. THE STRICT MESSAGE FIX (This stops the "Data Incompatible" error)# Convert simple history [[u,b]] to strict Gradio 5/6 api_historyapi_history = []for u, b in history:api_history.append({"role": "user","content": [{"type": "text", "text": str(u)}]})api_history.append({"role": "assistant","content": [{"type": "text", "text": str(b)}]})# Prepare the current message as a content block listbundled_text = f"Context: User is {emotion}. Message: {text_input}"current_message = {"role": "user","content": [{"type": "text", "text": bundled_text}]}# 3. CALL LLMtry:# We send the strictly formatted current_message and api_historyresponse = llm_english.predict(message=current_message,history=api_history,api_name="/chat")except Exception as e:# Fallback for LLMs that might still want simple stringstry:response = llm_english.predict(message=bundled_text, history=history, api_name="/chat")except:response = f"LLM Error: {str(e)}"# 4. TTS LOGICaudio_res = None3 unmodified linesexcept:audio_res = None# 5. UPDATE HISTORY# Return as list of lists for the UI Chatbothistory.append([text_input, response])return history, audio_res# --- UI ---with gr.Blocks() as demo:state = gr.State([])with gr.Row():2 unmodified linestext_in = gr.Textbox(label="Message")btn = gr.Button("Send")with gr.Column():# type="messages" creates the dictionary format seen in your docschatbot_ui = gr.Chatbot(label="Therapy History")audio_out = gr.Audio(autoplay=True)btn.click(main_orchestrator, [audio_in, text_in, state], [chatbot_ui, audio_out])# Launch with ssr_mode=False if you see flickeringdemo.launch()17 unmodified lines1819202122237 unmodified lines313233343536373839404142434445464748495051525354553 unmodified lines596061626364656667682 unmodified lines7172737475767778798017 unmodified linestts_client = safe_client("E-motionAssistant/Space3")def main_orchestrator(audio_input, text_input, history):if history is None:history = []7 unmodified linesexcept:emotion = "Neutral"# 2. FORMAT FOR SUB-SPACE (STRICT DICTIONARY)# Even if our UI is old, our sub-space (TherapyEnglish) needs the new formatapi_history = []for u, b in history:api_history.append({"role": "user", "content": [{"type": "text", "text": str(u)}]})api_history.append({"role": "assistant", "content": [{"type": "text", "text": str(b)}]})bundled_text = f"Context: User is {emotion}. Message: {text_input}"current_message = {"role": "user", "content": [{"type": "text", "text": bundled_text}]}# 3. CALL LLMtry:response = llm_english.predict(message=current_message,history=api_history,api_name="/chat")except Exception as e:response = f"LLM Error: {str(e)}"# 4. TTS LOGICaudio_res = None3 unmodified linesexcept:audio_res = None# 5. UPDATE HISTORY (Simple list of lists for old Gradio UI)history.append([text_input, response])return history, audio_reswith gr.Blocks() as demo:state = gr.State([])with gr.Row():2 unmodified linestext_in = gr.Textbox(label="Message")btn = gr.Button("Send")with gr.Column():# REMOVED type="messages" to stop the crashchatbot_ui = gr.Chatbot(label="Therapy History")audio_out = gr.Audio(autoplay=True)btn.click(main_orchestrator, [audio_in, text_in, state], [chatbot_ui, audio_out])demo.launch()
app.py+8-27