Delete api.py
Browse files
api.py
DELETED
@@ -1,66 +0,0 @@
|
|
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')}"}
|
19 |
-
|
20 |
-
def query(payload):
|
21 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
22 |
-
if response.status_code != 200:
|
23 |
-
raise HTTPException(status_code=response.status_code, detail="API call failed")
|
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,
|
31 |
-
"parameters": {"seed": random_seed}
|
32 |
-
}
|
33 |
-
image_bytes = query(payload)
|
34 |
-
return Image.open(io.BytesIO(image_bytes))
|
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})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|