Maheen001 commited on
Commit
1da5b72
·
verified ·
1 Parent(s): d6124d1

Update ui/manual_dashboard.py

Browse files
Files changed (1) hide show
  1. ui/manual_dashboard.py +63 -18
ui/manual_dashboard.py CHANGED
@@ -186,39 +186,69 @@ def organize_files_ui():
186
  return f"⚠️ Error:\n{e}"
187
 
188
 
189
- # ---------------- FORM FILLING ----------------
190
  def fill_form_ui(template_file, user_text):
191
- """Fill form template with data"""
192
  if not template_file:
193
  return "⚠️ Upload .docx or .xlsx template", None
194
 
195
  try:
196
- template_path = os.path.join(UPLOAD_DIR, os.path.basename(template_file))
 
 
 
 
197
 
198
- # Copy uploaded file
199
- with open(template_file, "rb") as src:
200
- with open(template_path, "wb") as dst:
 
 
 
201
  dst.write(src.read())
202
 
203
  # Parse user input into dictionary
204
  fields = {}
205
  for line in user_text.split("\n"):
 
 
 
206
  if ":" in line:
207
- k, v = line.split(":", 1)
208
- fields[k.strip()] = v.strip()
 
 
 
 
 
 
 
 
209
 
210
  # Run async function properly
211
- result = asyncio.run(fill_form(template_path, fields))
212
 
213
  if isinstance(result, dict):
214
- if 'success' in result and result['success']:
215
  output_path = result.get('output_path', '')
216
- return "✅ Form filled successfully!", output_path
 
 
 
 
 
 
 
217
  else:
218
- return f"⚠️ Error: {result.get('error', 'Unknown error')}", None
219
- return " Form filled!", None
 
 
 
220
  except Exception as e:
221
- return f"⚠️ Error:\n{e}", None
 
 
222
 
223
 
224
  # ---------------- BUILD UI ----------------
@@ -295,19 +325,34 @@ def create_manual_dashboard(agent):
295
 
296
  btn2.click(calendar_ui, inputs=[title, date, time, desc], outputs=[status, file_out])
297
 
298
- # FORM FILLER TAB
299
  with gr.Tab("📋 Form Filler"):
300
  with gr.Group():
301
- gr.Markdown("Upload a template (.docx/.xlsx) with placeholders like `{{name}}`, `{{email}}`")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
  template = gr.File(label="Upload Template (.docx or .xlsx)")
303
  form_text = gr.Textbox(
304
  lines=10,
305
  label="Enter Field Data (key: value format)",
306
- placeholder="name: John Doe\nemail: john@example.com\nphone: 123-456-7890"
 
307
  )
308
 
309
  btn3 = gr.Button("Fill Form", variant="primary")
310
- status2 = gr.Textbox(label="Status")
311
  file2 = gr.File(label="Download Filled Form")
312
 
313
  btn3.click(fill_form_ui, inputs=[template, form_text], outputs=[status2, file2])
 
186
  return f"⚠️ Error:\n{e}"
187
 
188
 
189
+ # ---------------- FORM FILLING (FIXED) ----------------
190
  def fill_form_ui(template_file, user_text):
191
+ """Fill form template with data - FIXED"""
192
  if not template_file:
193
  return "⚠️ Upload .docx or .xlsx template", None
194
 
195
  try:
196
+ # Handle file path
197
+ if isinstance(template_file, str):
198
+ template_path = template_file
199
+ else:
200
+ template_path = template_file.name if hasattr(template_file, 'name') else str(template_file)
201
 
202
+ # Copy to uploads directory
203
+ filename = os.path.basename(template_path)
204
+ dest_template = os.path.join(UPLOAD_DIR, filename)
205
+
206
+ with open(template_path, "rb") as src:
207
+ with open(dest_template, "wb") as dst:
208
  dst.write(src.read())
209
 
210
  # Parse user input into dictionary
211
  fields = {}
212
  for line in user_text.split("\n"):
213
+ line = line.strip()
214
+ if not line:
215
+ continue
216
  if ":" in line:
217
+ parts = line.split(":", 1)
218
+ if len(parts) == 2:
219
+ key = parts[0].strip()
220
+ value = parts[1].strip()
221
+ # Clean field names
222
+ key = key.replace(" ", "_").lower()
223
+ fields[key] = value
224
+
225
+ if not fields:
226
+ return "⚠️ No fields parsed. Use format:\nField Name: Value", None
227
 
228
  # Run async function properly
229
+ result = asyncio.run(fill_form(dest_template, fields))
230
 
231
  if isinstance(result, dict):
232
+ if result.get('success'):
233
  output_path = result.get('output_path', '')
234
+ fields_filled = result.get('fields_filled', [])
235
+ total = result.get('total_fields', 0)
236
+
237
+ status_msg = f"✅ Form filled successfully!\n\nFields filled: {total}\n"
238
+ if fields_filled:
239
+ status_msg += f"Field names: {', '.join(fields_filled)}"
240
+
241
+ return status_msg, output_path
242
  else:
243
+ error = result.get('error', 'Unknown error')
244
+ return f"⚠️ Error: {error}", None
245
+
246
+ return "⚠️ Unexpected result format", None
247
+
248
  except Exception as e:
249
+ import traceback
250
+ error_detail = traceback.format_exc()
251
+ return f"⚠️ Error:\n{str(e)}\n\nDetails:\n{error_detail}", None
252
 
253
 
254
  # ---------------- BUILD UI ----------------
 
325
 
326
  btn2.click(calendar_ui, inputs=[title, date, time, desc], outputs=[status, file_out])
327
 
328
+ # FORM FILLER TAB (FIXED)
329
  with gr.Tab("📋 Form Filler"):
330
  with gr.Group():
331
+ gr.Markdown("""
332
+ ### How to use:
333
+ 1. Upload a template (.docx or .xlsx)
334
+ 2. Enter field values in the format: `Field Name: Value`
335
+ 3. Click "Fill Form"
336
+
337
+ **Example input:**
338
+ ```
339
+ First Name: Touqeer
340
+ Last Name: Iqbal
341
+ Cell Phone: +92-300-1234567
342
+ Email: touqeer@example.com
343
+ ```
344
+ """)
345
+
346
  template = gr.File(label="Upload Template (.docx or .xlsx)")
347
  form_text = gr.Textbox(
348
  lines=10,
349
  label="Enter Field Data (key: value format)",
350
+ placeholder="First Name: Touqeer\nLast Name: Iqbal\nCell Phone: +92-300-1234567\nEmail: touqeer@example.com",
351
+ value=""
352
  )
353
 
354
  btn3 = gr.Button("Fill Form", variant="primary")
355
+ status2 = gr.Textbox(label="Status", lines=4)
356
  file2 = gr.File(label="Download Filled Form")
357
 
358
  btn3.click(fill_form_ui, inputs=[template, form_text], outputs=[status2, file2])