Update api.py
Browse files
api.py
CHANGED
@@ -1,12 +1,18 @@
|
|
1 |
import io
|
2 |
import random
|
3 |
import os
|
|
|
4 |
import requests
|
5 |
from fastapi import FastAPI, HTTPException
|
6 |
-
from fastapi.responses import
|
7 |
from fastapi.concurrency import run_in_threadpool
|
|
|
8 |
from PIL import Image
|
9 |
|
|
|
|
|
|
|
|
|
10 |
# API Configuration for Hugging Face
|
11 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
12 |
headers = {"Authorization": f"Bearer {os.getenv('HF')}"}
|
@@ -18,7 +24,7 @@ def query(payload):
|
|
18 |
return response.content
|
19 |
|
20 |
def generate_image(prompt: str) -> Image.Image:
|
21 |
-
# Use a random seed so
|
22 |
random_seed = random.randint(0, 999999)
|
23 |
payload = {
|
24 |
"inputs": prompt,
|
@@ -29,19 +35,32 @@ def generate_image(prompt: str) -> Image.Image:
|
|
29 |
|
30 |
app = FastAPI()
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
@app.get("/")
|
33 |
async def get_generated_image(text: str):
|
34 |
"""
|
35 |
-
API endpoint that
|
36 |
-
|
37 |
-
|
38 |
"""
|
39 |
try:
|
40 |
-
# Run the
|
41 |
image = await run_in_threadpool(generate_image, text)
|
42 |
except Exception as e:
|
43 |
raise HTTPException(status_code=500, detail=str(e))
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import io
|
2 |
import random
|
3 |
import os
|
4 |
+
import uuid
|
5 |
import requests
|
6 |
from fastapi import FastAPI, HTTPException
|
7 |
+
from fastapi.responses import JSONResponse
|
8 |
from fastapi.concurrency import run_in_threadpool
|
9 |
+
from fastapi.staticfiles import StaticFiles
|
10 |
from PIL import Image
|
11 |
|
12 |
+
# Directory to store generated images
|
13 |
+
IMAGE_DIR = "generated_images"
|
14 |
+
os.makedirs(IMAGE_DIR, exist_ok=True)
|
15 |
+
|
16 |
# API Configuration for Hugging Face
|
17 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
18 |
headers = {"Authorization": f"Bearer {os.getenv('HF')}"}
|
|
|
24 |
return response.content
|
25 |
|
26 |
def generate_image(prompt: str) -> Image.Image:
|
27 |
+
# Use a random seed so identical prompts produce different images.
|
28 |
random_seed = random.randint(0, 999999)
|
29 |
payload = {
|
30 |
"inputs": prompt,
|
|
|
35 |
|
36 |
app = FastAPI()
|
37 |
|
38 |
+
# Mount the static file directory to serve generated images.
|
39 |
+
app.mount("/generated_images", StaticFiles(directory=IMAGE_DIR), name="generated_images")
|
40 |
+
|
41 |
+
# Define your base URL (adjust as needed or set via environment variable)
|
42 |
+
BASE_URL = os.getenv("BASE_URL", "https://bowozzz-gen.hf.space")
|
43 |
+
|
44 |
@app.get("/")
|
45 |
async def get_generated_image(text: str):
|
46 |
"""
|
47 |
+
API endpoint that generates an image for the given text prompt,
|
48 |
+
saves it, and returns a link to download the image.
|
49 |
+
Example: https://bowozzz-gen.hf.space/api?text=jokowi
|
50 |
"""
|
51 |
try:
|
52 |
+
# Run the blocking image generation in a threadpool
|
53 |
image = await run_in_threadpool(generate_image, text)
|
54 |
except Exception as e:
|
55 |
raise HTTPException(status_code=500, detail=str(e))
|
56 |
+
|
57 |
+
# Generate a unique filename and save the image to the static folder
|
58 |
+
filename = f"{uuid.uuid4().hex}.png"
|
59 |
+
file_path = os.path.join(IMAGE_DIR, filename)
|
60 |
+
image.save(file_path, format="PNG")
|
61 |
+
|
62 |
+
# Construct the download URL using the base URL and static folder mount
|
63 |
+
download_url = f"{BASE_URL}/generated_images/{filename}"
|
64 |
+
|
65 |
+
# Return the download URL in a JSON response
|
66 |
+
return JSONResponse(content={"download_url": download_url})
|