Update space
Browse files
app.py
CHANGED
|
@@ -65,7 +65,6 @@ def extract_table(url):
|
|
| 65 |
<button onclick='
|
| 66 |
document.getElementById("prepare-topic").value = "{i}";
|
| 67 |
document.getElementById("prepare-topic").dispatchEvent(new Event("input"));
|
| 68 |
-
document.getElementById("prepare-button").click();
|
| 69 |
'>
|
| 70 |
Prepare
|
| 71 |
</button>
|
|
@@ -85,21 +84,39 @@ def generate_response(history, system_message):
|
|
| 85 |
if not history:
|
| 86 |
return history
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
response = ""
|
| 89 |
for chunk in respond(history[-1][0], history[:-1], system_message):
|
| 90 |
response = chunk
|
| 91 |
history[-1] = (history[-1][0], response)
|
| 92 |
yield history
|
| 93 |
|
| 94 |
-
def handle_prepare(index, history):
|
| 95 |
try:
|
| 96 |
index = int(index)
|
| 97 |
if 0 <= index < len(data):
|
| 98 |
topic = data[index]["Topic"]
|
| 99 |
date = data[index]["Date"]
|
| 100 |
message = f"Please prepare a 10-minute reading guide for the topic '{topic}' scheduled for {date}"
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
return history
|
| 104 |
|
| 105 |
def clear_chat():
|
|
@@ -118,7 +135,6 @@ with gr.Blocks() as demo:
|
|
| 118 |
|
| 119 |
# Hidden components for prepare functionality
|
| 120 |
prepare_topic = gr.Textbox(value="", visible=False, elem_id="prepare-topic")
|
| 121 |
-
prepare_button = gr.Button("Prepare", visible=False, elem_id="prepare-button")
|
| 122 |
|
| 123 |
with gr.Column(scale=2):
|
| 124 |
chatbot = gr.Chatbot()
|
|
@@ -139,6 +155,13 @@ with gr.Blocks() as demo:
|
|
| 139 |
outputs=[table_output]
|
| 140 |
)
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
# Submit button handler
|
| 143 |
msg.submit(
|
| 144 |
fn=add_text,
|
|
@@ -166,17 +189,6 @@ with gr.Blocks() as demo:
|
|
| 166 |
outputs=chatbot
|
| 167 |
)
|
| 168 |
|
| 169 |
-
# Prepare button handler
|
| 170 |
-
prepare_button.click(
|
| 171 |
-
fn=handle_prepare,
|
| 172 |
-
inputs=[prepare_topic, chatbot],
|
| 173 |
-
outputs=chatbot
|
| 174 |
-
).then(
|
| 175 |
-
fn=generate_response,
|
| 176 |
-
inputs=[chatbot, system_message],
|
| 177 |
-
outputs=chatbot
|
| 178 |
-
)
|
| 179 |
-
|
| 180 |
# Clear button handler
|
| 181 |
clear.click(fn=clear_chat, outputs=[chatbot, msg])
|
| 182 |
|
|
|
|
| 65 |
<button onclick='
|
| 66 |
document.getElementById("prepare-topic").value = "{i}";
|
| 67 |
document.getElementById("prepare-topic").dispatchEvent(new Event("input"));
|
|
|
|
| 68 |
'>
|
| 69 |
Prepare
|
| 70 |
</button>
|
|
|
|
| 84 |
if not history:
|
| 85 |
return history
|
| 86 |
|
| 87 |
+
# Ensure the last entry is a full user message and not partially processed
|
| 88 |
+
if history[-1][1] is not None:
|
| 89 |
+
return history
|
| 90 |
+
|
| 91 |
response = ""
|
| 92 |
for chunk in respond(history[-1][0], history[:-1], system_message):
|
| 93 |
response = chunk
|
| 94 |
history[-1] = (history[-1][0], response)
|
| 95 |
yield history
|
| 96 |
|
| 97 |
+
def handle_prepare(index, history, system_message):
|
| 98 |
try:
|
| 99 |
index = int(index)
|
| 100 |
if 0 <= index < len(data):
|
| 101 |
topic = data[index]["Topic"]
|
| 102 |
date = data[index]["Date"]
|
| 103 |
message = f"Please prepare a 10-minute reading guide for the topic '{topic}' scheduled for {date}"
|
| 104 |
+
|
| 105 |
+
# Add the prepare message to history
|
| 106 |
+
new_history = history + [(message, None)]
|
| 107 |
+
|
| 108 |
+
# Generate response specifically for this prepare request
|
| 109 |
+
response_generator = respond(message, history, system_message)
|
| 110 |
+
full_response = ""
|
| 111 |
+
for chunk in response_generator:
|
| 112 |
+
full_response = chunk
|
| 113 |
+
|
| 114 |
+
# Update history with the full response
|
| 115 |
+
new_history[-1] = (message, full_response)
|
| 116 |
+
|
| 117 |
+
return new_history
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"Error in handle_prepare: {e}")
|
| 120 |
return history
|
| 121 |
|
| 122 |
def clear_chat():
|
|
|
|
| 135 |
|
| 136 |
# Hidden components for prepare functionality
|
| 137 |
prepare_topic = gr.Textbox(value="", visible=False, elem_id="prepare-topic")
|
|
|
|
| 138 |
|
| 139 |
with gr.Column(scale=2):
|
| 140 |
chatbot = gr.Chatbot()
|
|
|
|
| 155 |
outputs=[table_output]
|
| 156 |
)
|
| 157 |
|
| 158 |
+
# Prepare topic handler
|
| 159 |
+
prepare_topic.change(
|
| 160 |
+
fn=handle_prepare,
|
| 161 |
+
inputs=[prepare_topic, chatbot, system_message],
|
| 162 |
+
outputs=[chatbot]
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
# Submit button handler
|
| 166 |
msg.submit(
|
| 167 |
fn=add_text,
|
|
|
|
| 189 |
outputs=chatbot
|
| 190 |
)
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
# Clear button handler
|
| 193 |
clear.click(fn=clear_chat, outputs=[chatbot, msg])
|
| 194 |
|