#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Gradio Application for Code Review Agent This module defines the Gradio web interface for the Code Review Agent. It creates a professional UI with components for repository input, language selection, progress tracking, and results display. """ import os import gradio as gr import logging from src.ui.components.repo_input import create_repo_input from src.ui.components.language_selector import create_language_selector from src.ui.components.progress_tracker import create_progress_tracker from src.ui.components.results_dashboard import create_results_dashboard from src.ui.components.export_manager import create_export_manager from src.ui.styles.themes import get_theme logger = logging.getLogger(__name__) def create_gradio_app(agent_manager): """ Create and configure the Gradio application. Args: agent_manager: The AgentManager instance that handles the business logic. Returns: gr.Blocks: The configured Gradio application. """ # Load custom CSS css_path = os.path.join(os.path.dirname(__file__), 'styles', 'custom.css') with open(css_path, 'r') as f: custom_css = f.read() # Create the Gradio app with custom theme theme = get_theme() with gr.Blocks(css=custom_css, theme=theme, title="Code Review Agent") as app: gr.Markdown( """ # 🔍 Professional Code Review Agent Upload a GitHub repository URL and get comprehensive code analysis with actionable recommendations. """ ) with gr.Row(): with gr.Column(scale=3): # Repository input component repo_url, github_token, submit_btn = create_repo_input() # Language selector component selected_languages = create_language_selector() with gr.Column(scale=1): # Information panel gr.Markdown( """ ### 📋 Features - Multi-language support (15+ languages) - Security vulnerability detection - Performance analysis - Code quality metrics - Actionable recommendations """ ) # Progress tracker component with gr.Group(visible=False) as progress_group: gr.Markdown("### ⏳ Analysis Progress") overall_progress, status_message, step_progress = create_progress_tracker() # Results dashboard component results_dashboard = create_results_dashboard() # Export options component export_buttons = create_export_manager() # Set up event handlers def start_review_with_progress(repo_url, github_token, selected_languages): # We can't use Group objects as outputs, so we'll handle visibility differently # First, make progress group visible and results dashboard invisible progress_group.visible = True results_dashboard.visible = False # Start review process progress_components = (progress_group, overall_progress, status_message, step_progress) try: _, _, _, results = agent_manager.start_review(repo_url, github_token, selected_languages, progress_components) # Hide progress group and show results dashboard when done progress_group.visible = False results_dashboard.visible = True return [ 0, # reset overall_progress value "*Analysis complete!*", # status_message value results # results dashboard with data ] except Exception as e: # Show error message but keep progress group visible return [ 0, # reset overall_progress value f"*Error: {str(e)}*", # error message results_dashboard # keep current dashboard ] submit_btn.click( fn=start_review_with_progress, inputs=[repo_url, github_token, selected_languages], outputs=[overall_progress, status_message, results_dashboard] ) for export_btn, export_format in export_buttons: export_btn.click( fn=agent_manager.export_report, inputs=[results_dashboard, export_format], outputs=[] ) # Add WebSocket for real-time updates app.queue() return app