JawadBenali commited on
Commit
7aa1b7c
·
1 Parent(s): eab4832

Initial deployment - ArchitectAI MCP

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import ast
3
  import logging
4
  import io
 
5
  import json
6
  import sys
7
  from pathlib import Path
@@ -148,6 +149,89 @@ def process_zip_upload(zip_path, progress=gr.Progress()):
148
  shutil.rmtree(temp_dir)
149
  return f"❌ Error: {e}", None, gr.update(visible=True, value="❌ Failed")
150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  # --- HELPER FUNCTIONS ---
152
  def render_plantuml(puml_text: str):
153
  """Render PlantUML to image"""
 
2
  import ast
3
  import logging
4
  import io
5
+ import os
6
  import json
7
  import sys
8
  from pathlib import Path
 
149
  shutil.rmtree(temp_dir)
150
  return f"❌ Error: {e}", None, gr.update(visible=True, value="❌ Failed")
151
 
152
+
153
+ def run_modal_refactoring_zip(zip_path, file_path, instruction, test_path=None, progress=gr.Progress()):
154
+ """TAB 4: Modal execution with ZIP upload"""
155
+ if not zip_path:
156
+ return "⚠️ Please upload a ZIP file.", gr.update(visible=False)
157
+ if not file_path or not instruction:
158
+ return "⚠️ Please provide file path and instruction.", gr.update(visible=False)
159
+
160
+ try:
161
+ # Create temp directories
162
+ temp_extract = tempfile.mkdtemp()
163
+ temp_output = tempfile.mkdtemp()
164
+
165
+ progress(0.1, desc="Extracting project...")
166
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
167
+ zip_ref.extractall(temp_extract)
168
+
169
+ progress(0.3, desc="Preparing for Modal...")
170
+
171
+ # Read target file
172
+ target_file = Path(temp_extract) / file_path
173
+ if not target_file.exists():
174
+ shutil.rmtree(temp_extract)
175
+ shutil.rmtree(temp_output)
176
+ return f"❌ File not found: {file_path}", gr.update(visible=False)
177
+
178
+ original_code = target_file.read_text(encoding='utf-8')
179
+
180
+ # Read test file if provided
181
+ test_code = None
182
+ if test_path and test_path.strip():
183
+ test_file = Path(temp_extract) / test_path
184
+ if test_file.exists():
185
+ test_code = test_file.read_text(encoding='utf-8')
186
+
187
+ progress(0.5, desc="Executing in Modal sandbox...")
188
+
189
+ # Call Modal function (import from server.py)
190
+ try:
191
+ from server import apply_refactoring_safely
192
+
193
+ # Execute refactoring in Modal
194
+ result = apply_refactoring_safely(file_path, instruction, test_path)
195
+
196
+ progress(0.8, desc="Packaging results...")
197
+
198
+ # If successful, create output ZIP
199
+ if "✅" in result and "PASSED" in result:
200
+ # Copy entire project to output
201
+ shutil.copytree(temp_extract, Path(temp_output) / "project")
202
+
203
+ # Create ZIP
204
+ output_zip_path = Path(temp_output) / "refactored_project.zip"
205
+ with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
206
+ for root, dirs, files in os.walk(Path(temp_output) / "project"):
207
+ for file in files:
208
+ file_path_full = Path(root) / file
209
+ arcname = file_path_full.relative_to(Path(temp_output) / "project")
210
+ zipf.write(file_path_full, arcname)
211
+
212
+ progress(1.0, desc="Complete!")
213
+
214
+ # Cleanup extract dir
215
+ shutil.rmtree(temp_extract)
216
+
217
+ return result, gr.update(visible=True, value=str(output_zip_path))
218
+ else:
219
+ # Failed - don't provide download
220
+ shutil.rmtree(temp_extract)
221
+ shutil.rmtree(temp_output)
222
+ return result, gr.update(visible=False)
223
+
224
+ except ImportError:
225
+ shutil.rmtree(temp_extract)
226
+ shutil.rmtree(temp_output)
227
+ return "⚠️ Modal integration not configured. Set up Modal credentials in Space secrets.", gr.update(visible=False)
228
+
229
+ except Exception as e:
230
+ if 'temp_extract' in locals():
231
+ shutil.rmtree(temp_extract)
232
+ if 'temp_output' in locals():
233
+ shutil.rmtree(temp_output)
234
+ return f"❌ Execution failed: {str(e)}", gr.update(visible=False)
235
  # --- HELPER FUNCTIONS ---
236
  def render_plantuml(puml_text: str):
237
  """Render PlantUML to image"""