emeses commited on
Commit
43b7d18
·
1 Parent(s): 76839f2

Update space

Browse files
Files changed (1) hide show
  1. app.py +34 -56
app.py CHANGED
@@ -71,44 +71,18 @@ def extract_table(url):
71
  .dataframe tr:nth-child(even) {
72
  background-color: #f9f9f9;
73
  }
74
- .prepare-btn {
75
- padding: 5px 10px;
76
- cursor: pointer;
77
- background-color: #0366d6;
78
- color: white;
79
- border: none;
80
- border-radius: 4px;
81
- }
82
- .prepare-btn:hover {
83
- background-color: #0056b3;
84
- }
85
  </style>
86
  '''
87
 
88
  html += '<table class="dataframe">'
89
- html += '<thead><tr><th>Date</th><th>Topic</th><th>Action</th></tr></thead>'
90
  html += '<tbody>'
91
 
92
- for i, row in enumerate(data):
93
- safe_topic = row['Topic'].replace("'", "\\'") # Escape single quotes
94
  html += f'''
95
  <tr>
96
  <td>{row['Date']}</td>
97
  <td>{row['Topic']}</td>
98
- <td>
99
- <button class="prepare-btn" onclick="(function() {{
100
- const index = '{i}';
101
- const input = document.getElementById('prepare-index');
102
- input.value = index;
103
- const event = new Event('input');
104
- input.dispatchEvent(event);
105
- setTimeout(() => {{
106
- document.getElementById('prepare-trigger').click();
107
- }}, 100);
108
- }})()">
109
- Prepare
110
- </button>
111
- </td>
112
  </tr>
113
  '''
114
  html += '</tbody></table>'
@@ -118,26 +92,28 @@ def extract_table(url):
118
  print(f"Error in extract_table: {e}") # Debug print
119
  return f"<p>Error: {str(e)}</p>"
120
 
121
- def prepare_topic_message(index):
 
122
  try:
123
- print(f"Received index: {index}") # Debug print
124
- if not index:
125
- print("Empty index received")
126
- return ""
127
-
128
- idx = int(index)
129
- if 0 <= idx < len(data):
130
- topic = data[idx]["Topic"]
131
- date = data[idx]["Date"]
132
  message = f"Please prepare a 10-minute reading guide for the topic '{topic}' scheduled for {date}"
133
- print(f"Generated message: {message}") # Debug print
134
  return message
135
  else:
136
- print(f"Index {idx} out of range")
137
- return ""
 
 
 
138
  except Exception as e:
139
- print(f"Error in prepare_topic_message: {e}")
140
- return ""
141
 
142
  def add_text(history, text):
143
  history = history + [(text, None)]
@@ -153,11 +129,9 @@ def generate_response(history, system_message):
153
  history[-1] = (history[-1][0], response)
154
  yield history
155
 
156
-
157
  def clear_chat():
158
  return [], ""
159
 
160
- # Gradio app
161
  # Gradio app
162
  with gr.Blocks() as demo:
163
  with gr.Row():
@@ -169,10 +143,13 @@ with gr.Blocks() as demo:
169
  table_output = gr.HTML(label="Extracted Table")
170
  extract_btn = gr.Button("Extract Table")
171
 
172
- # Replace this section
173
- with gr.Row(visible=False): # Hide these components
174
- prepare_index = gr.Textbox(value="", elem_id="prepare-index")
175
- prepare_trigger = gr.Button("Prepare", elem_id="prepare-trigger")
 
 
 
176
 
177
  with gr.Column(scale=2):
178
  chatbot = gr.Chatbot()
@@ -191,14 +168,16 @@ with gr.Blocks() as demo:
191
  fn=extract_table,
192
  inputs=[url_input],
193
  outputs=[table_output]
 
 
 
194
  )
195
 
196
- # Replace the existing prepare trigger handler with this one
197
- prepare_trigger.click(
198
- fn=prepare_topic_message,
199
- inputs=[prepare_index],
200
- outputs=[msg],
201
- queue=False
202
  ).success(
203
  fn=add_text,
204
  inputs=[chatbot, msg],
@@ -240,6 +219,5 @@ with gr.Blocks() as demo:
240
  # Clear button handler
241
  clear.click(fn=clear_chat, outputs=[chatbot, msg])
242
 
243
-
244
  if __name__ == "__main__":
245
  demo.launch()
 
71
  .dataframe tr:nth-child(even) {
72
  background-color: #f9f9f9;
73
  }
 
 
 
 
 
 
 
 
 
 
 
74
  </style>
75
  '''
76
 
77
  html += '<table class="dataframe">'
78
+ html += '<thead><tr><th>Date</th><th>Topic</th></tr></thead>'
79
  html += '<tbody>'
80
 
81
+ for row in data:
 
82
  html += f'''
83
  <tr>
84
  <td>{row['Date']}</td>
85
  <td>{row['Topic']}</td>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  </tr>
87
  '''
88
  html += '</tbody></table>'
 
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"
117
 
118
  def add_text(history, text):
119
  history = history + [(text, None)]
 
129
  history[-1] = (history[-1][0], response)
130
  yield history
131
 
 
132
  def clear_chat():
133
  return [], ""
134
 
 
135
  # Gradio app
136
  with gr.Blocks() as demo:
137
  with gr.Row():
 
143
  table_output = gr.HTML(label="Extracted Table")
144
  extract_btn = gr.Button("Extract Table")
145
 
146
+ # Dropdown for selecting topic
147
+ topic_dropdown = gr.Dropdown(
148
+ label="Select Topic",
149
+ choices=[],
150
+ interactive=True
151
+ )
152
+ prepare_btn = gr.Button("Prepare Topic")
153
 
154
  with gr.Column(scale=2):
155
  chatbot = gr.Chatbot()
 
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
177
+ prepare_btn.click(
178
+ fn=prepare_topic,
179
+ inputs=[topic_dropdown],
180
+ outputs=[msg]
 
181
  ).success(
182
  fn=add_text,
183
  inputs=[chatbot, msg],
 
219
  # Clear button handler
220
  clear.click(fn=clear_chat, outputs=[chatbot, msg])
221
 
 
222
  if __name__ == "__main__":
223
  demo.launch()