Update space
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ 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 |
|
|
@@ -33,6 +34,7 @@ def respond(message, history, system_message):
|
|
| 33 |
|
| 34 |
def extract_table(url):
|
| 35 |
global data
|
|
|
|
| 36 |
try:
|
| 37 |
response = requests.get(url)
|
| 38 |
response.raise_for_status()
|
|
@@ -41,10 +43,8 @@ def extract_table(url):
|
|
| 41 |
if not table:
|
| 42 |
return "<p>No table found on page</p>"
|
| 43 |
|
| 44 |
-
# Clear existing data
|
| 45 |
-
data = []
|
| 46 |
rows = table.find_all('tr')
|
| 47 |
-
for
|
| 48 |
cells = row.find_all('td')
|
| 49 |
if len(cells) >= 2:
|
| 50 |
data.append({
|
|
@@ -52,92 +52,36 @@ def extract_table(url):
|
|
| 52 |
'Topic': cells[1].text.strip(),
|
| 53 |
})
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
<style>
|
| 58 |
-
.dataframe {
|
| 59 |
-
border-collapse: collapse;
|
| 60 |
-
width: 100%;
|
| 61 |
-
margin: 10px 0;
|
| 62 |
-
}
|
| 63 |
-
.dataframe th, .dataframe td {
|
| 64 |
-
border: 1px solid #ddd;
|
| 65 |
-
padding: 8px;
|
| 66 |
-
text-align: left;
|
| 67 |
-
}
|
| 68 |
-
.dataframe th {
|
| 69 |
-
background-color: #f6f8fa;
|
| 70 |
-
}
|
| 71 |
-
.dataframe tr:nth-child(even) {
|
| 72 |
-
background-color: #f9f9f9;
|
| 73 |
-
}
|
| 74 |
-
.prepare-btn {
|
| 75 |
-
padding: 5px 10px;
|
| 76 |
-
cursor: pointer;
|
| 77 |
-
background-color: #0366d6;
|
| 78 |
-
color: white;
|
| 79 |
-
border: none;
|
| 80 |
-
border-radius: 4px;
|
| 81 |
-
}
|
| 82 |
-
.prepare-btn:hover {
|
| 83 |
-
background-color: #0056b3;
|
| 84 |
-
}
|
| 85 |
-
</style>
|
| 86 |
-
'''
|
| 87 |
-
|
| 88 |
-
html += '<table class="dataframe">'
|
| 89 |
-
html += '<thead><tr><th>Date</th><th>Topic</th><th>Action</th></tr></thead>'
|
| 90 |
-
html += '<tbody>'
|
| 91 |
-
|
| 92 |
-
for i, row in enumerate(data):
|
| 93 |
-
safe_topic = row['Topic'].replace("'", "\\'") # Escape single quotes
|
| 94 |
-
html += f'''
|
| 95 |
-
<tr>
|
| 96 |
-
<td>{row['Date']}</td>
|
| 97 |
-
<td>{row['Topic']}</td>
|
| 98 |
-
<td>
|
| 99 |
-
<button class="prepare-btn" onclick="(function() {{
|
| 100 |
-
const index = {i};
|
| 101 |
-
const input = document.getElementById('prepare-index');
|
| 102 |
-
input.value = index;
|
| 103 |
-
const event = new Event('input');
|
| 104 |
-
input.dispatchEvent(event);
|
| 105 |
-
setTimeout(() => {{
|
| 106 |
-
document.getElementById('prepare-trigger').click();
|
| 107 |
-
}}, 100);
|
| 108 |
-
}})()">
|
| 109 |
-
Prepare
|
| 110 |
-
</button>
|
| 111 |
-
</td>
|
| 112 |
-
</tr>
|
| 113 |
-
'''
|
| 114 |
-
html += '</tbody></table>'
|
| 115 |
-
return html
|
| 116 |
-
|
| 117 |
except Exception as e:
|
| 118 |
-
print(f"Error in extract_table: {e}")
|
| 119 |
return f"<p>Error: {str(e)}</p>"
|
| 120 |
|
| 121 |
-
def
|
|
|
|
| 122 |
try:
|
| 123 |
-
|
| 124 |
-
if
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
-
print(f"
|
| 140 |
-
return ""
|
| 141 |
|
| 142 |
def add_text(history, text):
|
| 143 |
history = history + [(text, None)]
|
|
@@ -153,6 +97,15 @@ def generate_response(history, system_message):
|
|
| 153 |
history[-1] = (history[-1][0], response)
|
| 154 |
yield history
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
def clear_chat():
|
| 157 |
return [], ""
|
| 158 |
|
|
@@ -167,10 +120,14 @@ with gr.Blocks() as demo:
|
|
| 167 |
table_output = gr.HTML(label="Extracted Table")
|
| 168 |
extract_btn = gr.Button("Extract Table")
|
| 169 |
|
| 170 |
-
#
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
with gr.Column(scale=2):
|
| 176 |
chatbot = gr.Chatbot()
|
|
@@ -186,17 +143,19 @@ with gr.Blocks() as demo:
|
|
| 186 |
|
| 187 |
# Event handlers
|
| 188 |
extract_btn.click(
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
| 192 |
)
|
| 193 |
|
| 194 |
# Prepare topic handler
|
| 195 |
-
|
| 196 |
-
fn=
|
| 197 |
-
inputs=[
|
| 198 |
-
outputs=[msg]
|
| 199 |
-
queue=False
|
| 200 |
).success(
|
| 201 |
fn=add_text,
|
| 202 |
inputs=[chatbot, msg],
|
|
|
|
| 3 |
import requests
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
import pandas as pd
|
| 6 |
+
import ast
|
| 7 |
|
| 8 |
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
| 9 |
|
|
|
|
| 34 |
|
| 35 |
def extract_table(url):
|
| 36 |
global data
|
| 37 |
+
data = [] # Clear existing data
|
| 38 |
try:
|
| 39 |
response = requests.get(url)
|
| 40 |
response.raise_for_status()
|
|
|
|
| 43 |
if not table:
|
| 44 |
return "<p>No table found on page</p>"
|
| 45 |
|
|
|
|
|
|
|
| 46 |
rows = table.find_all('tr')
|
| 47 |
+
for row in rows[1:]: # Skip header row
|
| 48 |
cells = row.find_all('td')
|
| 49 |
if len(cells) >= 2:
|
| 50 |
data.append({
|
|
|
|
| 52 |
'Topic': cells[1].text.strip(),
|
| 53 |
})
|
| 54 |
|
| 55 |
+
print(f"Extracted {len(data)} rows") # Debug print
|
| 56 |
+
return create_table_html() # Your existing HTML creation code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
+
print(f"Error in extract_table: {e}")
|
| 59 |
return f"<p>Error: {str(e)}</p>"
|
| 60 |
|
| 61 |
+
def prepare_topic(selected_topic):
|
| 62 |
+
print(f"Preparing topic: {selected_topic}") # Debug print
|
| 63 |
try:
|
| 64 |
+
# Handle potential list or string input
|
| 65 |
+
if isinstance(selected_topic, list):
|
| 66 |
+
# If it's a list, take the first item
|
| 67 |
+
selected_topic = selected_topic[0] if selected_topic else ""
|
| 68 |
+
|
| 69 |
+
# Find the index of the selected topic
|
| 70 |
+
for index, row in enumerate(data):
|
| 71 |
+
full_topic = f"{row['Topic']} ({row['Date']})"
|
| 72 |
+
if full_topic == selected_topic:
|
| 73 |
+
topic = row["Topic"]
|
| 74 |
+
date = row["Date"]
|
| 75 |
+
message = f"Please prepare a 10-minute reading guide for the topic '{topic}' scheduled for {date}"
|
| 76 |
+
print(f"Generated preparation message: {message}") # Debug print
|
| 77 |
+
return message
|
| 78 |
+
|
| 79 |
+
print(f"Topic not found: {selected_topic}")
|
| 80 |
+
return "Error: Topic not found"
|
| 81 |
+
|
| 82 |
except Exception as e:
|
| 83 |
+
print(f"Unexpected error in prepare_topic: {e}")
|
| 84 |
+
return "Error: Could not prepare topic"
|
| 85 |
|
| 86 |
def add_text(history, text):
|
| 87 |
history = history + [(text, None)]
|
|
|
|
| 97 |
history[-1] = (history[-1][0], response)
|
| 98 |
yield history
|
| 99 |
|
| 100 |
+
def update_dropdown():
|
| 101 |
+
try:
|
| 102 |
+
choices = [f"{row['Topic']} ({row['Date']})" for row in data]
|
| 103 |
+
print(f"Generated choices: {choices}") # Debug print
|
| 104 |
+
return gr.Dropdown(choices=choices)
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"Error updating dropdown: {e}")
|
| 107 |
+
return gr.Dropdown(choices=[])
|
| 108 |
+
|
| 109 |
def clear_chat():
|
| 110 |
return [], ""
|
| 111 |
|
|
|
|
| 120 |
table_output = gr.HTML(label="Extracted Table")
|
| 121 |
extract_btn = gr.Button("Extract Table")
|
| 122 |
|
| 123 |
+
# Dropdown for selecting topic
|
| 124 |
+
topic_dropdown = gr.Dropdown(
|
| 125 |
+
label="Select Topic",
|
| 126 |
+
choices=[],
|
| 127 |
+
interactive=True,
|
| 128 |
+
allow_custom_value=True
|
| 129 |
+
)
|
| 130 |
+
prepare_btn = gr.Button("Prepare Topic")
|
| 131 |
|
| 132 |
with gr.Column(scale=2):
|
| 133 |
chatbot = gr.Chatbot()
|
|
|
|
| 143 |
|
| 144 |
# Event handlers
|
| 145 |
extract_btn.click(
|
| 146 |
+
fn=extract_table,
|
| 147 |
+
inputs=[url_input],
|
| 148 |
+
outputs=[table_output]
|
| 149 |
+
).success(
|
| 150 |
+
fn=lambda: [f"{row['Topic']} ({row['Date']})" for row in data],
|
| 151 |
+
outputs=[topic_dropdown]
|
| 152 |
)
|
| 153 |
|
| 154 |
# Prepare topic handler
|
| 155 |
+
prepare_btn.click(
|
| 156 |
+
fn=prepare_topic,
|
| 157 |
+
inputs=[topic_dropdown],
|
| 158 |
+
outputs=[msg]
|
|
|
|
| 159 |
).success(
|
| 160 |
fn=add_text,
|
| 161 |
inputs=[chatbot, msg],
|