Update space
Browse files11 unmodified lines1213141516171857 unmodified lines7677787980818283848586878889909192939495969798991001011022 unmodified lines10510610710810911011111211311411511611711814 unmodified lines13313413513613713813914014114214312 unmodified lines15615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419511 unmodified linesdef respond(message, history, system_message):messages = [{"role": "system", "content": system_message}]for val in history:messages.append({"role": "user", "content": val[0]})if val[1]:messages.append({"role": "assistant", "content": val[1]})57 unmodified linesexcept Exception as e:return f"<p>Error: {str(e)}</p>"def user_message(message, history):if message:history = history + [(message, None)]return historydef bot(history, system_message):if not history or history[-1][1] is not None: # Skip if no history or last message already has a responsereturn historyhistory_with_response = history.copy()response = ""try:for chunk in respond(history[-1][0], history[:-1], system_message):response = chunkhistory_with_response[-1] = (history[-1][0], response)yield history_with_responseexcept Exception as e:print(f"Error in bot: {e}")history_with_response[-1] = (history[-1][0], "I apologize, but I encountered an error processing your request.")yield history_with_responsedef handle_prepare(index, history):try:2 unmodified linestopic = data[index]["Topic"]date = data[index]["Date"]message = f"Please prepare a 10-minute reading guide for the topic '{topic}' scheduled for {date}"history = history or []history = history + [(message, None)]print(f"Prepare button clicked: {message}")return historyexcept Exception as e:print(f"Error in handle_prepare: {e}")return history or []def clear_chat():return [], ""14 unmodified linesprepare_button = gr.Button("Prepare", visible=False, elem_id="prepare-button")with gr.Column(scale=2):chatbot = gr.Chatbot(height=600,bubble_full_width=False,)msg = gr.Textbox(label="Message", interactive=True)system_message = gr.Textbox(value="Student class preparation companion.",label="System message"12 unmodified lines# Submit button handlermsg.submit(fn=user_message,inputs=[msg, chatbot],outputs=chatbot,).then(fn=lambda: "",outputs=msg,).then(fn=bot,inputs=[chatbot, system_message],outputs=chatbot,)submit.click(fn=user_message,inputs=[msg, chatbot],outputs=chatbot,).then(fn=lambda: "",outputs=msg,).then(fn=bot,inputs=[chatbot, system_message],outputs=chatbot,)# Prepare button handlerprepare_button.click(fn=handle_prepare,inputs=[prepare_topic, chatbot],outputs=chatbot,).then(fn=bot,inputs=[chatbot, system_message],outputs=chatbot,)# Clear button handler11 unmodified lines121314151617181957 unmodified lines777879808182838485868788899091929394952 unmodified lines989910010110210310410510614 unmodified lines12112212312412512612712812 unmodified lines14114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918011 unmodified linesdef respond(message, history, system_message):messages = [{"role": "system", "content": system_message}]for val in history:if val[0]:messages.append({"role": "user", "content": val[0]})if val[1]:messages.append({"role": "assistant", "content": val[1]})57 unmodified linesexcept Exception as e:return f"<p>Error: {str(e)}</p>"def add_text(history, text):history = history + [(text, None)]return historydef generate_response(history, system_message):if not history:return historyresponse = ""for chunk in respond(history[-1][0], history[:-1], system_message):response = chunkhistory[-1] = (history[-1][0], response)yield historydef handle_prepare(index, history):try:2 unmodified linestopic = data[index]["Topic"]date = data[index]["Date"]message = f"Please prepare a 10-minute reading guide for the topic '{topic}' scheduled for {date}"return history + [(message, None)]except:return historydef clear_chat():return [], ""14 unmodified linesprepare_button = gr.Button("Prepare", visible=False, elem_id="prepare-button")with gr.Column(scale=2):chatbot = gr.Chatbot()msg = gr.Textbox(label="Message")system_message = gr.Textbox(value="Student class preparation companion.",label="System message"12 unmodified lines# Submit button handlermsg.submit(fn=add_text,inputs=[chatbot, msg],outputs=chatbot).success(fn=lambda: "",outputs=msg).then(fn=generate_response,inputs=[chatbot, system_message],outputs=chatbot)submit.click(fn=add_text,inputs=[chatbot, msg],outputs=chatbot).success(fn=lambda: "",outputs=msg).then(fn=generate_response,inputs=[chatbot, system_message],outputs=chatbot)# Prepare button handlerprepare_button.click(fn=handle_prepare,inputs=[prepare_topic, chatbot],outputs=chatbot).then(fn=generate_response,inputs=[chatbot, system_message],outputs=chatbot)# Clear button handler
app.py+32-47