Update space
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
|
| 8 |
# Global data store for the table
|
| 9 |
data = []
|
| 10 |
|
| 11 |
-
def respond(message, history, system_message, max_tokens
|
| 12 |
messages = [{"role": "system", "content": system_message}]
|
| 13 |
for val in history:
|
| 14 |
if val[0]:
|
|
@@ -28,6 +28,7 @@ def respond(message, history, system_message, max_tokens=2048, temperature=0.7,
|
|
| 28 |
response += token
|
| 29 |
yield response
|
| 30 |
|
|
|
|
| 31 |
def extract_table(url):
|
| 32 |
global data
|
| 33 |
try:
|
|
@@ -39,69 +40,48 @@ def extract_table(url):
|
|
| 39 |
return "<p>No table found on page</p>"
|
| 40 |
|
| 41 |
# Extract data
|
| 42 |
-
data
|
| 43 |
rows = table.find_all("tr")
|
| 44 |
for i, row in enumerate(rows[1:]): # Skip header
|
| 45 |
cells = row.find_all("td")
|
| 46 |
if len(cells) >= 2:
|
| 47 |
-
data.append({
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
})
|
| 52 |
-
|
| 53 |
-
# Generate HTML table
|
| 54 |
-
table_html = "<table border='1'><tr><th>Date</th><th>Topic</th></tr>"
|
| 55 |
for row in data:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
-
return f"<p>Error: {str(e)}</p>"
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
return
|
| 74 |
-
|
| 75 |
-
def clear_chat():
|
| 76 |
-
return [], []
|
| 77 |
-
|
| 78 |
-
def create_table_with_buttons():
|
| 79 |
-
with gr.Column() as container:
|
| 80 |
-
# Create the table display
|
| 81 |
-
table_html = "<table border='1'><tr><th>Date</th><th>Topic</th></tr>"
|
| 82 |
-
for row in data:
|
| 83 |
-
table_html += f"<tr><td>{row['Date']}</td><td>{row['Topic']}</td></tr>"
|
| 84 |
-
table_html += "</table>"
|
| 85 |
-
gr.HTML(table_html)
|
| 86 |
-
|
| 87 |
-
# Create buttons below the table
|
| 88 |
-
with gr.Row():
|
| 89 |
-
for i, row in enumerate(data):
|
| 90 |
-
btn = gr.Button(f"Prepare: {row['Topic']}")
|
| 91 |
-
btn.click(
|
| 92 |
-
fn=handle_prepare,
|
| 93 |
-
inputs=[gr.Number(value=i, visible=False), chatbot],
|
| 94 |
-
outputs=[chatbot]
|
| 95 |
-
).then(
|
| 96 |
-
fn=respond,
|
| 97 |
-
inputs=[
|
| 98 |
-
gr.Textbox(value=f"Prepare a 10-minute reading for: {row['Topic']}", visible=False),
|
| 99 |
-
chatbot,
|
| 100 |
-
system_message
|
| 101 |
-
],
|
| 102 |
-
outputs=[chatbot]
|
| 103 |
-
)
|
| 104 |
-
return container
|
| 105 |
|
| 106 |
# Gradio App
|
| 107 |
with gr.Blocks() as demo:
|
|
@@ -111,42 +91,27 @@ with gr.Blocks() as demo:
|
|
| 111 |
value="https://id2223kth.github.io/schedule/",
|
| 112 |
label="Table URL",
|
| 113 |
)
|
| 114 |
-
|
| 115 |
extract_btn = gr.Button("Extract Table")
|
| 116 |
|
|
|
|
|
|
|
| 117 |
with gr.Column(scale=2):
|
| 118 |
-
chatbot = gr.
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
).then(
|
| 134 |
-
fn=create_table_with_buttons,
|
| 135 |
-
inputs=None,
|
| 136 |
-
outputs=table_container
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
submit.click(
|
| 140 |
-
fn=chat,
|
| 141 |
-
inputs=[msg, chatbot, system_message],
|
| 142 |
-
outputs=[msg, chatbot]
|
| 143 |
-
).then(
|
| 144 |
-
fn=respond,
|
| 145 |
-
inputs=[msg, chatbot, system_message],
|
| 146 |
-
outputs=[chatbot]
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
clear.click(fn=clear_chat, outputs=[chatbot, msg])
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
| 152 |
-
demo.launch()
|
|
|
|
| 8 |
# Global data store for the table
|
| 9 |
data = []
|
| 10 |
|
| 11 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 12 |
messages = [{"role": "system", "content": system_message}]
|
| 13 |
for val in history:
|
| 14 |
if val[0]:
|
|
|
|
| 28 |
response += token
|
| 29 |
yield response
|
| 30 |
|
| 31 |
+
|
| 32 |
def extract_table(url):
|
| 33 |
global data
|
| 34 |
try:
|
|
|
|
| 40 |
return "<p>No table found on page</p>"
|
| 41 |
|
| 42 |
# Extract data
|
| 43 |
+
data = []
|
| 44 |
rows = table.find_all("tr")
|
| 45 |
for i, row in enumerate(rows[1:]): # Skip header
|
| 46 |
cells = row.find_all("td")
|
| 47 |
if len(cells) >= 2:
|
| 48 |
+
data.append({"Index": i, "Date": cells[0].text.strip()[:10], "Topic": cells[1].text.strip()})
|
| 49 |
+
|
| 50 |
+
# Generate HTML table without buttons
|
| 51 |
+
html_rows = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
for row in data:
|
| 53 |
+
html_rows += f"""
|
| 54 |
+
<tr>
|
| 55 |
+
<td>{row['Date']}</td>
|
| 56 |
+
<td>{row['Topic']}</td>
|
| 57 |
+
</tr>
|
| 58 |
+
"""
|
| 59 |
+
html_table = f"""
|
| 60 |
+
<table border="1">
|
| 61 |
+
<tr>
|
| 62 |
+
<th>Date</th>
|
| 63 |
+
<th>Topic</th>
|
| 64 |
+
</tr>
|
| 65 |
+
{html_rows}
|
| 66 |
+
</table>
|
| 67 |
+
"""
|
| 68 |
+
return html_table
|
| 69 |
except Exception as e:
|
| 70 |
+
return f"<p>Error: {str(e)}</p>"
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def handle_prepare(index):
|
| 74 |
+
topic = data[index]["Topic"]
|
| 75 |
+
return f"Prepare a 10-minute reading on what I should know before the class for the topic: {topic}"
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def create_buttons():
|
| 79 |
+
# Dynamically create buttons for each topic
|
| 80 |
+
button_list = []
|
| 81 |
+
for i, row in enumerate(data):
|
| 82 |
+
button_list.append(gr.Button(f"Prepare: {row['Topic']}").style(full_width=False))
|
| 83 |
+
return button_list
|
| 84 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# Gradio App
|
| 87 |
with gr.Blocks() as demo:
|
|
|
|
| 91 |
value="https://id2223kth.github.io/schedule/",
|
| 92 |
label="Table URL",
|
| 93 |
)
|
| 94 |
+
table_output = gr.HTML(label="Extracted Table")
|
| 95 |
extract_btn = gr.Button("Extract Table")
|
| 96 |
|
| 97 |
+
extract_btn.click(fn=extract_table, inputs=[url_input], outputs=[table_output])
|
| 98 |
+
|
| 99 |
with gr.Column(scale=2):
|
| 100 |
+
chatbot = gr.ChatInterface(
|
| 101 |
+
respond,
|
| 102 |
+
additional_inputs=[
|
| 103 |
+
gr.Textbox(value="Student class preparation companion.", label="System message"),
|
| 104 |
+
],
|
| 105 |
)
|
| 106 |
|
| 107 |
+
buttons_container = gr.Column(visible=True)
|
| 108 |
+
|
| 109 |
+
# Dynamically add buttons after table extraction
|
| 110 |
+
def update_buttons():
|
| 111 |
+
buttons = create_buttons()
|
| 112 |
+
return gr.update(components=buttons)
|
| 113 |
+
|
| 114 |
+
extract_btn.click(fn=update_buttons, inputs=[], outputs=[buttons_container])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
demo.launch()
|