Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from fastapi import FastAPI, HTTPException, BackgroundTasks, Depends
|
2 |
from pydantic import BaseModel
|
|
|
3 |
from fastapi.concurrency import run_in_threadpool
|
4 |
from fastapi.middleware.cors import CORSMiddleware
|
5 |
import uuid
|
@@ -42,6 +43,9 @@ chat_sessions = {}
|
|
42 |
image_storage_dir = tempfile.mkdtemp()
|
43 |
print(f"Temporary directory for images: {image_storage_dir}")
|
44 |
|
|
|
|
|
|
|
45 |
# Dictionary to store images temporarily
|
46 |
image_store = {}
|
47 |
|
@@ -142,7 +146,6 @@ async def generate_image(request: ImageRequest):
|
|
142 |
"max_sequence_length": 512,
|
143 |
}
|
144 |
|
145 |
-
# Initial request to generate the image
|
146 |
response = requests.post(url, headers=headers, json=payload).json()
|
147 |
if "id" not in response:
|
148 |
raise HTTPException(status_code=500, detail="Error generating image: ID missing from response")
|
@@ -168,7 +171,10 @@ async def generate_image(request: ImageRequest):
|
|
168 |
img = Image.open(BytesIO(image_response.content))
|
169 |
filename = f"generated_{uuid.uuid4()}.png"
|
170 |
filepath = save_image_locally(img, filename)
|
171 |
-
|
|
|
|
|
|
|
172 |
else:
|
173 |
raise HTTPException(status_code=500, detail="Failed to download the image")
|
174 |
else:
|
@@ -176,16 +182,15 @@ async def generate_image(request: ImageRequest):
|
|
176 |
elif result["status"] == "Error":
|
177 |
raise HTTPException(status_code=500, detail=f"Image generation failed: {result.get('error', 'Unknown error')}")
|
178 |
|
179 |
-
# Run the request processing in a thread
|
180 |
future = executor.submit(process_request)
|
181 |
-
filepath = await run_in_threadpool(future.result)
|
182 |
|
183 |
return {
|
184 |
"status": "Image generated successfully",
|
185 |
"file_path": filepath,
|
|
|
186 |
}
|
187 |
|
188 |
-
|
189 |
@app.post("/upscale-image", response_model=dict)
|
190 |
async def upscale_image(image_url: str, background_tasks: BackgroundTasks):
|
191 |
def process_image():
|
|
|
1 |
from fastapi import FastAPI, HTTPException, BackgroundTasks, Depends
|
2 |
from pydantic import BaseModel
|
3 |
+
from fastapi.staticfiles import StaticFiles
|
4 |
from fastapi.concurrency import run_in_threadpool
|
5 |
from fastapi.middleware.cors import CORSMiddleware
|
6 |
import uuid
|
|
|
43 |
image_storage_dir = tempfile.mkdtemp()
|
44 |
print(f"Temporary directory for images: {image_storage_dir}")
|
45 |
|
46 |
+
# Serve the temporary image directory as a static file directory
|
47 |
+
app.mount("/images", StaticFiles(directory=image_storage_dir), name="images")
|
48 |
+
|
49 |
# Dictionary to store images temporarily
|
50 |
image_store = {}
|
51 |
|
|
|
146 |
"max_sequence_length": 512,
|
147 |
}
|
148 |
|
|
|
149 |
response = requests.post(url, headers=headers, json=payload).json()
|
150 |
if "id" not in response:
|
151 |
raise HTTPException(status_code=500, detail="Error generating image: ID missing from response")
|
|
|
171 |
img = Image.open(BytesIO(image_response.content))
|
172 |
filename = f"generated_{uuid.uuid4()}.png"
|
173 |
filepath = save_image_locally(img, filename)
|
174 |
+
|
175 |
+
# Generate a URL for the image
|
176 |
+
file_url = f"/images/{filename}"
|
177 |
+
return filepath, file_url
|
178 |
else:
|
179 |
raise HTTPException(status_code=500, detail="Failed to download the image")
|
180 |
else:
|
|
|
182 |
elif result["status"] == "Error":
|
183 |
raise HTTPException(status_code=500, detail=f"Image generation failed: {result.get('error', 'Unknown error')}")
|
184 |
|
|
|
185 |
future = executor.submit(process_request)
|
186 |
+
filepath, file_url = await run_in_threadpool(future.result)
|
187 |
|
188 |
return {
|
189 |
"status": "Image generated successfully",
|
190 |
"file_path": filepath,
|
191 |
+
"file_url": file_url,
|
192 |
}
|
193 |
|
|
|
194 |
@app.post("/upscale-image", response_model=dict)
|
195 |
async def upscale_image(image_url: str, background_tasks: BackgroundTasks):
|
196 |
def process_image():
|