create app.py boilerplate
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import tempfile
|
4 |
+
import zipfile
|
5 |
+
from your_adain_module import run_adain # Your actual AdaIN implementation
|
6 |
+
|
7 |
+
def process_adain_api(files, style_strength=1.0):
|
8 |
+
'''API endpoint for AdaIN processing.'''
|
9 |
+
if not files:
|
10 |
+
return None, "No files uploaded"
|
11 |
+
|
12 |
+
# Create temporary directories
|
13 |
+
temp_dir = tempfile.mkdtemp()
|
14 |
+
input_dir = os.path.join(temp_dir, "input")
|
15 |
+
output_dir = os.path.join(temp_dir, "output")
|
16 |
+
|
17 |
+
os.makedirs(input_dir, exist_ok=True)
|
18 |
+
os.makedirs(output_dir, exist_ok=True)
|
19 |
+
|
20 |
+
# Save uploaded files
|
21 |
+
for file in files:
|
22 |
+
if file is not None:
|
23 |
+
shutil.copy(file.name, input_dir)
|
24 |
+
|
25 |
+
try:
|
26 |
+
# Your internal style images path
|
27 |
+
style_images_path = "path/to/your/style/images"
|
28 |
+
|
29 |
+
# Run AdaIN
|
30 |
+
result_path = run_adain(input_dir, style_images_path, output_dir, style_strength)
|
31 |
+
|
32 |
+
# Create zip file
|
33 |
+
zip_path = os.path.join(temp_dir, "style_transfer_results.zip")
|
34 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
35 |
+
for file in os.listdir(result_path):
|
36 |
+
if file.lower().endswith(('.jpg', '.jpeg', '.png')):
|
37 |
+
zipf.write(os.path.join(result_path, file), file)
|
38 |
+
|
39 |
+
return zip_path, f"Style transfer completed with strength {style_strength}"
|
40 |
+
|
41 |
+
except Exception as e:
|
42 |
+
return None, f"Error: {str(e)}"
|
43 |
+
|
44 |
+
# Create interface with API endpoint
|
45 |
+
with gr.Blocks() as adain_demo:
|
46 |
+
gr.Markdown("# AdaIN Style Transfer Service")
|
47 |
+
|
48 |
+
with gr.Row():
|
49 |
+
with gr.Column():
|
50 |
+
files_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
|
51 |
+
strength_input = gr.Slider(0.0, 2.0, 1.0, step=0.1, label="Style Strength")
|
52 |
+
process_btn = gr.Button("Process", variant="primary")
|
53 |
+
|
54 |
+
with gr.Column():
|
55 |
+
download_output = gr.File(label="Download Results")
|
56 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
57 |
+
|
58 |
+
process_btn.click(
|
59 |
+
fn=process_adain_api,
|
60 |
+
inputs=[files_input, strength_input],
|
61 |
+
outputs=[download_output, status_output],
|
62 |
+
api_name="adain_process" # This creates the API endpoint
|
63 |
+
)
|
64 |
+
|
65 |
+
adain_demo.launch()
|