emeses commited on
Commit
05f71ae
·
1 Parent(s): 62b19be

Update space

Browse files
Files changed (1) hide show
  1. app.py +79 -67
app.py CHANGED
@@ -52,80 +52,79 @@ def extract_table(url):
52
  'Topic': cells[1].text.strip(),
53
  })
54
 
55
- # Create HTML table with prepare buttons using inline JavaScript
56
- html = '<table class="dataframe">'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  html += '<thead><tr><th>Date</th><th>Topic</th><th>Action</th></tr></thead>'
58
  html += '<tbody>'
 
59
  for i, row in enumerate(data):
 
60
  html += f'''
61
  <tr>
62
  <td>{row['Date']}</td>
63
  <td>{row['Topic']}</td>
64
  <td>
65
- <button
66
- onclick="(() => {{
67
- const index = document.getElementById('prepare-index');
68
- index.value = '{i}';
69
- index.dispatchEvent(new Event('input'));
 
 
70
  document.getElementById('prepare-trigger').click();
71
- }})();"
72
- style="padding: 5px 10px; cursor: pointer; background-color: #0366d6; color: white; border: none; border-radius: 4px;"
73
- >
74
  Prepare
75
  </button>
76
  </td>
77
  </tr>
78
  '''
79
  html += '</tbody></table>'
80
-
81
- # Add some CSS to make the table look better
82
- html = f'''
83
- <style>
84
- .dataframe {{
85
- border-collapse: collapse;
86
- width: 100%;
87
- margin: 10px 0;
88
- }}
89
- .dataframe th, .dataframe td {{
90
- border: 1px solid #ddd;
91
- padding: 8px;
92
- text-align: left;
93
- }}
94
- .dataframe th {{
95
- background-color: #f6f8fa;
96
- }}
97
- .dataframe tr:nth-child(even) {{
98
- background-color: #f9f9f9;
99
- }}
100
- .dataframe button:hover {{
101
- background-color: #0056b3 !important;
102
- }}
103
- </style>
104
- {html}
105
- '''
106
  return html
 
107
  except Exception as e:
 
108
  return f"<p>Error: {str(e)}</p>"
109
 
110
- def add_text(history, text):
111
- history = history + [(text, None)]
112
- return history
113
-
114
- def generate_response(history, system_message):
115
- if not history:
116
- return history
117
-
118
- response = ""
119
- for chunk in respond(history[-1][0], history[:-1], system_message):
120
- response = chunk
121
- history[-1] = (history[-1][0], response)
122
- yield history
123
-
124
  def prepare_topic_message(index):
125
  try:
126
  print(f"Received index: {index}") # Debug print
127
-
128
- # Handle the index as a number
 
 
129
  idx = int(index)
130
  if 0 <= idx < len(data):
131
  topic = data[idx]["Topic"]
@@ -140,9 +139,25 @@ def prepare_topic_message(index):
140
  print(f"Error in prepare_topic_message: {e}")
141
  return ""
142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  def clear_chat():
144
  return [], ""
145
 
 
146
  # Gradio app
147
  with gr.Blocks() as demo:
148
  with gr.Row():
@@ -154,9 +169,10 @@ with gr.Blocks() as demo:
154
  table_output = gr.HTML(label="Extracted Table")
155
  extract_btn = gr.Button("Extract Table")
156
 
157
- # Hidden components for prepare functionality
158
- prepare_index = gr.Textbox(value="", visible=False, elem_id="prepare-index")
159
- prepare_trigger = gr.Button("Prepare", visible=False, elem_id="prepare-trigger")
 
160
 
161
  with gr.Column(scale=2):
162
  chatbot = gr.Chatbot()
@@ -177,25 +193,24 @@ with gr.Blocks() as demo:
177
  outputs=[table_output]
178
  )
179
 
180
- # Prepare trigger handler
181
- # Prepare trigger handler
182
  prepare_trigger.click(
183
  fn=prepare_topic_message,
184
- inputs=prepare_index,
185
- outputs=msg,
186
- queue=False # Changed from api_name to queue=False
187
  ).success(
188
  fn=add_text,
189
  inputs=[chatbot, msg],
190
- outputs=chatbot,
191
  queue=False
192
  ).then(
193
  fn=generate_response,
194
  inputs=[chatbot, system_message],
195
- outputs=chatbot
196
  )
197
 
198
- # Submit button handler
199
  msg.submit(
200
  fn=add_text,
201
  inputs=[chatbot, msg],
@@ -223,7 +238,4 @@ with gr.Blocks() as demo:
223
  )
224
 
225
  # Clear button handler
226
- clear.click(fn=clear_chat, outputs=[chatbot, msg])
227
-
228
- if __name__ == "__main__":
229
- demo.launch()
 
52
  'Topic': cells[1].text.strip(),
53
  })
54
 
55
+ # Create HTML table with prepare buttons
56
+ html = '''
57
+ <style>
58
+ .dataframe {
59
+ border-collapse: collapse;
60
+ width: 100%;
61
+ margin: 10px 0;
62
+ }
63
+ .dataframe th, .dataframe td {
64
+ border: 1px solid #ddd;
65
+ padding: 8px;
66
+ text-align: left;
67
+ }
68
+ .dataframe th {
69
+ background-color: #f6f8fa;
70
+ }
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>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  return html
116
+
117
  except Exception as e:
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"]
 
139
  print(f"Error in prepare_topic_message: {e}")
140
  return ""
141
 
142
+ def add_text(history, text):
143
+ history = history + [(text, None)]
144
+ return history
145
+
146
+ def generate_response(history, system_message):
147
+ if not history:
148
+ return history
149
+
150
+ response = ""
151
+ for chunk in respond(history[-1][0], history[:-1], system_message):
152
+ response = chunk
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
  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()
 
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],
205
+ outputs=[chatbot],
206
  queue=False
207
  ).then(
208
  fn=generate_response,
209
  inputs=[chatbot, system_message],
210
+ outputs=[chatbot]
211
  )
212
 
213
+ # Rest of your event handlers remain the same
214
  msg.submit(
215
  fn=add_text,
216
  inputs=[chatbot, msg],
 
238
  )
239
 
240
  # Clear button handler
241
+ clear.click(fn=clear_chat, outputs=[chatbot, msg])