ChristianQ commited on
Commit
4e73e35
·
1 Parent(s): 7e79f99

Fix Attempt for coding module

Browse files
Files changed (1) hide show
  1. app.py +26 -27
app.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import uuid
3
  import shutil
4
  import traceback
5
-
6
  import gradio as gr
7
  from fastapi import FastAPI, File, UploadFile
8
  from fastapi.responses import JSONResponse
@@ -31,12 +31,10 @@ OUTPUT_DIR = "./output"
31
  os.makedirs(TEMP_DIR, exist_ok=True)
32
  os.makedirs(OUTPUT_DIR, exist_ok=True)
33
 
34
-
35
  @api.get("/")
36
  def health_check():
37
  return {"status": "ok"}
38
 
39
-
40
  @api.post("/process-wireframe")
41
  async def process_wireframe_api(image: UploadFile = File(...)):
42
  file_id = str(uuid.uuid4())
@@ -61,7 +59,14 @@ async def process_wireframe_api(image: UploadFile = File(...)):
61
 
62
  json_path = results.get("json_path")
63
 
64
- #Generate styled HTML using CodingModule
 
 
 
 
 
 
 
65
  try:
66
  html_generator = HTMLGenerator(json_path)
67
  html_filename = f"{file_id}_styled.html"
@@ -71,11 +76,18 @@ async def process_wireframe_api(image: UploadFile = File(...)):
71
  print(f"Warning: Could not generate styled HTML: {e}")
72
  html_path = results.get("html_path")
73
 
 
 
 
 
 
 
74
  return {
75
  "success": True,
76
- "json_path": json_path,
77
- "html_path": html_path,
78
- "total_elements": len(results["normalized_elements"])
 
79
  }
80
 
81
  except Exception as e:
@@ -102,18 +114,6 @@ async def serve_output_file(filename: str):
102
  return FileResponse(file_path)
103
  return JSONResponse(status_code=404, content={"error": "File not Found"})
104
 
105
- # # Legacy endpoint for just for backward compatibility
106
- # @api.get("/view-html/{file_id}")
107
- # async def view_html(file_id: str):
108
- # html_path = os.path.join(OUTPUT_DIR, f"{file_id}.html")
109
- # if os.path.exists(html_path):
110
- # return FileResponse(html_path, media_type="text/html")
111
- # # Try with _styled suffix
112
- # html_path = os.path.join(OUTPUT_DIR, f"{file_id}_styled.html")
113
- # if os.path.exists(html_path):
114
- # return FileResponse(html_path, media_type="text/html")
115
- # return JSONResponse(status_code=404, content={"error": "File not found"})
116
-
117
  # -----------------------------------------------------------------------------
118
  # GRADIO (for Hugging Face UI)
119
  # -----------------------------------------------------------------------------
@@ -216,14 +216,14 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Wireframe Layout Normalizer") as d
216
  gr.Markdown("""
217
  ---
218
  ### How to use:
219
- 1. *Upload* a wireframe image (hand-drawn or digital)
220
- 2. *Click* "Process Wireframe" and wait for processing
221
- 3. *Download* the JSON file for structured data
222
- 4. *Download* the HTML file to see a styled preview
223
 
224
  ### Output Files:
225
- - *JSON*: Contains all detected elements with positions, types, and grid data
226
- - *HTML*: Beautiful, responsive preview with modern styling
227
 
228
  ### Technical Details:
229
  - Model: TensorFlow-based object detection
@@ -231,13 +231,12 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Wireframe Layout Normalizer") as d
231
  - Supported Elements: Buttons, Checkboxes, Text Fields, Images, Paragraphs, Text, Navbars
232
  """)
233
 
234
-
235
  # -----------------------------------------------------------------------------
236
  # ENTRY POINT (THIS IS IMPORTANT)
237
  # -----------------------------------------------------------------------------
238
  app = gr.mount_gradio_app(api, demo, path="/")
239
 
240
- if __name__ == "__main__":
241
  import uvicorn
242
  # For local testing
243
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
2
  import uuid
3
  import shutil
4
  import traceback
5
+ import json
6
  import gradio as gr
7
  from fastapi import FastAPI, File, UploadFile
8
  from fastapi.responses import JSONResponse
 
31
  os.makedirs(TEMP_DIR, exist_ok=True)
32
  os.makedirs(OUTPUT_DIR, exist_ok=True)
33
 
 
34
  @api.get("/")
35
  def health_check():
36
  return {"status": "ok"}
37
 
 
38
  @api.post("/process-wireframe")
39
  async def process_wireframe_api(image: UploadFile = File(...)):
40
  file_id = str(uuid.uuid4())
 
59
 
60
  json_path = results.get("json_path")
61
 
62
+ # Read the JSON file content and return it directly
63
+ json_content = None
64
+ if json_path and os.path.exists(json_path):
65
+ with open(json_path, "r") as f:
66
+ json_content = json.load(f)
67
+
68
+ # Generate styled HTML using CodingModule
69
+ html_path = None
70
  try:
71
  html_generator = HTMLGenerator(json_path)
72
  html_filename = f"{file_id}_styled.html"
 
76
  print(f"Warning: Could not generate styled HTML: {e}")
77
  html_path = results.get("html_path")
78
 
79
+ # Read the HTML file content
80
+ html_content = None
81
+ if html_path and os.path.exists(html_path):
82
+ with open(html_path, "r", encoding="utf-8") as f:
83
+ html_content = f.read()
84
+
85
  return {
86
  "success": True,
87
+ "json_data": json_content, # Return actual JSON content instead of path
88
+ "html_content": html_content, # Return actual HTML content instead of path
89
+ "total_elements": len(results["normalized_elements"]),
90
+ "normalized_elements": results.get("normalized_elements", []) # Include normalized elements
91
  }
92
 
93
  except Exception as e:
 
114
  return FileResponse(file_path)
115
  return JSONResponse(status_code=404, content={"error": "File not Found"})
116
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  # -----------------------------------------------------------------------------
118
  # GRADIO (for Hugging Face UI)
119
  # -----------------------------------------------------------------------------
 
216
  gr.Markdown("""
217
  ---
218
  ### How to use:
219
+ 1. Upload a wireframe image (hand-drawn or digital)
220
+ 2. Click "Process Wireframe" and wait for processing
221
+ 3. Download the JSON file for structured data
222
+ 4. Download the HTML file to see a styled preview
223
 
224
  ### Output Files:
225
+ - JSON: Contains all detected elements with positions, types, and grid data
226
+ - HTML: Beautiful, responsive preview with modern styling
227
 
228
  ### Technical Details:
229
  - Model: TensorFlow-based object detection
 
231
  - Supported Elements: Buttons, Checkboxes, Text Fields, Images, Paragraphs, Text, Navbars
232
  """)
233
 
 
234
  # -----------------------------------------------------------------------------
235
  # ENTRY POINT (THIS IS IMPORTANT)
236
  # -----------------------------------------------------------------------------
237
  app = gr.mount_gradio_app(api, demo, path="/")
238
 
239
+ if __name__ == "_main_":
240
  import uvicorn
241
  # For local testing
242
  uvicorn.run(app, host="0.0.0.0", port=7860)