File size: 4,836 Bytes
88d205f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/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