|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
import requests |
|
from bs4 import BeautifulSoup |
|
import pandas as pd |
|
|
|
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct") |
|
|
|
data = [] |
|
|
|
def respond(message, history, system_message, max_tokens=2048, temperature=0.7, top_p=0.9): |
|
messages = [{"role": "system", "content": system_message}] |
|
for val in history: |
|
if val[0]: |
|
messages.append({"role": "user", "content": val[0]}) |
|
if val[1]: |
|
messages.append({"role": "assistant", "content": val[1]}) |
|
messages.append({"role": "user", "content": message}) |
|
response = "" |
|
for message in client.chat_completion( |
|
messages, |
|
max_tokens=max_tokens, |
|
stream=True, |
|
temperature=temperature, |
|
top_p=top_p, |
|
): |
|
token = message.choices[0].delta.content |
|
response += token |
|
yield response |
|
|
|
def extract_table(url): |
|
global data |
|
try: |
|
response = requests.get(url) |
|
response.raise_for_status() |
|
soup = BeautifulSoup(response.text, "html.parser") |
|
table = soup.find("table") |
|
if not table: |
|
return "<p>No table found on page</p>" |
|
|
|
|
|
data = [] |
|
rows = table.find_all("tr") |
|
for i, row in enumerate(rows[1:]): |
|
cells = row.find_all("td") |
|
if len(cells) >= 2: |
|
data.append({"Index": i, "Date": cells[0].text.strip()[:10], "Topic": cells[1].text.strip()}) |
|
|
|
|
|
html_rows = "" |
|
for row in data: |
|
html_rows += f""" |
|
<tr> |
|
<td>{row['Date']}</td> |
|
<td>{row['Topic']}</td> |
|
<td><button class="gr-button gr-button-lg" onclick="handle_topic_click({row['Index']})">Prepare</button></td> |
|
</tr> |
|
""" |
|
html_table = f""" |
|
<table border="1"> |
|
<tr> |
|
<th>Date</th> |
|
<th>Topic</th> |
|
<th>Action</th> |
|
</tr> |
|
{html_rows} |
|
</table> |
|
<script> |
|
function handle_topic_click(index) {{ |
|
const prepareInput = document.querySelector('#prepareInput textarea'); |
|
if (prepareInput) {{ |
|
prepareInput.value = index; |
|
prepareInput.dispatchEvent(new Event('input')); |
|
document.querySelector('#prepareSubmit').click(); |
|
}} |
|
}} |
|
</script> |
|
""" |
|
return html_table |
|
except Exception as e: |
|
return f"<p>Error: {str(e)}</p>" |
|
|
|
def handle_prepare(index, history): |
|
try: |
|
index = int(index) |
|
if 0 <= index < len(data): |
|
topic = data[index]["Topic"] |
|
prompt = f"Prepare a 10-minute reading on what I should know before the class for the topic: {topic}" |
|
history = history or [] |
|
history.append((prompt, None)) |
|
return history |
|
except Exception as e: |
|
print(f"Error in handle_prepare: {e}") |
|
return history or [] |
|
|
|
|
|
with gr.Blocks() as demo: |
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
url_input = gr.Textbox(value="https://id2223kth.github.io/schedule/", label="Table URL") |
|
table_output = gr.HTML(label="Extracted Table") |
|
extract_btn = gr.Button("Extract Table") |
|
|
|
|
|
prepare_input = gr.Textbox(visible=False, elem_id="prepareInput") |
|
prepare_submit = gr.Button("Prepare Submit", visible=False, elem_id="prepareSubmit") |
|
|
|
with gr.Column(scale=2): |
|
chatbot = gr.Chatbot() |
|
msg = gr.Textbox(label="Message") |
|
system_message = gr.Textbox( |
|
value="Student class preparation companion.", |
|
label="System message" |
|
) |
|
|
|
with gr.Row(): |
|
submit = gr.Button("Submit") |
|
clear = gr.Button("Clear") |
|
|
|
|
|
extract_btn.click( |
|
fn=extract_table, |
|
inputs=[url_input], |
|
outputs=[table_output] |
|
) |
|
|
|
def user_message(message, history): |
|
history = history or [] |
|
history.append((message, None)) |
|
return "", history |
|
|
|
submit.click( |
|
user_message, |
|
inputs=[msg, chatbot], |
|
outputs=[msg, chatbot] |
|
).then( |
|
respond, |
|
inputs=[msg, chatbot, system_message], |
|
outputs=[chatbot] |
|
) |
|
|
|
|
|
prepare_submit.click( |
|
handle_prepare, |
|
inputs=[prepare_input, chatbot], |
|
outputs=[chatbot] |
|
).then( |
|
respond, |
|
inputs=[ |
|
lambda x: f"Prepare a 10-minute reading for topic: {data[int(x)]['Topic'] if x.isdigit() and 0 <= int(x) < len(data) else ''}", |
|
chatbot, |
|
system_message |
|
], |
|
outputs=[chatbot] |
|
) |
|
|
|
def clear_chat(): |
|
return [], [] |
|
|
|
clear.click(fn=clear_chat, outputs=[chatbot, msg]) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |