Maheen001 commited on
Commit
1e13436
Β·
verified Β·
1 Parent(s): 02476c0

Create ui/manual_dashboard.py

Browse files
Files changed (1) hide show
  1. ui/manual_dashboard.py +363 -0
ui/manual_dashboard.py ADDED
@@ -0,0 +1,363 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Manual Dashboard UI - Visual tool interface
3
+ """
4
+
5
+ import gradio as gr
6
+ import asyncio
7
+ from pathlib import Path
8
+ from typing import List
9
+ import json
10
+
11
+
12
+ def create_manual_dashboard(agent):
13
+ """Create manual dashboard interface"""
14
+
15
+ # State for uploaded files
16
+ uploaded_files_state = gr.State([])
17
+
18
+ with gr.Row():
19
+ # Left sidebar - File upload and management
20
+ with gr.Column(scale=1):
21
+ gr.Markdown("### πŸ“ File Manager")
22
+
23
+ file_upload = gr.File(
24
+ label="Upload Files",
25
+ file_count="multiple",
26
+ file_types=[".pdf", ".png", ".jpg", ".jpeg", ".docx", ".txt", ".csv"]
27
+ )
28
+
29
+ files_list = gr.Textbox(
30
+ label="Uploaded Files",
31
+ placeholder="No files uploaded yet",
32
+ interactive=False,
33
+ lines=5
34
+ )
35
+
36
+ gr.Markdown("---")
37
+ gr.Markdown("### πŸ”§ Quick Actions")
38
+
39
+ with gr.Group():
40
+ ocr_btn = gr.Button("πŸ” Extract Text (OCR)", variant="secondary", size="sm")
41
+ summarize_btn = gr.Button("πŸ“ Summarize Document", variant="secondary", size="sm")
42
+ metadata_btn = gr.Button("πŸ“Š Extract Metadata", variant="secondary", size="sm")
43
+ calendar_btn = gr.Button("πŸ“… Create Calendar Event", variant="secondary", size="sm")
44
+ email_btn = gr.Button("βœ‰οΈ Draft Email", variant="secondary", size="sm")
45
+ organize_btn = gr.Button("πŸ—‚οΈ Organize Files", variant="secondary", size="sm")
46
+
47
+ # Main content area
48
+ with gr.Column(scale=2):
49
+ gr.Markdown("### 🎯 Tool Operations")
50
+
51
+ # Tabbed interface for different tools
52
+ with gr.Tabs():
53
+ # OCR Tool
54
+ with gr.Tab("πŸ” OCR"):
55
+ with gr.Row():
56
+ ocr_file_select = gr.Dropdown(
57
+ label="Select File",
58
+ choices=[],
59
+ interactive=True
60
+ )
61
+ ocr_language = gr.Dropdown(
62
+ label="Language",
63
+ choices=["en", "es", "fr", "de", "zh"],
64
+ value="en"
65
+ )
66
+
67
+ ocr_execute_btn = gr.Button("Extract Text", variant="primary")
68
+
69
+ ocr_output = gr.Textbox(
70
+ label="Extracted Text",
71
+ lines=10,
72
+ placeholder="Text will appear here..."
73
+ )
74
+
75
+ ocr_confidence = gr.Textbox(label="Confidence Score")
76
+
77
+ # PDF Summarization Tool
78
+ with gr.Tab("πŸ“ Summarize"):
79
+ pdf_file_select = gr.Dropdown(
80
+ label="Select PDF",
81
+ choices=[],
82
+ interactive=True
83
+ )
84
+
85
+ summary_length = gr.Slider(
86
+ label="Summary Length (words)",
87
+ minimum=100,
88
+ maximum=1000,
89
+ value=300,
90
+ step=50
91
+ )
92
+
93
+ summarize_execute_btn = gr.Button("Generate Summary", variant="primary")
94
+
95
+ summary_output = gr.Textbox(
96
+ label="Summary",
97
+ lines=10,
98
+ placeholder="Summary will appear here..."
99
+ )
100
+
101
+ pdf_metadata = gr.JSON(label="Document Metadata")
102
+
103
+ # Calendar Tool
104
+ with gr.Tab("πŸ“… Calendar"):
105
+ with gr.Row():
106
+ event_title = gr.Textbox(label="Event Title", placeholder="Meeting with team")
107
+ event_location = gr.Textbox(label="Location (optional)", placeholder="Conference Room A")
108
+
109
+ with gr.Row():
110
+ event_start = gr.Textbox(
111
+ label="Start Date/Time",
112
+ placeholder="2024-12-01 10:00 or Dec 1, 2024 10:00 AM"
113
+ )
114
+ event_end = gr.Textbox(
115
+ label="End Date/Time",
116
+ placeholder="2024-12-01 11:00"
117
+ )
118
+
119
+ event_description = gr.Textbox(
120
+ label="Description",
121
+ lines=3,
122
+ placeholder="Event details..."
123
+ )
124
+
125
+ calendar_execute_btn = gr.Button("Create Event", variant="primary")
126
+
127
+ calendar_output = gr.File(label="Download ICS File")
128
+ calendar_status = gr.Textbox(label="Status")
129
+
130
+ # Email Drafting Tool
131
+ with gr.Tab("βœ‰οΈ Email"):
132
+ with gr.Row():
133
+ email_recipient = gr.Textbox(label="Recipient", placeholder="john@example.com")
134
+ email_subject = gr.Textbox(label="Subject", placeholder="Regarding...")
135
+
136
+ email_context = gr.Textbox(
137
+ label="Context/Purpose",
138
+ lines=4,
139
+ placeholder="What should this email be about?"
140
+ )
141
+
142
+ email_tone = gr.Radio(
143
+ label="Tone",
144
+ choices=["professional", "friendly", "formal", "casual"],
145
+ value="professional"
146
+ )
147
+
148
+ email_execute_btn = gr.Button("Draft Email", variant="primary")
149
+
150
+ email_output = gr.Textbox(
151
+ label="Draft Email",
152
+ lines=10,
153
+ placeholder="Email draft will appear here..."
154
+ )
155
+
156
+ email_download = gr.File(label="Download Email Files")
157
+
158
+ # Form Filler Tool
159
+ with gr.Tab("πŸ“‹ Form Filler"):
160
+ form_template = gr.File(label="Upload Form Template (.docx or .xlsx)")
161
+
162
+ form_data = gr.Textbox(
163
+ label="Form Data (JSON)",
164
+ lines=8,
165
+ placeholder='{\n "name": "John Doe",\n "email": "john@example.com",\n "date": "2024-12-01"\n}',
166
+ value='{}'
167
+ )
168
+
169
+ form_execute_btn = gr.Button("Fill Form", variant="primary")
170
+
171
+ form_output = gr.File(label="Download Filled Form")
172
+ form_status = gr.Textbox(label="Status")
173
+
174
+ # File Organizer Tool
175
+ with gr.Tab("πŸ—‚οΈ Organize"):
176
+ organize_strategy = gr.Radio(
177
+ label="Organization Strategy",
178
+ choices=["by_type", "by_date", "by_size"],
179
+ value="by_type"
180
+ )
181
+
182
+ organize_execute_btn = gr.Button("Organize Files", variant="primary")
183
+
184
+ organize_output = gr.JSON(label="Organization Results")
185
+
186
+ # Event handlers
187
+ async def handle_file_upload(files):
188
+ if not files:
189
+ return "No files uploaded", [], []
190
+
191
+ file_list = []
192
+ file_info = []
193
+
194
+ for file in files:
195
+ # Copy to uploads directory
196
+ from utils.file_utils import copy_file, get_file_info
197
+
198
+ dest_path = f"data/uploads/{Path(file.name).name}"
199
+ copy_file(file.name, dest_path)
200
+
201
+ info = get_file_info(dest_path)
202
+ file_list.append(dest_path)
203
+ file_info.append(f"βœ“ {info['name']} ({info['size_mb']} MB)")
204
+
205
+ # Add to RAG
206
+ await agent.process_files_to_rag([{'path': dest_path, 'name': info['name']}])
207
+
208
+ files_text = "\n".join(file_info)
209
+ return files_text, file_list, file_list
210
+
211
+ async def handle_ocr(file_path, language):
212
+ if not file_path:
213
+ return "Please select a file", ""
214
+
215
+ result = await agent.manual_tool_call(
216
+ 'ocr_extract_text',
217
+ {'file_path': file_path, 'language': language}
218
+ )
219
+
220
+ if result['success']:
221
+ data = result['result']
222
+ return data.get('text', ''), f"Confidence: {data.get('confidence', 0):.2%}"
223
+ else:
224
+ return f"Error: {result['error']}", ""
225
+
226
+ async def handle_summarize(file_path, max_length):
227
+ if not file_path:
228
+ return "Please select a file", {}
229
+
230
+ result = await agent.manual_tool_call(
231
+ 'pdf_summarize',
232
+ {'file_path': file_path, 'max_length': max_length}
233
+ )
234
+
235
+ if result['success']:
236
+ data = result['result']
237
+ return data.get('summary', ''), data.get('metadata', {})
238
+ else:
239
+ return f"Error: {result['error']}", {}
240
+
241
+ async def handle_calendar(title, start, end, description, location):
242
+ result = await agent.manual_tool_call(
243
+ 'calendar_create_event',
244
+ {
245
+ 'title': title,
246
+ 'start': start,
247
+ 'end': end,
248
+ 'description': description,
249
+ 'location': location
250
+ }
251
+ )
252
+
253
+ if result['success']:
254
+ data = result['result']
255
+ return data.get('output_path'), f"βœ“ Event created: {title}"
256
+ else:
257
+ return None, f"βœ— Error: {result['error']}"
258
+
259
+ async def handle_email(recipient, subject, context, tone):
260
+ result = await agent.manual_tool_call(
261
+ 'email_draft',
262
+ {
263
+ 'recipient': recipient,
264
+ 'subject': subject,
265
+ 'context': context,
266
+ 'tone': tone
267
+ }
268
+ )
269
+
270
+ if result['success']:
271
+ data = result['result']
272
+ return data.get('email_body', ''), data.get('output_path_html')
273
+ else:
274
+ return f"Error: {result['error']}", None
275
+
276
+ async def handle_form_fill(template_file, data_json):
277
+ if not template_file:
278
+ return None, "Please upload a form template"
279
+
280
+ try:
281
+ data = json.loads(data_json)
282
+ except:
283
+ return None, "Invalid JSON data"
284
+
285
+ result = await agent.manual_tool_call(
286
+ 'form_fill',
287
+ {
288
+ 'template_path': template_file.name,
289
+ 'data': data
290
+ }
291
+ )
292
+
293
+ if result['success']:
294
+ res_data = result['result']
295
+ return res_data.get('output_path'), f"βœ“ Form filled: {len(res_data.get('fields_filled', []))} fields"
296
+ else:
297
+ return None, f"βœ— Error: {result['error']}"
298
+
299
+ async def handle_organize(file_list, strategy):
300
+ if not file_list:
301
+ return {"error": "No files to organize"}
302
+
303
+ # Use upload directory
304
+ result = await agent.manual_tool_call(
305
+ 'file_organize',
306
+ {
307
+ 'directory': 'data/uploads',
308
+ 'strategy': strategy
309
+ }
310
+ )
311
+
312
+ if result['success']:
313
+ return result['result']
314
+ else:
315
+ return {"error": result['error']}
316
+
317
+ # Wire up events
318
+ file_upload.change(
319
+ fn=handle_file_upload,
320
+ inputs=[file_upload],
321
+ outputs=[files_list, uploaded_files_state, ocr_file_select, pdf_file_select]
322
+ )
323
+
324
+ ocr_execute_btn.click(
325
+ fn=handle_ocr,
326
+ inputs=[ocr_file_select, ocr_language],
327
+ outputs=[ocr_output, ocr_confidence]
328
+ )
329
+
330
+ ocr_btn.click(
331
+ fn=lambda: gr.Tabs.update(selected="πŸ” OCR"),
332
+ outputs=[]
333
+ )
334
+
335
+ summarize_execute_btn.click(
336
+ fn=handle_summarize,
337
+ inputs=[pdf_file_select, summary_length],
338
+ outputs=[summary_output, pdf_metadata]
339
+ )
340
+
341
+ calendar_execute_btn.click(
342
+ fn=handle_calendar,
343
+ inputs=[event_title, event_start, event_end, event_description, event_location],
344
+ outputs=[calendar_output, calendar_status]
345
+ )
346
+
347
+ email_execute_btn.click(
348
+ fn=handle_email,
349
+ inputs=[email_recipient, email_subject, email_context, email_tone],
350
+ outputs=[email_output, email_download]
351
+ )
352
+
353
+ form_execute_btn.click(
354
+ fn=handle_form_fill,
355
+ inputs=[form_template, form_data],
356
+ outputs=[form_output, form_status]
357
+ )
358
+
359
+ organize_execute_btn.click(
360
+ fn=handle_organize,
361
+ inputs=[uploaded_files_state, organize_strategy],
362
+ outputs=[organize_output]
363
+ )