Update space
Browse files
app.py
CHANGED
|
@@ -9,26 +9,15 @@ client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
|
| 9 |
# Global data store for the table
|
| 10 |
data = []
|
| 11 |
|
| 12 |
-
def respond(
|
| 13 |
-
message,
|
| 14 |
-
history: list[tuple[str, str]],
|
| 15 |
-
system_message,
|
| 16 |
-
max_tokens,
|
| 17 |
-
temperature,
|
| 18 |
-
top_p,
|
| 19 |
-
):
|
| 20 |
messages = [{"role": "system", "content": system_message}]
|
| 21 |
-
|
| 22 |
for val in history:
|
| 23 |
if val[0]:
|
| 24 |
messages.append({"role": "user", "content": val[0]})
|
| 25 |
if val[1]:
|
| 26 |
messages.append({"role": "assistant", "content": val[1]})
|
| 27 |
-
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
-
|
| 30 |
response = ""
|
| 31 |
-
|
| 32 |
for message in client.chat_completion(
|
| 33 |
messages,
|
| 34 |
max_tokens=max_tokens,
|
|
@@ -63,9 +52,7 @@ def extract_table(url):
|
|
| 63 |
|
| 64 |
# Create HTML table with prepare buttons
|
| 65 |
html = '<table class="dataframe">'
|
| 66 |
-
# Add header
|
| 67 |
html += '<thead><tr><th>Date</th><th>Topic</th><th>Action</th></tr></thead>'
|
| 68 |
-
# Add rows with prepare buttons
|
| 69 |
html += '<tbody>'
|
| 70 |
for i, row in enumerate(data):
|
| 71 |
html += f'''
|
|
@@ -75,6 +62,7 @@ def extract_table(url):
|
|
| 75 |
<td>
|
| 76 |
<button onclick='
|
| 77 |
document.getElementById("prepare-topic").value = "{i}";
|
|
|
|
| 78 |
document.getElementById("prepare-button").click();
|
| 79 |
'>
|
| 80 |
Prepare
|
|
@@ -87,11 +75,27 @@ def extract_table(url):
|
|
| 87 |
except Exception as e:
|
| 88 |
return f"<p>Error: {str(e)}</p>"
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def clear_chat():
|
| 91 |
-
|
| 92 |
-
# For now, it just returns an empty chat history
|
| 93 |
-
return []
|
| 94 |
|
|
|
|
| 95 |
with gr.Blocks() as demo:
|
| 96 |
with gr.Row():
|
| 97 |
with gr.Column(scale=1):
|
|
@@ -107,12 +111,16 @@ with gr.Blocks() as demo:
|
|
| 107 |
prepare_button = gr.Button("Prepare", visible=False, elem_id="prepare-button")
|
| 108 |
|
| 109 |
with gr.Column(scale=2):
|
| 110 |
-
chatbot = gr.
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# Event handlers
|
| 118 |
extract_btn.click(
|
|
@@ -121,12 +129,30 @@ with gr.Blocks() as demo:
|
|
| 121 |
outputs=[table_output]
|
| 122 |
)
|
| 123 |
|
| 124 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
prepare_button.click(
|
| 126 |
-
fn=
|
| 127 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
outputs=[chatbot]
|
| 129 |
)
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
demo.launch()
|
|
|
|
| 9 |
# Global data store for the table
|
| 10 |
data = []
|
| 11 |
|
| 12 |
+
def respond(message, history, system_message, max_tokens=2048, temperature=0.7, top_p=0.9):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 14 |
for val in history:
|
| 15 |
if val[0]:
|
| 16 |
messages.append({"role": "user", "content": val[0]})
|
| 17 |
if val[1]:
|
| 18 |
messages.append({"role": "assistant", "content": val[1]})
|
|
|
|
| 19 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 20 |
response = ""
|
|
|
|
| 21 |
for message in client.chat_completion(
|
| 22 |
messages,
|
| 23 |
max_tokens=max_tokens,
|
|
|
|
| 52 |
|
| 53 |
# Create HTML table with prepare buttons
|
| 54 |
html = '<table class="dataframe">'
|
|
|
|
| 55 |
html += '<thead><tr><th>Date</th><th>Topic</th><th>Action</th></tr></thead>'
|
|
|
|
| 56 |
html += '<tbody>'
|
| 57 |
for i, row in enumerate(data):
|
| 58 |
html += f'''
|
|
|
|
| 62 |
<td>
|
| 63 |
<button onclick='
|
| 64 |
document.getElementById("prepare-topic").value = "{i}";
|
| 65 |
+
document.getElementById("prepare-topic").dispatchEvent(new Event("input"));
|
| 66 |
document.getElementById("prepare-button").click();
|
| 67 |
'>
|
| 68 |
Prepare
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
return f"<p>Error: {str(e)}</p>"
|
| 77 |
|
| 78 |
+
def user_message(message, history):
|
| 79 |
+
history = history or []
|
| 80 |
+
history.append((message, None))
|
| 81 |
+
return "", history
|
| 82 |
+
|
| 83 |
+
def handle_prepare(index, history):
|
| 84 |
+
try:
|
| 85 |
+
index = int(index)
|
| 86 |
+
if 0 <= index < len(data):
|
| 87 |
+
topic = data[index]["Topic"]
|
| 88 |
+
message = f"Prepare a 10-minute reading for the topic: {topic}"
|
| 89 |
+
history = history or []
|
| 90 |
+
history.append((message, None))
|
| 91 |
+
return history
|
| 92 |
+
except:
|
| 93 |
+
return history or []
|
| 94 |
+
|
| 95 |
def clear_chat():
|
| 96 |
+
return [], ""
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# Gradio app
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
with gr.Row():
|
| 101 |
with gr.Column(scale=1):
|
|
|
|
| 111 |
prepare_button = gr.Button("Prepare", visible=False, elem_id="prepare-button")
|
| 112 |
|
| 113 |
with gr.Column(scale=2):
|
| 114 |
+
chatbot = gr.Chatbot()
|
| 115 |
+
msg = gr.Textbox(label="Message")
|
| 116 |
+
system_message = gr.Textbox(
|
| 117 |
+
value="Student class preparation companion.",
|
| 118 |
+
label="System message"
|
| 119 |
)
|
| 120 |
+
|
| 121 |
+
with gr.Row():
|
| 122 |
+
submit = gr.Button("Submit")
|
| 123 |
+
clear = gr.Button("Clear")
|
| 124 |
|
| 125 |
# Event handlers
|
| 126 |
extract_btn.click(
|
|
|
|
| 129 |
outputs=[table_output]
|
| 130 |
)
|
| 131 |
|
| 132 |
+
# Submit button handler
|
| 133 |
+
submit.click(
|
| 134 |
+
fn=user_message,
|
| 135 |
+
inputs=[msg, chatbot],
|
| 136 |
+
outputs=[msg, chatbot]
|
| 137 |
+
).then(
|
| 138 |
+
fn=respond,
|
| 139 |
+
inputs=[msg, chatbot, system_message],
|
| 140 |
+
outputs=[chatbot]
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
# Prepare button handler
|
| 144 |
prepare_button.click(
|
| 145 |
+
fn=handle_prepare,
|
| 146 |
+
inputs=[prepare_topic, chatbot],
|
| 147 |
+
outputs=[chatbot]
|
| 148 |
+
).then(
|
| 149 |
+
fn=respond,
|
| 150 |
+
inputs=[prepare_topic, chatbot, system_message],
|
| 151 |
outputs=[chatbot]
|
| 152 |
)
|
| 153 |
+
|
| 154 |
+
# Clear button handler
|
| 155 |
+
clear.click(fn=clear_chat, outputs=[chatbot, msg])
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
demo.launch()
|