Update space
Browse files
app.py
CHANGED
@@ -2,11 +2,11 @@ import gradio as gr
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import requests
|
4 |
from bs4 import BeautifulSoup
|
|
|
5 |
|
6 |
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
7 |
|
8 |
-
# Global
|
9 |
-
data = []
|
10 |
|
11 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
12 |
messages = [{"role": "system", "content": system_message}]
|
@@ -40,23 +40,21 @@ def extract_table(url):
|
|
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 row
|
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 with
|
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 |
-
<td>
|
58 |
-
<button onclick="gradioApp().submit('{row['Index']}', 'prepare_button')">Prepare</button>
|
59 |
-
</td>
|
60 |
</tr>
|
61 |
"""
|
62 |
html_table = f"""
|
@@ -68,29 +66,33 @@ def extract_table(url):
|
|
68 |
</tr>
|
69 |
{html_rows}
|
70 |
</table>
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
"""
|
72 |
return html_table
|
73 |
except Exception as e:
|
74 |
-
return f"<p>Error: {str(e)}</p>"
|
75 |
|
76 |
|
77 |
def handle_prepare(index):
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
82 |
|
83 |
-
# Gradio
|
84 |
with gr.Blocks() as demo:
|
85 |
with gr.Row():
|
86 |
with gr.Column(scale=1):
|
87 |
-
url_input = gr.Textbox(
|
88 |
-
value="https://id2223kth.github.io/schedule/",
|
89 |
-
label="Table URL",
|
90 |
-
)
|
91 |
table_output = gr.HTML(label="Extracted Table")
|
92 |
extract_btn = gr.Button("Extract Table")
|
93 |
-
|
94 |
extract_btn.click(fn=extract_table, inputs=[url_input], outputs=[table_output])
|
95 |
|
96 |
with gr.Column(scale=2):
|
@@ -100,15 +102,10 @@ with gr.Blocks() as demo:
|
|
100 |
gr.Textbox(value="Student class preparation companion.", label="System message"),
|
101 |
],
|
102 |
)
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
prepare_button.change(
|
108 |
-
fn=handle_prepare,
|
109 |
-
inputs=[prepare_button],
|
110 |
-
outputs=chatbot,
|
111 |
-
)
|
112 |
|
113 |
if __name__ == "__main__":
|
114 |
demo.launch()
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import requests
|
4 |
from bs4 import BeautifulSoup
|
5 |
+
import pandas as pd
|
6 |
|
7 |
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
8 |
|
9 |
+
data = [] # Global variable to store table data
|
|
|
10 |
|
11 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
12 |
messages = [{"role": "system", "content": system_message}]
|
|
|
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 row
|
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 with Prepare 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 |
+
<td><button onclick="prepareTopic({row['Index']})">Prepare</button></td>
|
|
|
|
|
58 |
</tr>
|
59 |
"""
|
60 |
html_table = f"""
|
|
|
66 |
</tr>
|
67 |
{html_rows}
|
68 |
</table>
|
69 |
+
<script>
|
70 |
+
function prepareTopic(index) {{
|
71 |
+
document.getElementById('prepareInput').value = index;
|
72 |
+
document.getElementById('prepareSubmit').click();
|
73 |
+
}}
|
74 |
+
</script>
|
75 |
"""
|
76 |
return html_table
|
77 |
except Exception as e:
|
78 |
+
return f"<p>Error: {str(e)}</p>"
|
79 |
|
80 |
|
81 |
def handle_prepare(index):
|
82 |
+
try:
|
83 |
+
index = int(index)
|
84 |
+
topic = data[index]["Topic"]
|
85 |
+
return f"Prepare a 10-minute reading on what I should know before the class for the topic: {topic}"
|
86 |
+
except Exception as e:
|
87 |
+
return f"<p>Error: {str(e)}</p>"
|
88 |
|
89 |
+
# Gradio app
|
90 |
with gr.Blocks() as demo:
|
91 |
with gr.Row():
|
92 |
with gr.Column(scale=1):
|
93 |
+
url_input = gr.Textbox(value="https://id2223kth.github.io/schedule/", label="Table URL")
|
|
|
|
|
|
|
94 |
table_output = gr.HTML(label="Extracted Table")
|
95 |
extract_btn = gr.Button("Extract Table")
|
|
|
96 |
extract_btn.click(fn=extract_table, inputs=[url_input], outputs=[table_output])
|
97 |
|
98 |
with gr.Column(scale=2):
|
|
|
102 |
gr.Textbox(value="Student class preparation companion.", label="System message"),
|
103 |
],
|
104 |
)
|
105 |
+
# Hidden input to handle prepare actions
|
106 |
+
prepare_input = gr.Textbox(visible=False, elem_id="prepareInput")
|
107 |
+
prepare_submit = gr.Button("Prepare Submit", visible=False, elem_id="prepareSubmit")
|
108 |
+
prepare_submit.click(fn=handle_prepare, inputs=[prepare_input], outputs=[chatbot])
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
if __name__ == "__main__":
|
111 |
demo.launch()
|