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, temperature, top_p):
|
12 |
messages = [{"role": "system", "content": system_message}]
|
13 |
for val in history:
|
14 |
if val[0]:
|
@@ -28,7 +28,6 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
28 |
response += token
|
29 |
yield response
|
30 |
|
31 |
-
|
32 |
def extract_table(url):
|
33 |
global data
|
34 |
try:
|
@@ -40,14 +39,18 @@ 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
|
46 |
cells = row.find_all("td")
|
47 |
if len(cells) >= 2:
|
48 |
-
data.append({
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
# Generate HTML table
|
51 |
html_rows = ""
|
52 |
for row in data:
|
53 |
html_rows += f"""
|
@@ -67,21 +70,15 @@ def extract_table(url):
|
|
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:
|
@@ -94,24 +91,60 @@ with gr.Blocks() as demo:
|
|
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.
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
)
|
106 |
|
107 |
-
|
|
|
|
|
108 |
|
109 |
-
#
|
110 |
-
|
111 |
-
buttons = create_buttons()
|
112 |
-
return gr.update(components=buttons)
|
113 |
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
if __name__ == "__main__":
|
117 |
-
demo.launch()
|
|
|
8 |
# Global data store for the table
|
9 |
data = []
|
10 |
|
11 |
+
def respond(message, history, system_message, max_tokens=2048, temperature=0.7, top_p=0.9):
|
12 |
messages = [{"role": "system", "content": system_message}]
|
13 |
for val in history:
|
14 |
if val[0]:
|
|
|
28 |
response += token
|
29 |
yield response
|
30 |
|
|
|
31 |
def extract_table(url):
|
32 |
global data
|
33 |
try:
|
|
|
39 |
return "<p>No table found on page</p>"
|
40 |
|
41 |
# Extract data
|
42 |
+
data.clear() # Clear existing 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 |
+
"Index": i,
|
49 |
+
"Date": cells[0].text.strip()[:10],
|
50 |
+
"Topic": cells[1].text.strip()
|
51 |
+
})
|
52 |
|
53 |
+
# Generate HTML table
|
54 |
html_rows = ""
|
55 |
for row in data:
|
56 |
html_rows += f"""
|
|
|
70 |
"""
|
71 |
return html_table
|
72 |
except Exception as e:
|
73 |
+
return f"<p>Error: {str(e)}</p>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
def handle_prepare(index, chatbot):
|
76 |
+
if 0 <= index < len(data):
|
77 |
+
topic = data[index]["Topic"]
|
78 |
+
prompt = f"Prepare a 10-minute reading on what I should know before the class for the topic: {topic}"
|
79 |
+
history = chatbot.value if chatbot.value else []
|
80 |
+
history.append((prompt, None))
|
81 |
+
return history
|
82 |
|
83 |
# Gradio App
|
84 |
with gr.Blocks() as demo:
|
|
|
91 |
table_output = gr.HTML(label="Extracted Table")
|
92 |
extract_btn = gr.Button("Extract Table")
|
93 |
|
|
|
|
|
94 |
with gr.Column(scale=2):
|
95 |
+
chatbot = gr.Chatbot()
|
96 |
+
msg = gr.Textbox(label="Message")
|
97 |
+
system_message = gr.Textbox(
|
98 |
+
value="Student class preparation companion.",
|
99 |
+
label="System message"
|
100 |
)
|
101 |
|
102 |
+
with gr.Row():
|
103 |
+
submit = gr.Button("Submit")
|
104 |
+
clear = gr.Button("Clear")
|
105 |
|
106 |
+
# Container for dynamic buttons
|
107 |
+
buttons_container = gr.Column()
|
|
|
|
|
108 |
|
109 |
+
def clear_chat():
|
110 |
+
return [], []
|
111 |
+
|
112 |
+
def chat(message, history, system_msg):
|
113 |
+
return "", history + [[message, None]]
|
114 |
+
|
115 |
+
submit.click(chat, [msg, chatbot, system_message], [msg, chatbot]).then(
|
116 |
+
respond, [chatbot.value[-1][0], chatbot.value, system_message], chatbot
|
117 |
+
)
|
118 |
+
clear.click(clear_chat, None, [chatbot, msg])
|
119 |
+
|
120 |
+
def update_buttons():
|
121 |
+
buttons_container.clear()
|
122 |
+
with buttons_container:
|
123 |
+
for i, row in enumerate(data):
|
124 |
+
btn = gr.Button(f"Prepare: {row['Topic']}")
|
125 |
+
btn.click(
|
126 |
+
fn=handle_prepare,
|
127 |
+
inputs=[gr.Number(value=i, visible=False), chatbot],
|
128 |
+
outputs=[chatbot]
|
129 |
+
).then(
|
130 |
+
respond,
|
131 |
+
inputs=[
|
132 |
+
lambda: f"Prepare a 10-minute reading for: {row['Topic']}",
|
133 |
+
lambda: chatbot.value,
|
134 |
+
system_message
|
135 |
+
],
|
136 |
+
outputs=chatbot
|
137 |
+
)
|
138 |
+
|
139 |
+
extract_btn.click(
|
140 |
+
fn=extract_table,
|
141 |
+
inputs=[url_input],
|
142 |
+
outputs=[table_output]
|
143 |
+
).then(
|
144 |
+
fn=update_buttons,
|
145 |
+
inputs=None,
|
146 |
+
outputs=None
|
147 |
+
)
|
148 |
|
149 |
if __name__ == "__main__":
|
150 |
+
demo.launch()
|