Spaces:
Running
Running
| import os | |
| os.environ["GRADIO_API_FORCE_3"] = "1" | |
| import gradio as gr | |
| import sys | |
| import json | |
| import shutil | |
| import gdown | |
| from PIL import Image | |
| print("Gradio App Starting...") | |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| # Paths | |
| UPLOAD_DIR = "/tmp/uploads/" | |
| JSON_DIR = "/tmp/results/" | |
| OUTPUT_DIR = "/tmp/output/" | |
| MODEL_DIR = os.path.join(BASE_DIR, "rcnn_model", "scripts") | |
| logo_path = os.path.join(BASE_DIR, "public", "logo.png") | |
| model_path = os.path.join(OUTPUT_DIR, "model_final.pth") | |
| # Google Drive model | |
| GOOGLE_DRIVE_FILE_ID = "1yr64AOgaYZPTcQzG6cxG6lWBENHR9qjW" | |
| GDRIVE_URL = f"https://drive.google.com/uc?id={GOOGLE_DRIVE_FILE_ID}" | |
| # Create folders | |
| os.makedirs(UPLOAD_DIR, exist_ok=True) | |
| os.makedirs(JSON_DIR, exist_ok=True) | |
| os.makedirs(OUTPUT_DIR, exist_ok=True) | |
| # Download model if missing | |
| if not os.path.exists(model_path): | |
| print("Model file not found! Downloading...") | |
| try: | |
| gdown.download(GDRIVE_URL, model_path, quiet=False, use_cookies=False) | |
| print("Model downloaded successfully.") | |
| except Exception as e: | |
| print(f"Failed to download model: {e}") | |
| # Import model | |
| sys.path.append(MODEL_DIR) | |
| from rcnn_model.scripts.rcnn_run import main, write_config | |
| cfg = write_config() | |
| def clear_outputs_on_upload(uploaded_file_path): | |
| if uploaded_file_path is None: | |
| return None, None, None, None, None | |
| return gr.update(value=uploaded_file_path), None, None, None, None | |
| def show_uploaded_image(path): | |
| if path is None: | |
| return None | |
| try: | |
| return Image.open(path) | |
| except: | |
| return None | |
| def predict(uploaded_file_path): | |
| if uploaded_file_path is None: | |
| return None, None, "No file uploaded.", None | |
| # Save uploaded file to tmp folder | |
| uploaded_path = os.path.join(UPLOAD_DIR, "input_image.png") | |
| shutil.copy(uploaded_file_path, uploaded_path) | |
| input_filename = "input_image.png" | |
| output_json_name = input_filename.replace(".png", "_result.json").replace(".jpg", "_result.json").replace(".jpeg", "_result.json") | |
| output_image_name = input_filename.replace(".png", "_result.png").replace(".jpg", "_result.png").replace(".jpeg", "_result.png") | |
| output_json_path = os.path.join(JSON_DIR, output_json_name) | |
| output_image_path = os.path.join(JSON_DIR, output_image_name) | |
| # Run model | |
| main(cfg, uploaded_path, output_json_name, output_image_name) | |
| # Read outputs | |
| result_img = Image.open(output_image_path) if os.path.exists(output_image_path) else None | |
| result_json = {} | |
| if os.path.exists(output_json_path): | |
| with open(output_json_path, "r") as jf: | |
| result_json = json.load(jf) | |
| # Save JSON to file for download | |
| download_json_path = os.path.join(JSON_DIR, "output.json") | |
| with open(download_json_path, "w") as f: | |
| json.dump(result_json, f, indent=2) | |
| return result_img, json.dumps(result_json, indent=2), None, download_json_path, uploaded_path | |
| with gr.Blocks() as demo: | |
| with gr.Row(): | |
| gr.Markdown( | |
| f""" | |
| <div style='display: flex; align-items: center; justify-content: center;'> | |
| <h1>Inovonics 2D Floorplan Vectorizer</h1> | |
| </div> | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| uploaded_file = gr.File(label="Upload your Floorplan Image", type="filepath") | |
| uploaded_image_display = gr.Image(label="Uploaded Image", visible=True) | |
| run_button = gr.Button("Run Vectorizer") | |
| with gr.Column(): | |
| output_image = gr.Image(label="Output Vectorized Image") | |
| download_button = gr.File(label="Download JSON", visible=True) | |
| output_json = gr.JSON(label="Output JSON") | |
| error_output = gr.Textbox(label="Error Message", visible=False) | |
| uploaded_file.change( | |
| lambda path: ( | |
| gr.update(value=path), | |
| None, None, None, show_uploaded_image(path) | |
| ), | |
| inputs=[uploaded_file], | |
| outputs=[uploaded_file, output_image, output_json, download_button, uploaded_image_display] | |
| ) | |
| run_button.click( | |
| lambda x: (x, gr.update(interactive=False)), | |
| inputs=[uploaded_file], | |
| outputs=[uploaded_file, run_button], | |
| ).then( | |
| predict, | |
| inputs=[uploaded_file], | |
| outputs=[output_image, output_json, error_output, download_button, uploaded_image_display] | |
| ).then( | |
| lambda: gr.update(interactive=True), | |
| None, | |
| [run_button], | |
| ) | |
| demo.launch(server_name="0.0.0.0", server_port=7860, share=True) |