Download app.py from imageomics/drexel_metadata_app: direct link, hf CLI and curl.
- Browser
- Download file 1.67 kB
-
https://huggingface.co/spaces/imageomics/drexel_metadata_app/resolve/938a85a5a3195763b22117ad5cc491c2a84109f7/app.py
- Command line
-
hf download hf://spaces/imageomics/drexel_metadata_app@938a85a5a3195763b22117ad5cc491c2a84109f7/app.py
-
curl -L -o app.py https://huggingface.co/spaces/imageomics/drexel_metadata_app/resolve/938a85a5a3195763b22117ad5cc491c2a84109f7/app.py
1.67 kB
| import os | |
| import tempfile | |
| import json | |
| import numpy as np | |
| import gradio as gr | |
| import cv2 | |
| from drexel_metadata.gen_metadata import gen_metadata | |
| from PIL import Image | |
| def create_temp_file_path(prefix, suffix): | |
| with tempfile.NamedTemporaryFile(prefix=prefix, suffix=suffix, delete=False) as tmpfile: | |
| return tmpfile.name | |
| def run_inference(markdown, input_img): | |
| # input_mg: NumPy array with the shape (width, height, 3) | |
| # Save input_mg as a temporary file | |
| tmpfile = create_temp_file_path(prefix="input_", suffix=".png") | |
| im = Image.fromarray(input_img) | |
| im.save(tmpfile) | |
| # Create temp filenames for output images | |
| visfname = create_temp_file_path(prefix="vis_", suffix=".png") | |
| maskfname = create_temp_file_path(prefix="mask_", suffix=".png") | |
| # Run inference | |
| result = gen_metadata(tmpfile, device='cpu', maskfname=maskfname, visfname=visfname) | |
| json_metadata = json.dumps(result) | |
| # Cleanup | |
| os.remove(tmpfile) | |
| return visfname, maskfname, json_metadata | |
| def read_app_header_markdown(): | |
| with open('app_header.md') as infile: | |
| return infile.read() | |
| dm_app = gr.Interface( | |
| fn=run_inference, | |
| # Input shows markdown explaining and app and a single image upload panel | |
| inputs=[ | |
| gr.Markdown(read_app_header_markdown()), | |
| gr.Image() | |
| ], | |
| # Output consists of a visualization image, a masked image, and JSON metadata | |
| outputs=[ | |
| gr.Image(label='visualization'), | |
| gr.Image(label='mask'), | |
| gr.JSON(label="JSON metadata") | |
| ], | |
| allow_flagging="never" # Do not save user's results or prompt for users to save the results | |
| ) | |
| dm_app.launch() | |