tidalove commited on
Commit
0125293
·
verified ·
1 Parent(s): 28a1103

create app.py boilerplate

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import tempfile
4
+ import json
5
+ import zipfile
6
+ from your_yolox_module import run_yolox # Your actual YOLOX implementation
7
+ from your_crop_module import run_square_crop # Your cropping implementation
8
+
9
+ def process_yolox_api(files):
10
+ '''API endpoint for YOLOX processing.'''
11
+ if not files:
12
+ return [], "No files uploaded"
13
+
14
+ # Create temporary directories
15
+ temp_dir = tempfile.mkdtemp()
16
+ input_dir = os.path.join(temp_dir, "input")
17
+ output_dir = os.path.join(temp_dir, "output")
18
+ cropped_dir = os.path.join(temp_dir, "cropped")
19
+
20
+ os.makedirs(input_dir, exist_ok=True)
21
+ os.makedirs(output_dir, exist_ok=True)
22
+ os.makedirs(cropped_dir, exist_ok=True)
23
+
24
+ # Save uploaded files
25
+ for file in files:
26
+ if file is not None:
27
+ shutil.copy(file.name, input_dir)
28
+
29
+ try:
30
+ # Run YOLOX
31
+ coco_json_path = run_yolox(input_dir, output_dir)
32
+
33
+ # Run cropping
34
+ cropped_paths = run_square_crop(input_dir, coco_json_path, cropped_dir)
35
+
36
+ # Create zip file
37
+ zip_path = os.path.join(temp_dir, "cropped_results.zip")
38
+ with zipfile.ZipFile(zip_path, 'w') as zipf:
39
+ for img_path in cropped_paths:
40
+ if os.path.exists(img_path):
41
+ zipf.write(img_path, os.path.basename(img_path))
42
+
43
+ return zip_path, cropped_paths, f"Processed {len(cropped_paths)} images"
44
+
45
+ except Exception as e:
46
+ return None, [], f"Error: {str(e)}"
47
+
48
+ # Create interface with API endpoint
49
+ with gr.Blocks() as yolox_demo:
50
+ gr.Markdown("# YOLOX Auto-Cropping Service")
51
+
52
+ with gr.Row():
53
+ with gr.Column():
54
+ files_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
55
+ process_btn = gr.Button("Process", variant="primary")
56
+
57
+ with gr.Column():
58
+ gallery_output = gr.Gallery(label="Cropped Images", columns=3, rows=2)
59
+ download_output = gr.File(label="Download Results")
60
+ status_output = gr.Textbox(label="Status", interactive=False)
61
+
62
+ process_btn.click(
63
+ fn=process_yolox_api,
64
+ inputs=[files_input],
65
+ outputs=[download_output, gallery_output, status_output],
66
+ api_name="yolox_process" # This creates the API endpoint
67
+ )
68
+
69
+ yolox_demo.launch()