Spaces:
Running
Running
| """ | |
| Manual Dashboard UI - Visual tool interface | |
| """ | |
| import gradio as gr | |
| import asyncio | |
| from pathlib import Path | |
| from typing import List | |
| import json | |
| def create_manual_dashboard(agent): | |
| """Create manual dashboard interface""" | |
| # State for uploaded files | |
| uploaded_files_state = gr.State([]) | |
| with gr.Row(): | |
| # Left sidebar - File upload and management | |
| with gr.Column(scale=1): | |
| gr.Markdown("### π File Manager") | |
| file_upload = gr.File( | |
| label="Upload Files", | |
| file_count="multiple", | |
| file_types=[".pdf", ".png", ".jpg", ".jpeg", ".docx", ".txt", ".csv"] | |
| ) | |
| files_list = gr.Textbox( | |
| label="Uploaded Files", | |
| placeholder="No files uploaded yet", | |
| interactive=False, | |
| lines=5 | |
| ) | |
| gr.Markdown("---") | |
| gr.Markdown("### π§ Quick Actions") | |
| with gr.Group(): | |
| ocr_btn = gr.Button("π Extract Text (OCR)", variant="secondary", size="sm") | |
| summarize_btn = gr.Button("π Summarize Document", variant="secondary", size="sm") | |
| metadata_btn = gr.Button("π Extract Metadata", variant="secondary", size="sm") | |
| calendar_btn = gr.Button("π Create Calendar Event", variant="secondary", size="sm") | |
| email_btn = gr.Button("βοΈ Draft Email", variant="secondary", size="sm") | |
| organize_btn = gr.Button("ποΈ Organize Files", variant="secondary", size="sm") | |
| # Main content area | |
| with gr.Column(scale=2): | |
| gr.Markdown("### π― Tool Operations") | |
| # Tabbed interface for different tools | |
| with gr.Tabs(): | |
| # OCR Tool | |
| with gr.Tab("π OCR"): | |
| with gr.Row(): | |
| ocr_file_select = gr.Dropdown( | |
| label="Select File", | |
| choices=[], | |
| interactive=True | |
| ) | |
| ocr_language = gr.Dropdown( | |
| label="Language", | |
| choices=["en", "es", "fr", "de", "zh"], | |
| value="en" | |
| ) | |
| ocr_execute_btn = gr.Button("Extract Text", variant="primary") | |
| ocr_output = gr.Textbox( | |
| label="Extracted Text", | |
| lines=10, | |
| placeholder="Text will appear here..." | |
| ) | |
| ocr_confidence = gr.Textbox(label="Confidence Score") | |
| # PDF Summarization Tool | |
| with gr.Tab("π Summarize"): | |
| pdf_file_select = gr.Dropdown( | |
| label="Select PDF", | |
| choices=[], | |
| interactive=True | |
| ) | |
| summary_length = gr.Slider( | |
| label="Summary Length (words)", | |
| minimum=100, | |
| maximum=1000, | |
| value=300, | |
| step=50 | |
| ) | |
| summarize_execute_btn = gr.Button("Generate Summary", variant="primary") | |
| summary_output = gr.Textbox( | |
| label="Summary", | |
| lines=10, | |
| placeholder="Summary will appear here..." | |
| ) | |
| pdf_metadata = gr.JSON(label="Document Metadata") | |
| # Calendar Tool | |
| with gr.Tab("π Calendar"): | |
| with gr.Row(): | |
| event_title = gr.Textbox(label="Event Title", placeholder="Meeting with team") | |
| event_location = gr.Textbox(label="Location (optional)", placeholder="Conference Room A") | |
| with gr.Row(): | |
| event_start = gr.Textbox( | |
| label="Start Date/Time", | |
| placeholder="2024-12-01 10:00 or Dec 1, 2024 10:00 AM" | |
| ) | |
| event_end = gr.Textbox( | |
| label="End Date/Time", | |
| placeholder="2024-12-01 11:00" | |
| ) | |
| event_description = gr.Textbox( | |
| label="Description", | |
| lines=3, | |
| placeholder="Event details..." | |
| ) | |
| calendar_execute_btn = gr.Button("Create Event", variant="primary") | |
| calendar_output = gr.File(label="Download ICS File") | |
| calendar_status = gr.Textbox(label="Status") | |
| # Email Drafting Tool | |
| with gr.Tab("βοΈ Email"): | |
| with gr.Row(): | |
| email_recipient = gr.Textbox(label="Recipient", placeholder="john@example.com") | |
| email_subject = gr.Textbox(label="Subject", placeholder="Regarding...") | |
| email_context = gr.Textbox( | |
| label="Context/Purpose", | |
| lines=4, | |
| placeholder="What should this email be about?" | |
| ) | |
| email_tone = gr.Radio( | |
| label="Tone", | |
| choices=["professional", "friendly", "formal", "casual"], | |
| value="professional" | |
| ) | |
| email_execute_btn = gr.Button("Draft Email", variant="primary") | |
| email_output = gr.Textbox( | |
| label="Draft Email", | |
| lines=10, | |
| placeholder="Email draft will appear here..." | |
| ) | |
| email_download = gr.File(label="Download Email Files") | |
| # Form Filler Tool | |
| with gr.Tab("π Form Filler"): | |
| form_template = gr.File(label="Upload Form Template (.docx or .xlsx)") | |
| form_data = gr.Textbox( | |
| label="Form Data (JSON)", | |
| lines=8, | |
| placeholder='{\n "name": "John Doe",\n "email": "john@example.com",\n "date": "2024-12-01"\n}', | |
| value='{}' | |
| ) | |
| form_execute_btn = gr.Button("Fill Form", variant="primary") | |
| form_output = gr.File(label="Download Filled Form") | |
| form_status = gr.Textbox(label="Status") | |
| # File Organizer Tool | |
| with gr.Tab("ποΈ Organize"): | |
| organize_strategy = gr.Radio( | |
| label="Organization Strategy", | |
| choices=["by_type", "by_date", "by_size"], | |
| value="by_type" | |
| ) | |
| organize_execute_btn = gr.Button("Organize Files", variant="primary") | |
| organize_output = gr.JSON(label="Organization Results") | |
| # Event handlers | |
| async def handle_file_upload(files): | |
| if not files: | |
| return "No files uploaded", [], [] | |
| file_list = [] | |
| file_info = [] | |
| for file in files: | |
| # Copy to uploads directory | |
| from utils.file_utils import copy_file, get_file_info | |
| dest_path = f"data/uploads/{Path(file.name).name}" | |
| copy_file(file.name, dest_path) | |
| info = get_file_info(dest_path) | |
| file_list.append(dest_path) | |
| file_info.append(f"β {info['name']} ({info['size_mb']} MB)") | |
| # Add to RAG | |
| await agent.process_files_to_rag([{'path': dest_path, 'name': info['name']}]) | |
| files_text = "\n".join(file_info) | |
| return files_text, file_list, file_list | |
| async def handle_ocr(file_path, language): | |
| if not file_path: | |
| return "Please select a file", "" | |
| result = await agent.manual_tool_call( | |
| 'ocr_extract_text', | |
| {'file_path': file_path, 'language': language} | |
| ) | |
| if result['success']: | |
| data = result['result'] | |
| return data.get('text', ''), f"Confidence: {data.get('confidence', 0):.2%}" | |
| else: | |
| return f"Error: {result['error']}", "" | |
| async def handle_summarize(file_path, max_length): | |
| if not file_path: | |
| return "Please select a file", {} | |
| result = await agent.manual_tool_call( | |
| 'pdf_summarize', | |
| {'file_path': file_path, 'max_length': max_length} | |
| ) | |
| if result['success']: | |
| data = result['result'] | |
| return data.get('summary', ''), data.get('metadata', {}) | |
| else: | |
| return f"Error: {result['error']}", {} | |
| async def handle_calendar(title, start, end, description, location): | |
| result = await agent.manual_tool_call( | |
| 'calendar_create_event', | |
| { | |
| 'title': title, | |
| 'start': start, | |
| 'end': end, | |
| 'description': description, | |
| 'location': location | |
| } | |
| ) | |
| if result['success']: | |
| data = result['result'] | |
| return data.get('output_path'), f"β Event created: {title}" | |
| else: | |
| return None, f"β Error: {result['error']}" | |
| async def handle_email(recipient, subject, context, tone): | |
| result = await agent.manual_tool_call( | |
| 'email_draft', | |
| { | |
| 'recipient': recipient, | |
| 'subject': subject, | |
| 'context': context, | |
| 'tone': tone | |
| } | |
| ) | |
| if result['success']: | |
| data = result['result'] | |
| return data.get('email_body', ''), data.get('output_path_html') | |
| else: | |
| return f"Error: {result['error']}", None | |
| async def handle_form_fill(template_file, data_json): | |
| if not template_file: | |
| return None, "Please upload a form template" | |
| try: | |
| data = json.loads(data_json) | |
| except: | |
| return None, "Invalid JSON data" | |
| result = await agent.manual_tool_call( | |
| 'form_fill', | |
| { | |
| 'template_path': template_file.name, | |
| 'data': data | |
| } | |
| ) | |
| if result['success']: | |
| res_data = result['result'] | |
| return res_data.get('output_path'), f"β Form filled: {len(res_data.get('fields_filled', []))} fields" | |
| else: | |
| return None, f"β Error: {result['error']}" | |
| async def handle_organize(file_list, strategy): | |
| if not file_list: | |
| return {"error": "No files to organize"} | |
| # Use upload directory | |
| result = await agent.manual_tool_call( | |
| 'file_organize', | |
| { | |
| 'directory': 'data/uploads', | |
| 'strategy': strategy | |
| } | |
| ) | |
| if result['success']: | |
| return result['result'] | |
| else: | |
| return {"error": result['error']} | |
| # Wire up events | |
| file_upload.change( | |
| fn=handle_file_upload, | |
| inputs=[file_upload], | |
| outputs=[files_list, uploaded_files_state, ocr_file_select, pdf_file_select] | |
| ) | |
| ocr_execute_btn.click( | |
| fn=handle_ocr, | |
| inputs=[ocr_file_select, ocr_language], | |
| outputs=[ocr_output, ocr_confidence] | |
| ) | |
| ocr_btn.click( | |
| fn=lambda: gr.Tabs.update(selected="π OCR"), | |
| outputs=[] | |
| ) | |
| summarize_execute_btn.click( | |
| fn=handle_summarize, | |
| inputs=[pdf_file_select, summary_length], | |
| outputs=[summary_output, pdf_metadata] | |
| ) | |
| calendar_execute_btn.click( | |
| fn=handle_calendar, | |
| inputs=[event_title, event_start, event_end, event_description, event_location], | |
| outputs=[calendar_output, calendar_status] | |
| ) | |
| email_execute_btn.click( | |
| fn=handle_email, | |
| inputs=[email_recipient, email_subject, email_context, email_tone], | |
| outputs=[email_output, email_download] | |
| ) | |
| form_execute_btn.click( | |
| fn=handle_form_fill, | |
| inputs=[form_template, form_data], | |
| outputs=[form_output, form_status] | |
| ) | |
| organize_execute_btn.click( | |
| fn=handle_organize, | |
| inputs=[uploaded_files_state, organize_strategy], | |
| outputs=[organize_output] | |
| ) |