Update space
Browse files
app.py
CHANGED
@@ -1,160 +1,106 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
import pandas as pd
|
4 |
import requests
|
5 |
from bs4 import BeautifulSoup
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
def respond(message, history, max_tokens=512, temperature=0.7, top_p=0.95):
|
11 |
-
try:
|
12 |
-
# Format messages including history
|
13 |
-
messages = []
|
14 |
-
for user_msg, assistant_msg in history:
|
15 |
-
messages.append({"role": "user", "content": user_msg})
|
16 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
17 |
-
messages.append({"role": "user", "content": message})
|
18 |
-
|
19 |
-
# Generate response
|
20 |
-
response = ""
|
21 |
-
for chunk in client.chat_completion(
|
22 |
-
messages,
|
23 |
-
max_tokens=max_tokens,
|
24 |
-
temperature=temperature,
|
25 |
-
top_p=top_p,
|
26 |
-
stream=True,
|
27 |
-
):
|
28 |
-
if hasattr(chunk.choices[0].delta, 'content'):
|
29 |
-
token = chunk.choices[0].delta.content
|
30 |
-
if token:
|
31 |
-
response += token
|
32 |
-
return response
|
33 |
-
|
34 |
-
except Exception as e:
|
35 |
-
return f"Error: {str(e)}"
|
36 |
|
37 |
-
def
|
|
|
38 |
try:
|
39 |
-
# Fetch and parse webpage
|
40 |
response = requests.get(url)
|
41 |
response.raise_for_status()
|
42 |
soup = BeautifulSoup(response.text, 'html.parser')
|
43 |
-
|
44 |
-
# Find table and extract data
|
45 |
table = soup.find('table')
|
46 |
if not table:
|
47 |
return "<p>No table found on page</p>"
|
48 |
-
|
49 |
-
|
50 |
rows = table.find_all('tr')
|
51 |
-
for row in rows[1:]:
|
52 |
cells = row.find_all('td')
|
53 |
-
if len(cells) >=
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
schedule_data.append({
|
59 |
-
'Date': date[:10],
|
60 |
-
'Topic': topic,
|
61 |
-
'Action': f"""
|
62 |
-
<button
|
63 |
-
onclick="triggerChatbotPreparation('{topic.replace("'", "")}')"
|
64 |
-
class="prepare-btn">
|
65 |
-
Prepare
|
66 |
-
</button>
|
67 |
-
"""
|
68 |
-
})
|
69 |
-
|
70 |
-
df = pd.DataFrame(schedule_data)
|
71 |
-
|
72 |
-
# Convert to HTML with styling and JavaScript
|
73 |
-
html = f"""
|
74 |
-
<style>
|
75 |
-
table {{
|
76 |
-
border-collapse: collapse;
|
77 |
-
width: 100%;
|
78 |
-
font-size: 12px;
|
79 |
-
}}
|
80 |
-
th, td {{
|
81 |
-
border: 1px solid black;
|
82 |
-
padding: 6px;
|
83 |
-
text-align: left;
|
84 |
-
font-family: Arial, sans-serif;
|
85 |
-
}}
|
86 |
-
.prepare-btn {{
|
87 |
-
padding: 4px 8px;
|
88 |
-
font-size: 11px;
|
89 |
-
cursor: pointer;
|
90 |
-
}}
|
91 |
-
</style>
|
92 |
-
<script>
|
93 |
-
function triggerChatbotPreparation(topic) {{
|
94 |
-
// Find all Gradio textareas
|
95 |
-
const textareas = document.querySelectorAll('.gradio-container textarea');
|
96 |
-
|
97 |
-
// Find the first textarea (assuming it's the input)
|
98 |
-
const textbox = textareas[0];
|
99 |
-
|
100 |
-
if (textbox) {{
|
101 |
-
// Set the value
|
102 |
-
const preparationMessage = `prepare 5 minutes reading important parts related to ${topic}`;
|
103 |
-
textbox.value = preparationMessage;
|
104 |
-
|
105 |
-
// Trigger input and change events
|
106 |
-
const inputEvent = new Event('input', {{ bubbles: true }});
|
107 |
-
const changeEvent = new Event('change', {{ bubbles: true }});
|
108 |
-
textbox.dispatchEvent(inputEvent);
|
109 |
-
textbox.dispatchEvent(changeEvent);
|
110 |
-
|
111 |
-
// Find and click the send button
|
112 |
-
const sendButtons = document.querySelectorAll('.gradio-container button');
|
113 |
-
for (let button of sendButtons) {{
|
114 |
-
if (button.getAttribute('aria-label') === 'Send') {{
|
115 |
-
button.click();
|
116 |
-
break;
|
117 |
-
}}
|
118 |
-
}}
|
119 |
-
}}
|
120 |
-
}}
|
121 |
-
</script>
|
122 |
-
{df.to_html(index=False, escape=False)}
|
123 |
-
"""
|
124 |
-
return html
|
125 |
|
|
|
|
|
126 |
except Exception as e:
|
127 |
return f"<p>Error: {str(e)}</p>"
|
128 |
|
129 |
-
def display_schedule(url):
|
130 |
-
try:
|
131 |
-
html_table = extract_schedule(url)
|
132 |
-
return html_table # Already HTML string
|
133 |
-
except Exception as e:
|
134 |
-
return str(e)
|
135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
with gr.Blocks() as demo:
|
137 |
with gr.Row():
|
138 |
-
# Left Column - Schedule
|
139 |
with gr.Column(scale=1):
|
140 |
url_input = gr.Textbox(
|
141 |
value="https://id2223kth.github.io/schedule/",
|
142 |
-
label="
|
143 |
)
|
144 |
-
|
145 |
-
extract_btn = gr.Button("Extract
|
146 |
|
147 |
extract_btn.click(
|
148 |
-
fn=
|
149 |
inputs=[url_input],
|
150 |
-
outputs=[
|
151 |
)
|
152 |
|
153 |
-
# Right Column - Chatbot
|
154 |
with gr.Column(scale=2):
|
|
|
155 |
chatbot = gr.ChatInterface(
|
156 |
-
respond
|
|
|
|
|
|
|
157 |
)
|
|
|
158 |
|
159 |
if __name__ == "__main__":
|
160 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
import requests
|
4 |
from bs4 import BeautifulSoup
|
5 |
+
import pandas as pd
|
6 |
|
7 |
+
"""
|
8 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
9 |
+
"""
|
10 |
+
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
11 |
+
|
12 |
+
|
13 |
+
def respond(
|
14 |
+
message,
|
15 |
+
history: list[tuple[str, str]],
|
16 |
+
system_message,
|
17 |
+
max_tokens,
|
18 |
+
temperature,
|
19 |
+
top_p,
|
20 |
+
):
|
21 |
+
messages = [{"role": "system", "content": system_message}]
|
22 |
+
|
23 |
+
for val in history:
|
24 |
+
if val[0]:
|
25 |
+
messages.append({"role": "user", "content": val[0]})
|
26 |
+
if val[1]:
|
27 |
+
messages.append({"role": "assistant", "content": val[1]})
|
28 |
+
|
29 |
+
messages.append({"role": "user", "content": message})
|
30 |
+
|
31 |
+
response = ""
|
32 |
+
|
33 |
+
for message in client.chat_completion(
|
34 |
+
messages,
|
35 |
+
max_tokens=max_tokens,
|
36 |
+
stream=True,
|
37 |
+
temperature=temperature,
|
38 |
+
top_p=top_p,
|
39 |
+
):
|
40 |
+
token = message.choices[0].delta.content
|
41 |
+
|
42 |
+
response += token
|
43 |
+
yield response
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
def extract_table(url):
|
47 |
+
global data
|
48 |
try:
|
|
|
49 |
response = requests.get(url)
|
50 |
response.raise_for_status()
|
51 |
soup = BeautifulSoup(response.text, 'html.parser')
|
|
|
|
|
52 |
table = soup.find('table')
|
53 |
if not table:
|
54 |
return "<p>No table found on page</p>"
|
55 |
+
|
56 |
+
data = []
|
57 |
rows = table.find_all('tr')
|
58 |
+
for i, row in enumerate(rows[1:]):
|
59 |
cells = row.find_all('td')
|
60 |
+
if len(cells) >= 2:
|
61 |
+
data.append({
|
62 |
+
'Date': cells[0].text.strip()[:10],
|
63 |
+
'Topic': cells[1].text.strip(),
|
64 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
df = pd.DataFrame(data)
|
67 |
+
return df.to_html(escape=False, index=False)
|
68 |
except Exception as e:
|
69 |
return f"<p>Error: {str(e)}</p>"
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
def display_table(url):
|
73 |
+
return extract_table(url)
|
74 |
+
|
75 |
+
|
76 |
+
"""
|
77 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
78 |
+
"""
|
79 |
with gr.Blocks() as demo:
|
80 |
with gr.Row():
|
|
|
81 |
with gr.Column(scale=1):
|
82 |
url_input = gr.Textbox(
|
83 |
value="https://id2223kth.github.io/schedule/",
|
84 |
+
label="Table URL"
|
85 |
)
|
86 |
+
table_output = gr.HTML(label="Extracted Table")
|
87 |
+
extract_btn = gr.Button("Extract Table")
|
88 |
|
89 |
extract_btn.click(
|
90 |
+
fn=display_table,
|
91 |
inputs=[url_input],
|
92 |
+
outputs=[table_output]
|
93 |
)
|
94 |
|
|
|
95 |
with gr.Column(scale=2):
|
96 |
+
|
97 |
chatbot = gr.ChatInterface(
|
98 |
+
respond,
|
99 |
+
additional_inputs=[
|
100 |
+
gr.Textbox(value="Student class preparation companion.", label="System message"),
|
101 |
+
],
|
102 |
)
|
103 |
+
|
104 |
|
105 |
if __name__ == "__main__":
|
106 |
demo.launch()
|