File size: 2,114 Bytes
7c3f1c6
 
9743107
7c3f1c6
9743107
 
 
7c3f1c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9743107
 
7c3f1c6
 
 
 
 
9743107
 
 
 
 
7c3f1c6
9743107
 
 
 
 
 
 
 
 
 
 
 
 
 
7c3f1c6
 
 
9743107
7c3f1c6
9743107
7c3f1c6
9743107
7c3f1c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from fastapi import FastAPI, Query, HTTPException
from gradio_client import Client
import requests
import os

app = FastAPI()

HF_MODEL = "yanze/PuLID-FLUX"
BASE_URL = "https://yanze-pulid-flux.hf.space/file="  # Ensure this is correct
TEMP_DIR = "/tmp"  # Folder to store temp image downloads

def download_image(image_url: str) -> str:
    """Downloads image from URL and saves it locally."""
    try:
        response = requests.get(image_url, stream=True)
        if response.status_code != 200:
            raise HTTPException(status_code=400, detail="Failed to download image")

        filename = os.path.join(TEMP_DIR, "input_image.jpg")
        with open(filename, "wb") as f:
            for chunk in response.iter_content(1024):
                f.write(chunk)
        return filename
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error downloading image: {str(e)}")

@app.get("/generate")
def generate_image(prompt: str, image_url: str):
    client = Client(HF_MODEL)
    
    # Download image from URL first
    image_path = download_image(image_url)

    width, height = 1080, 1080  # Instagram format (fixed size)

    try:
        result = client.predict(
            prompt=prompt,
            id_image=image_path,  # File path instead of URL
            start_step=0,
            guidance=4,
            seed="-1",
            true_cfg=1,
            width=width,
            height=height,
            num_steps=20,
            id_weight=1,
            neg_prompt="bad quality, worst quality, text, signature, watermark, extra limbs",
            timestep_to_start_cfg=1,
            max_sequence_length=128,
            api_name="/generate_image"
        )

        if not result or not isinstance(result, list) or len(result) == 0:
            raise HTTPException(status_code=500, detail="Model did not return a valid response")

        file_path = result[0]
        full_url = f"{BASE_URL}{file_path}"
        return {"image_url": full_url}
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Generation failed: {str(e)}")