Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,11 @@ import os
|
|
3 |
import json
|
4 |
import time
|
5 |
import torch
|
6 |
-
import base64
|
7 |
from PIL import Image
|
8 |
from tqdm import tqdm
|
9 |
import gradio as gr
|
|
|
|
|
10 |
|
11 |
from safetensors.torch import save_file
|
12 |
from src.pipeline import FluxPipeline
|
@@ -52,54 +53,6 @@ def single_condition_generate_image(prompt, spatial_img, height, width, seed, co
|
|
52 |
clear_cache(pipe.transformer)
|
53 |
return image
|
54 |
|
55 |
-
# Proxy endpoint function to serve images directly from file path
|
56 |
-
def serve_file_from_path(file_path):
|
57 |
-
"""
|
58 |
-
Serve a file directly from a path
|
59 |
-
"""
|
60 |
-
if not file_path:
|
61 |
-
return None, "No path specified"
|
62 |
-
|
63 |
-
# Security check to prevent directory traversal attacks
|
64 |
-
if '..' in file_path:
|
65 |
-
return None, "Invalid path"
|
66 |
-
|
67 |
-
# Ensure path starts with a slash
|
68 |
-
if not file_path.startswith('/'):
|
69 |
-
file_path = '/' + file_path
|
70 |
-
|
71 |
-
try:
|
72 |
-
# Open and return the image
|
73 |
-
return Image.open(file_path), None
|
74 |
-
except Exception as e:
|
75 |
-
return None, f"Error accessing file: {str(e)}"
|
76 |
-
|
77 |
-
# Function to encode an image to base64
|
78 |
-
def get_base64_from_path(file_path):
|
79 |
-
"""
|
80 |
-
Get base64 encoded data from a file path
|
81 |
-
"""
|
82 |
-
if not file_path:
|
83 |
-
return {"error": "No path specified"}
|
84 |
-
|
85 |
-
# Security check to prevent directory traversal attacks
|
86 |
-
if '..' in file_path:
|
87 |
-
return {"error": "Invalid path"}
|
88 |
-
|
89 |
-
# Ensure path starts with a slash
|
90 |
-
if not file_path.startswith('/'):
|
91 |
-
file_path = '/' + file_path
|
92 |
-
|
93 |
-
try:
|
94 |
-
# Read the file and convert to base64
|
95 |
-
with open(file_path, 'rb') as f:
|
96 |
-
file_data = f.read()
|
97 |
-
|
98 |
-
base64_data = base64.b64encode(file_data).decode('utf-8')
|
99 |
-
return {"base64": base64_data, "mime_type": "image/webp"}
|
100 |
-
except Exception as e:
|
101 |
-
return {"error": str(e)}
|
102 |
-
|
103 |
# Define the Gradio interface components
|
104 |
control_types = ["Ghibli"]
|
105 |
|
@@ -148,42 +101,85 @@ with gr.Blocks() as demo:
|
|
148 |
label="Single Condition Examples"
|
149 |
)
|
150 |
|
151 |
-
# Add proxy functionality as Gradio endpoints
|
152 |
-
with gr.Tab("Proxy Service"):
|
153 |
-
gr.Markdown("## File Proxy Service")
|
154 |
-
gr.Markdown("This tab provides a way to access files from local paths.")
|
155 |
-
|
156 |
-
file_path_input = gr.Textbox(label="File Path", placeholder="Enter file path (e.g., /tmp/gradio/...)")
|
157 |
-
proxy_result = gr.Image(label="Retrieved Image", type="pil")
|
158 |
-
retrieve_btn = gr.Button("Retrieve Image")
|
159 |
-
|
160 |
-
# Connect the button to the function
|
161 |
-
retrieve_btn.click(
|
162 |
-
serve_file_from_path,
|
163 |
-
inputs=[file_path_input],
|
164 |
-
outputs=[proxy_result, gr.Textbox(label="Error")]
|
165 |
-
)
|
166 |
-
|
167 |
-
gr.Markdown("## Base64 Service")
|
168 |
-
gr.Markdown("Get a file as base64 encoded data.")
|
169 |
-
|
170 |
-
base64_path_input = gr.Textbox(label="File Path", placeholder="Enter file path (e.g., /tmp/gradio/...)")
|
171 |
-
base64_result = gr.JSON(label="Base64 Result")
|
172 |
-
base64_btn = gr.Button("Get Base64")
|
173 |
-
|
174 |
-
# Connect the button to the function
|
175 |
-
base64_btn.click(
|
176 |
-
get_base64_from_path,
|
177 |
-
inputs=[base64_path_input],
|
178 |
-
outputs=[base64_result]
|
179 |
-
)
|
180 |
-
|
181 |
# Link the buttons to the functions
|
182 |
single_generate_btn.click(
|
183 |
single_condition_generate_image,
|
184 |
inputs=[prompt, spatial_img, height, width, seed, control_type],
|
185 |
outputs=single_output_image
|
186 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
|
188 |
# Launch the Gradio app
|
189 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
3 |
import json
|
4 |
import time
|
5 |
import torch
|
|
|
6 |
from PIL import Image
|
7 |
from tqdm import tqdm
|
8 |
import gradio as gr
|
9 |
+
import base64
|
10 |
+
from flask import Flask, send_file, request, Response, jsonify
|
11 |
|
12 |
from safetensors.torch import save_file
|
13 |
from src.pipeline import FluxPipeline
|
|
|
53 |
clear_cache(pipe.transformer)
|
54 |
return image
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
# Define the Gradio interface components
|
57 |
control_types = ["Ghibli"]
|
58 |
|
|
|
101 |
label="Single Condition Examples"
|
102 |
)
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
# Link the buttons to the functions
|
105 |
single_generate_btn.click(
|
106 |
single_condition_generate_image,
|
107 |
inputs=[prompt, spatial_img, height, width, seed, control_type],
|
108 |
outputs=single_output_image
|
109 |
)
|
110 |
+
|
111 |
+
# Add proxy endpoints to access temporary files
|
112 |
+
with gr.Tab("API Endpoints"):
|
113 |
+
gr.Markdown("## Image Proxy Endpoints")
|
114 |
+
gr.Markdown("The following endpoints allow external access to generated images:")
|
115 |
+
gr.Markdown("- `/proxy-image?path=PATH` - Access a generated image directly")
|
116 |
+
gr.Markdown("- `/proxy-image-base64?path=PATH` - Get a generated image as base64")
|
117 |
+
gr.Markdown("These endpoints should only be used programmatically.")
|
118 |
+
|
119 |
+
# Create Flask app for proxy endpoints
|
120 |
+
# This allows Gradio to handle its routes while we add custom Flask routes
|
121 |
+
app = gr.flask_app
|
122 |
+
|
123 |
+
@app.route('/proxy-image')
|
124 |
+
def proxy_image():
|
125 |
+
"""
|
126 |
+
Proxies an image from a local file path
|
127 |
+
Example usage: /proxy-image?path=/tmp/gradio/4f461b7833afb048edcda4fd8968db08aeb352e871a5445721d63b11f9f489c7/image.webp
|
128 |
+
"""
|
129 |
+
file_path = request.args.get('path')
|
130 |
+
|
131 |
+
if not file_path:
|
132 |
+
return "No path specified", 400
|
133 |
+
|
134 |
+
# Security check to prevent directory traversal attacks
|
135 |
+
if '..' in file_path:
|
136 |
+
return "Invalid path", 400
|
137 |
+
|
138 |
+
# Ensure path starts with a slash
|
139 |
+
if not file_path.startswith('/'):
|
140 |
+
file_path = '/' + file_path
|
141 |
+
|
142 |
+
try:
|
143 |
+
# Read the file
|
144 |
+
with open(file_path, 'rb') as f:
|
145 |
+
file_data = f.read()
|
146 |
+
|
147 |
+
# Return the file
|
148 |
+
return Response(file_data, mimetype='image/webp')
|
149 |
+
except Exception as e:
|
150 |
+
return f"Error accessing file: {str(e)}", 404
|
151 |
+
|
152 |
+
@app.route('/proxy-image-base64')
|
153 |
+
def proxy_image_base64():
|
154 |
+
"""
|
155 |
+
Proxies an image from a local file path and returns it as base64
|
156 |
+
Example usage: /proxy-image-base64?path=/tmp/gradio/4f461b7833afb048edcda4fd8968db08aeb352e871a5445721d63b11f9f489c7/image.webp
|
157 |
+
"""
|
158 |
+
file_path = request.args.get('path')
|
159 |
+
|
160 |
+
if not file_path:
|
161 |
+
return jsonify({"error": "No path specified"}), 400
|
162 |
+
|
163 |
+
# Security check to prevent directory traversal attacks
|
164 |
+
if '..' in file_path:
|
165 |
+
return jsonify({"error": "Invalid path"}), 400
|
166 |
+
|
167 |
+
# Ensure path starts with a slash
|
168 |
+
if not file_path.startswith('/'):
|
169 |
+
file_path = '/' + file_path
|
170 |
+
|
171 |
+
try:
|
172 |
+
# Read the file
|
173 |
+
with open(file_path, 'rb') as f:
|
174 |
+
file_data = f.read()
|
175 |
+
|
176 |
+
# Convert to base64
|
177 |
+
base64_data = base64.b64encode(file_data).decode('utf-8')
|
178 |
+
|
179 |
+
# Return the base64 data
|
180 |
+
return jsonify({"base64": base64_data, "mime_type": "image/webp"})
|
181 |
+
except Exception as e:
|
182 |
+
return jsonify({"error": str(e)}), 404
|
183 |
|
184 |
# Launch the Gradio app
|
185 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
|