emeses commited on
Commit
1ae3fcb
·
1 Parent(s): 0897fdf

Update space

Browse files
Files changed (1) hide show
  1. app.py +40 -29
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
 
@@ -39,7 +40,7 @@ def extract_table(url):
39
  soup = BeautifulSoup(response.text, 'html.parser')
40
  table = soup.find('table')
41
  if not table:
42
- return "<p>No table found on page</p>"
43
 
44
  # Clear existing data
45
  data = []
@@ -52,7 +53,7 @@ def extract_table(url):
52
  'Topic': cells[1].text.strip(),
53
  })
54
 
55
- # Create HTML table with prepare buttons
56
  html = '''
57
  <style>
58
  .dataframe {
@@ -86,31 +87,38 @@ def extract_table(url):
86
  </tr>
87
  '''
88
  html += '</tbody></table>'
89
- return html
 
 
 
90
 
91
  except Exception as e:
92
- print(f"Error in extract_table: {e}") # Debug print
93
- return f"<p>Error: {str(e)}</p>"
94
 
95
- def prepare_topic(index):
96
- print(f"Preparing topic with index: {index}") # Debug print
97
  try:
98
- # Convert index to integer
99
- index = int(index)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
- # Check if index is valid
102
- if 0 <= index < len(data):
103
- topic = data[index]["Topic"]
104
- date = data[index]["Date"]
105
- message = f"Please prepare a 10-minute reading guide for the topic '{topic}' scheduled for {date}"
106
- print(f"Generated preparation message: {message}") # Debug print
107
- return message
108
- else:
109
- print(f"Invalid index: {index}. Total data length: {len(data)}")
110
- return "Error: Invalid topic selection"
111
- except ValueError:
112
- print(f"Could not convert {index} to integer")
113
- return "Error: Invalid topic selection"
114
  except Exception as e:
115
  print(f"Unexpected error in prepare_topic: {e}")
116
  return "Error: Could not prepare topic"
@@ -147,7 +155,8 @@ with gr.Blocks() as demo:
147
  topic_dropdown = gr.Dropdown(
148
  label="Select Topic",
149
  choices=[],
150
- interactive=True
 
151
  )
152
  prepare_btn = gr.Button("Prepare Topic")
153
 
@@ -164,13 +173,15 @@ with gr.Blocks() as demo:
164
  clear = gr.Button("Clear")
165
 
166
  # Event handlers
 
 
 
 
 
167
  extract_btn.click(
168
- fn=extract_table,
169
  inputs=[url_input],
170
- outputs=[table_output]
171
- ).success(
172
- fn=lambda: [f"{row['Topic']} ({row['Date']})" for row in data],
173
- outputs=[topic_dropdown]
174
  )
175
 
176
  # Prepare topic handler
@@ -189,7 +200,7 @@ with gr.Blocks() as demo:
189
  outputs=[chatbot]
190
  )
191
 
192
- # Rest of your event handlers remain the same
193
  msg.submit(
194
  fn=add_text,
195
  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
 
 
40
  soup = BeautifulSoup(response.text, 'html.parser')
41
  table = soup.find('table')
42
  if not table:
43
+ return "<p>No table found on page</p>", []
44
 
45
  # Clear existing data
46
  data = []
 
53
  'Topic': cells[1].text.strip(),
54
  })
55
 
56
+ # Create HTML table
57
  html = '''
58
  <style>
59
  .dataframe {
 
87
  </tr>
88
  '''
89
  html += '</tbody></table>'
90
+
91
+ # Generate choices for dropdown
92
+ choices = [f"{row['Topic']} ({row['Date']})" for row in data]
93
+ return html, choices
94
 
95
  except Exception as e:
96
+ print(f"Error in extract_table: {e}")
97
+ return f"<p>Error: {str(e)}</p>", []
98
 
99
+ def prepare_topic(selected_topic):
100
+ print(f"Preparing topic: {selected_topic}") # Debug print
101
  try:
102
+ if not selected_topic:
103
+ return "Please select a topic first"
104
+
105
+ # Handle potential list or string input
106
+ if isinstance(selected_topic, list):
107
+ selected_topic = selected_topic[0] if selected_topic else ""
108
+
109
+ # Find the index of the selected topic
110
+ for row in data:
111
+ full_topic = f"{row['Topic']} ({row['Date']})"
112
+ if full_topic == selected_topic:
113
+ topic = row["Topic"]
114
+ date = row["Date"]
115
+ message = f"Please prepare a 10-minute reading guide for the topic '{topic}' scheduled for {date}"
116
+ print(f"Generated preparation message: {message}") # Debug print
117
+ return message
118
 
119
+ print(f"Topic not found: {selected_topic}")
120
+ return "Error: Topic not found"
121
+
 
 
 
 
 
 
 
 
 
 
122
  except Exception as e:
123
  print(f"Unexpected error in prepare_topic: {e}")
124
  return "Error: Could not prepare topic"
 
155
  topic_dropdown = gr.Dropdown(
156
  label="Select Topic",
157
  choices=[],
158
+ interactive=True,
159
+ value=None
160
  )
161
  prepare_btn = gr.Button("Prepare Topic")
162
 
 
173
  clear = gr.Button("Clear")
174
 
175
  # Event handlers
176
+ # Extract table and update dropdown
177
+ def update_interface(url):
178
+ html, choices = extract_table(url)
179
+ return html, gr.Dropdown(choices=choices)
180
+
181
  extract_btn.click(
182
+ fn=update_interface,
183
  inputs=[url_input],
184
+ outputs=[table_output, topic_dropdown]
 
 
 
185
  )
186
 
187
  # Prepare topic handler
 
200
  outputs=[chatbot]
201
  )
202
 
203
+ # Message submit handlers
204
  msg.submit(
205
  fn=add_text,
206
  inputs=[chatbot, msg],