emeses commited on
Commit
1e2cf67
·
1 Parent(s): 857e527

Update space

Browse files
Files changed (1) hide show
  1. app.py +75 -23
app.py CHANGED
@@ -8,7 +8,7 @@ client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
8
 
9
  data = [] # Global variable to store table 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:
@@ -47,14 +46,14 @@ def extract_table(url):
47
  if len(cells) >= 2:
48
  data.append({"Index": i, "Date": cells[0].text.strip()[:10], "Topic": cells[1].text.strip()})
49
 
50
- # Generate HTML table with Prepare buttons
51
  html_rows = ""
52
  for row in data:
53
  html_rows += f"""
54
  <tr>
55
  <td>{row['Date']}</td>
56
  <td>{row['Topic']}</td>
57
- <td><button onclick="prepareTopic({row['Index']})">Prepare</button></td>
58
  </tr>
59
  """
60
  html_table = f"""
@@ -67,9 +66,13 @@ def extract_table(url):
67
  {html_rows}
68
  </table>
69
  <script>
70
- function prepareTopic(index) {{
71
- document.getElementById('prepareInput').value = index;
72
- document.getElementById('prepareSubmit').click();
 
 
 
 
73
  }}
74
  </script>
75
  """
@@ -77,14 +80,18 @@ def extract_table(url):
77
  except Exception as e:
78
  return f"<p>Error: {str(e)}</p>"
79
 
80
-
81
- def handle_prepare(index):
82
  try:
83
  index = int(index)
84
- topic = data[index]["Topic"]
85
- return f"Prepare a 10-minute reading on what I should know before the class for the topic: {topic}"
 
 
 
 
86
  except Exception as e:
87
- return f"<p>Error: {str(e)}</p>"
 
88
 
89
  # Gradio app
90
  with gr.Blocks() as demo:
@@ -93,19 +100,64 @@ with gr.Blocks() as demo:
93
  url_input = gr.Textbox(value="https://id2223kth.github.io/schedule/", label="Table URL")
94
  table_output = gr.HTML(label="Extracted Table")
95
  extract_btn = gr.Button("Extract Table")
96
- extract_btn.click(fn=extract_table, inputs=[url_input], outputs=[table_output])
 
 
 
97
 
98
  with gr.Column(scale=2):
99
- chatbot = gr.ChatInterface(
100
- respond,
101
- additional_inputs=[
102
- gr.Textbox(value="Student class preparation companion.", label="System message"),
103
- ],
104
  )
105
- # Hidden input to handle prepare actions
106
- prepare_input = gr.Textbox(visible=False, elem_id="prepareInput")
107
- prepare_submit = gr.Button("Prepare Submit", visible=False, elem_id="prepareSubmit")
108
- prepare_submit.click(fn=handle_prepare, inputs=[prepare_input], outputs=[chatbot])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  if __name__ == "__main__":
111
- demo.launch()
 
8
 
9
  data = [] # Global variable to store table 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:
 
46
  if len(cells) >= 2:
47
  data.append({"Index": i, "Date": cells[0].text.strip()[:10], "Topic": cells[1].text.strip()})
48
 
49
+ # Generate HTML table with buttons that trigger Gradio events
50
  html_rows = ""
51
  for row in data:
52
  html_rows += f"""
53
  <tr>
54
  <td>{row['Date']}</td>
55
  <td>{row['Topic']}</td>
56
+ <td><button class="gr-button gr-button-lg" onclick="handle_topic_click({row['Index']})">Prepare</button></td>
57
  </tr>
58
  """
59
  html_table = f"""
 
66
  {html_rows}
67
  </table>
68
  <script>
69
+ function handle_topic_click(index) {{
70
+ const prepareInput = document.querySelector('#prepareInput textarea');
71
+ if (prepareInput) {{
72
+ prepareInput.value = index;
73
+ prepareInput.dispatchEvent(new Event('input'));
74
+ document.querySelector('#prepareSubmit').click();
75
+ }}
76
  }}
77
  </script>
78
  """
 
80
  except Exception as e:
81
  return f"<p>Error: {str(e)}</p>"
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
+ prompt = f"Prepare a 10-minute reading on what I should know before the class for the topic: {topic}"
89
+ history = history or []
90
+ history.append((prompt, None))
91
+ return history
92
  except Exception as e:
93
+ print(f"Error in handle_prepare: {e}")
94
+ return history or []
95
 
96
  # Gradio app
97
  with gr.Blocks() as demo:
 
100
  url_input = gr.Textbox(value="https://id2223kth.github.io/schedule/", label="Table URL")
101
  table_output = gr.HTML(label="Extracted Table")
102
  extract_btn = gr.Button("Extract Table")
103
+
104
+ # Hidden components for prepare functionality
105
+ prepare_input = gr.Textbox(visible=False, elem_id="prepareInput")
106
+ prepare_submit = gr.Button("Prepare Submit", visible=False, elem_id="prepareSubmit")
107
 
108
  with gr.Column(scale=2):
109
+ chatbot = gr.Chatbot()
110
+ msg = gr.Textbox(label="Message")
111
+ system_message = gr.Textbox(
112
+ value="Student class preparation companion.",
113
+ label="System message"
114
  )
115
+
116
+ with gr.Row():
117
+ submit = gr.Button("Submit")
118
+ clear = gr.Button("Clear")
119
+
120
+ # Event handlers
121
+ extract_btn.click(
122
+ fn=extract_table,
123
+ inputs=[url_input],
124
+ outputs=[table_output]
125
+ )
126
+
127
+ def user_message(message, history):
128
+ history = history or []
129
+ history.append((message, None))
130
+ return "", history
131
+
132
+ submit.click(
133
+ user_message,
134
+ inputs=[msg, chatbot],
135
+ outputs=[msg, chatbot]
136
+ ).then(
137
+ respond,
138
+ inputs=[msg, chatbot, system_message],
139
+ outputs=[chatbot]
140
+ )
141
+
142
+ # Handle prepare button clicks
143
+ prepare_submit.click(
144
+ handle_prepare,
145
+ inputs=[prepare_input, chatbot],
146
+ outputs=[chatbot]
147
+ ).then(
148
+ respond,
149
+ inputs=[
150
+ lambda x: f"Prepare a 10-minute reading for topic: {data[int(x)]['Topic'] if x.isdigit() and 0 <= int(x) < len(data) else ''}",
151
+ chatbot,
152
+ system_message
153
+ ],
154
+ outputs=[chatbot]
155
+ )
156
+
157
+ def clear_chat():
158
+ return [], []
159
+
160
+ clear.click(fn=clear_chat, outputs=[chatbot, msg])
161
 
162
  if __name__ == "__main__":
163
+ demo.launch()