Spaces:
Sleeping
Sleeping
Dharini Baskaran
commited on
Commit
·
ce26e7b
1
Parent(s):
1063b4f
polish UI
Browse files
app.py
CHANGED
|
@@ -22,12 +22,9 @@ UPLOAD_DIR = "/tmp/uploads/"
|
|
| 22 |
JSON_DIR = "/tmp/results/"
|
| 23 |
OUTPUT_DIR = "/tmp/output/"
|
| 24 |
MODEL_DIR = os.path.join(BASE_DIR, "rcnn_model", "scripts")
|
| 25 |
-
logo_path = os.path.join(BASE_DIR, "public", "logo.png")
|
| 26 |
-
# model_path = os.path.join(OUTPUT_DIR, "model_final.pth")
|
| 27 |
-
# changine the model directory to the tmp directory
|
| 28 |
model_path = os.path.join(OUTPUT_DIR, "model_final.pth")
|
| 29 |
|
| 30 |
-
# Google Drive model
|
| 31 |
GOOGLE_DRIVE_FILE_ID = "1yr64AOgaYZPTcQzG6cxG6lWBENHR9qjW"
|
| 32 |
GDRIVE_URL = f"https://drive.google.com/uc?id={GOOGLE_DRIVE_FILE_ID}"
|
| 33 |
|
|
@@ -40,7 +37,6 @@ os.makedirs(OUTPUT_DIR, exist_ok=True)
|
|
| 40 |
if not os.path.exists(model_path):
|
| 41 |
print("🚀 Model file not found! Downloading...")
|
| 42 |
try:
|
| 43 |
-
# gdown.download(GDRIVE_URL, model_path, quiet=False)
|
| 44 |
gdown.download(GDRIVE_URL, model_path, quiet=False, use_cookies=False)
|
| 45 |
print("✅ Model downloaded successfully.")
|
| 46 |
except Exception as e:
|
|
@@ -59,55 +55,55 @@ cfg = write_config()
|
|
| 59 |
def predict(uploaded_file_path):
|
| 60 |
print("Inside Predict:" + uploaded_file_path)
|
| 61 |
if uploaded_file_path is None:
|
| 62 |
-
return None, None, "No file uploaded."
|
| 63 |
|
| 64 |
uploaded_path = os.path.join(UPLOAD_DIR, "input_image.png")
|
| 65 |
-
|
|
|
|
| 66 |
input_filename = "input_image.png"
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
output_image_name = input_filename.replace(".png", "_result.png").replace(".jpg", "_result.png").replace(".jpeg", "_result.png")
|
| 71 |
|
| 72 |
output_json_path = os.path.join(JSON_DIR, output_json_name)
|
| 73 |
output_image_path = os.path.join(JSON_DIR, output_image_name)
|
| 74 |
|
| 75 |
-
# print(f"Before calling main in app.py: {uploaded_file.name}")
|
| 76 |
-
# Run model
|
| 77 |
main(cfg, uploaded_file_path, output_json_name, output_image_name)
|
| 78 |
|
| 79 |
-
# Read outputs
|
| 80 |
result_img = Image.open(output_image_path) if os.path.exists(output_image_path) else None
|
| 81 |
result_json = {}
|
| 82 |
if os.path.exists(output_json_path):
|
| 83 |
with open(output_json_path, "r") as jf:
|
| 84 |
result_json = json.load(jf)
|
| 85 |
|
| 86 |
-
return result_img, json.dumps(result_json, indent=2),
|
| 87 |
|
| 88 |
# ==================================
|
| 89 |
# GRADIO UI
|
| 90 |
# ==================================
|
| 91 |
|
| 92 |
with gr.Blocks() as demo:
|
| 93 |
-
gr.
|
|
|
|
|
|
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
with gr.Column():
|
| 97 |
uploaded_file = gr.File(label="Upload your Floorplan Image", type="filepath")
|
| 98 |
-
|
| 99 |
run_button = gr.Button("Run Vectorizer 🔥")
|
| 100 |
|
| 101 |
with gr.Column():
|
| 102 |
output_image = gr.Image(label="🖼 Output Vectorized Image")
|
| 103 |
output_json = gr.JSON(label="🧾 Output JSON")
|
|
|
|
| 104 |
|
| 105 |
error_output = gr.Textbox(label="Error Message", visible=False)
|
| 106 |
|
| 107 |
run_button.click(
|
| 108 |
predict,
|
| 109 |
inputs=[uploaded_file],
|
| 110 |
-
outputs=[output_image, output_json,
|
| 111 |
)
|
| 112 |
|
| 113 |
-
demo.launch(server_name="0.0.0.0", server_port=7860,share=True)
|
|
|
|
| 22 |
JSON_DIR = "/tmp/results/"
|
| 23 |
OUTPUT_DIR = "/tmp/output/"
|
| 24 |
MODEL_DIR = os.path.join(BASE_DIR, "rcnn_model", "scripts")
|
| 25 |
+
logo_path = os.path.join(BASE_DIR, "public", "logo.png") # Your Inovonics image
|
|
|
|
|
|
|
| 26 |
model_path = os.path.join(OUTPUT_DIR, "model_final.pth")
|
| 27 |
|
|
|
|
| 28 |
GOOGLE_DRIVE_FILE_ID = "1yr64AOgaYZPTcQzG6cxG6lWBENHR9qjW"
|
| 29 |
GDRIVE_URL = f"https://drive.google.com/uc?id={GOOGLE_DRIVE_FILE_ID}"
|
| 30 |
|
|
|
|
| 37 |
if not os.path.exists(model_path):
|
| 38 |
print("🚀 Model file not found! Downloading...")
|
| 39 |
try:
|
|
|
|
| 40 |
gdown.download(GDRIVE_URL, model_path, quiet=False, use_cookies=False)
|
| 41 |
print("✅ Model downloaded successfully.")
|
| 42 |
except Exception as e:
|
|
|
|
| 55 |
def predict(uploaded_file_path):
|
| 56 |
print("Inside Predict:" + uploaded_file_path)
|
| 57 |
if uploaded_file_path is None:
|
| 58 |
+
return None, None, None, "No file uploaded."
|
| 59 |
|
| 60 |
uploaded_path = os.path.join(UPLOAD_DIR, "input_image.png")
|
| 61 |
+
shutil.copy(uploaded_file_path, uploaded_path)
|
| 62 |
+
|
| 63 |
input_filename = "input_image.png"
|
| 64 |
|
| 65 |
+
output_json_name = input_filename.replace(".png", "_result.json")
|
| 66 |
+
output_image_name = input_filename.replace(".png", "_result.png")
|
|
|
|
| 67 |
|
| 68 |
output_json_path = os.path.join(JSON_DIR, output_json_name)
|
| 69 |
output_image_path = os.path.join(JSON_DIR, output_image_name)
|
| 70 |
|
|
|
|
|
|
|
| 71 |
main(cfg, uploaded_file_path, output_json_name, output_image_name)
|
| 72 |
|
|
|
|
| 73 |
result_img = Image.open(output_image_path) if os.path.exists(output_image_path) else None
|
| 74 |
result_json = {}
|
| 75 |
if os.path.exists(output_json_path):
|
| 76 |
with open(output_json_path, "r") as jf:
|
| 77 |
result_json = json.load(jf)
|
| 78 |
|
| 79 |
+
return uploaded_path, result_img, json.dumps(result_json, indent=2), output_json_path
|
| 80 |
|
| 81 |
# ==================================
|
| 82 |
# GRADIO UI
|
| 83 |
# ==================================
|
| 84 |
|
| 85 |
with gr.Blocks() as demo:
|
| 86 |
+
with gr.Row():
|
| 87 |
+
gr.Image(logo_path, height=80, width=80)
|
| 88 |
+
gr.Markdown("<h1 style='text-align: center;'> Inovonics 2D Floorplan Vectorizer</h1>")
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
with gr.Column():
|
| 92 |
uploaded_file = gr.File(label="Upload your Floorplan Image", type="filepath")
|
| 93 |
+
uploaded_img_preview = gr.Image(label="📤 Uploaded Image", interactive=False)
|
| 94 |
run_button = gr.Button("Run Vectorizer 🔥")
|
| 95 |
|
| 96 |
with gr.Column():
|
| 97 |
output_image = gr.Image(label="🖼 Output Vectorized Image")
|
| 98 |
output_json = gr.JSON(label="🧾 Output JSON")
|
| 99 |
+
download_button = gr.File(label="⬇️ Download JSON")
|
| 100 |
|
| 101 |
error_output = gr.Textbox(label="Error Message", visible=False)
|
| 102 |
|
| 103 |
run_button.click(
|
| 104 |
predict,
|
| 105 |
inputs=[uploaded_file],
|
| 106 |
+
outputs=[uploaded_img_preview, output_image, output_json, download_button]
|
| 107 |
)
|
| 108 |
|
| 109 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|