Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,8 @@ import torch
|
|
| 6 |
from PIL import Image
|
| 7 |
from tqdm import tqdm
|
| 8 |
import gradio as gr
|
|
|
|
|
|
|
| 9 |
|
| 10 |
from safetensors.torch import save_file
|
| 11 |
from src.pipeline import FluxPipeline
|
|
@@ -105,6 +107,79 @@ with gr.Blocks() as demo:
|
|
| 105 |
inputs=[prompt, spatial_img, height, width, seed, control_type],
|
| 106 |
outputs=single_output_image
|
| 107 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
# Launch the Gradio app
|
| 110 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 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
|
|
|
|
| 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)
|