Spaces:
Sleeping
Sleeping
File size: 6,882 Bytes
0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 6ef5a61 0d91ffe 6ef5a61 0d91ffe 294a450 0d91ffe 503a15e 6ef5a61 0d91ffe 7e1939b 6ef5a61 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 7e1939b 503a15e 6ef5a61 503a15e 0d91ffe 503a15e | 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | """UI components and logic for the Ada Assistant Gradio interface."""
import gradio as gr
class AdaAssistantUI:
"""Ada Assistant Gradio UI class that manages the web interface."""
def __init__(self, project_handler):
"""Initialize the UI with a project handler."""
self.project_handler = project_handler
def extract_project(self, file):
"""Extract uploaded project"""
if file is None:
return "No file uploaded"
try:
# Extract zip file
extracted_path = self.project_handler.unzip_project(file.name)
return extracted_path
except Exception as e:
return f"Error processing file: {str(e)}"
def handle_zip_upload(self, zip_file):
"""Handle zip file upload and extraction"""
if zip_file is None:
return None, "No zip file uploaded"
try:
# Extract the project
extracted_path = self.extract_project(zip_file)
return extracted_path, f"Project extracted to: {extracted_path}"
except Exception as e:
return None, f"Error processing zip file: {str(e)}"
def handle_file_selection(self, selected_file):
"""Handle file selection from FileExplorer"""
if not selected_file:
return "", "", "", gr.Markdown(visible=False), gr.Code(visible=False), gr.Code(visible=False)
# Check if it's an Ada file
if not (selected_file.endswith('.ads') or selected_file.endswith('.adb')):
return "", "", "", gr.Markdown(visible=False), gr.Code(visible=False), gr.Code(visible=False)
try:
# Read the Ada file content
with open(selected_file, 'r', encoding='utf-8') as f:
ada_code = f.read()
# Process Ada code: analyze, convert, and generate tests
analysis_result, python_code, unit_tests = self.project_handler.process_ada_file(ada_code)
# Return markdown analysis, python code, and unit tests
return analysis_result, python_code, unit_tests, gr.Markdown(visible=True), gr.Code(visible=True), gr.Code(visible=True)
except Exception as e:
error_markdown = f"# Error\n\nError processing file: {str(e)}"
return error_markdown, "", "", gr.Markdown(visible=True), gr.Code(visible=False), gr.Code(visible=False)
@staticmethod
def update_explorer(path):
"""Update file explorer when path changes"""
if path:
return gr.FileExplorer(root_dir=path, visible=True)
return gr.FileExplorer(visible=False)
def handle_convert_download(self, extracted_path):
"""Handle convert and download with access to project_handler"""
zip_path = self.project_handler.convert_and_download_project(extracted_path)
if zip_path:
return gr.File(value=zip_path, visible=True)
return gr.File(visible=False)
def create_interface(self):
"""Create the main Gradio interface"""
with gr.Blocks(title="Ada Assistant - Project Analyzer") as app:
gr.Markdown("# Ada Assistant - Project Analyzer")
gr.Markdown("Upload a zip file containing your Ada project to extract and explore the code structure.")
with gr.Row():
with gr.Column(scale=1):
# Zip file upload with drag & drop
zip_input = gr.File(
label="Upload Project (ZIP)",
file_types=[".zip"],
type="filepath"
)
# Project info display
project_info = gr.Textbox(
label="Project Status",
value="No project loaded",
interactive=False
)
with gr.Column(scale=2):
# File explorer for extracted project
file_explorer = gr.FileExplorer(
label="Project Structure",
visible=False,
file_count="single"
)
with gr.Row():
with gr.Column(scale=1):
# Analysis results display
analysis_results = gr.Markdown(
label="Business Logic Analysis",
value="Select file to view analysis",
visible=True
)
with gr.Column(scale=1):
# Python code display
python_code_display = gr.Code(
label="Converted Python Code",
language="python",
lines=20,
visible=True
)
with gr.Column(scale=1):
# Unit tests display
unit_tests_display = gr.Code(
label="Generated Unit Tests",
language="python",
lines=20,
visible=True
)
with gr.Row():
# Convert & Download button
convert_download_btn = gr.Button("Convert & Download")
# Download component for the zip file
download_file = gr.File(label="Download Converted Project", visible=False)
# Hidden state to store extracted path
extracted_path_state = gr.State()
# Handle zip file upload
zip_input.upload(
fn=self.handle_zip_upload,
inputs=[zip_input],
outputs=[extracted_path_state, project_info]
)
# Update file explorer when path changes
extracted_path_state.change(
fn=self.update_explorer,
inputs=[extracted_path_state],
outputs=[file_explorer]
)
# Handle file selection
file_explorer.change(
fn=self.handle_file_selection,
inputs=[file_explorer],
outputs=[analysis_results, python_code_display, unit_tests_display, analysis_results, python_code_display, unit_tests_display]
)
# Handle convert & download button click
convert_download_btn.click(
fn=self.handle_convert_download,
inputs=[extracted_path_state],
outputs=[download_file]
)
return app |