File size: 7,867 Bytes
df5fc1f
db93af0
69138eb
df5fc1f
 
 
 
 
 
 
 
 
db93af0
 
df5fc1f
544fc6a
 
 
 
db93af0
 
 
df5fc1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ba2980
df5fc1f
 
 
db93af0
df5fc1f
 
db93af0
df5fc1f
 
 
 
 
 
 
 
 
 
1ba2980
df5fc1f
 
 
 
1ba2980
df5fc1f
 
db93af0
 
 
 
df5fc1f
db93af0
 
 
 
 
1ba2980
df5fc1f
1ba2980
df5fc1f
 
db93af0
df5fc1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d95a2ab
df5fc1f
 
 
 
 
 
f151981
 
 
df5fc1f
f151981
 
 
 
 
df5fc1f
f151981
df5fc1f
 
d205093
 
df5fc1f
 
1ba2980
d205093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df5fc1f
d95a2ab
 
d205093
d95a2ab
 
 
 
 
df5fc1f
d205093
df5fc1f
 
 
 
 
 
 
 
 
 
 
 
 
1308b02
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from fastapi import FastAPI, HTTPException, BackgroundTasks, Depends
from pydantic import BaseModel
from fastapi.concurrency import run_in_threadpool
from fastapi.middleware.cors import CORSMiddleware
import uuid
import tempfile
from concurrent.futures import ThreadPoolExecutor
from pymongo import MongoClient
from urllib.parse import quote_plus
from langchain_groq import ChatGroq
from aura_sr import AuraSR
from io import BytesIO
from PIL import Image
import requests
import os
import os

# Set the Hugging Face cache directory to a writable location
os.environ['HF_HOME'] = '/tmp/huggingface_cache'

app = FastAPI()

# Middleware for CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# Globals
executor = ThreadPoolExecutor(max_workers=10)
llm = None
upscale_model = None
client = MongoClient(f"mongodb+srv://hammad:{quote_plus('momimaad@123')}@cluster0.2a9yu.mongodb.net/")
db = client["Flux"]
collection = db["chat_histories"]
chat_sessions = {}

# Use a temporary directory for storing images
image_storage_dir = tempfile.mkdtemp()
print(f"Temporary directory for images: {image_storage_dir}")

# Dictionary to store images temporarily
image_store = {}

@app.on_event("startup")
async def startup():
    global llm, upscale_model
    llm = ChatGroq(
        model="llama-3.3-70b-versatile",
        temperature=0.7,
        max_tokens=1024,
        api_key="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",
    )
    upscale_model = AuraSR.from_pretrained("fal/AuraSR-v2")

@app.on_event("shutdown")
def shutdown():
    client.close()
    executor.shutdown()

# Pydantic models
class ImageRequest(BaseModel):
    subject: str
    style: str
    color_theme: str
    elements: str
    color_mode: str
    lighting_conditions: str
    framing_style: str
    material_details: str
    text: str
    background_details: str
    user_prompt: str
    chat_id: str

# Helper Functions
def generate_chat_id():
    chat_id = str(uuid.uuid4())
    chat_sessions[chat_id] = collection
    return chat_id

def get_chat_history(chat_id):
    messages = collection.find({"session_id": chat_id})
    return "\n".join(
        f"User: {msg['content']}" if msg['role'] == "user" else f"AI: {msg['content']}"
        for msg in messages
    )

def save_image_locally(image, filename):
    filepath = os.path.join(image_storage_dir, filename)
    image.save(filepath, format="PNG")
    return filepath

# Endpoints
@app.post("/new-chat", response_model=dict)
async def new_chat():
    chat_id = generate_chat_id()
    return {"chat_id": chat_id}

@app.post("/generate-image", response_model=dict)
async def generate_image(request: ImageRequest):
    def process_request():
        chat_history = get_chat_history(request.chat_id)
        prompt = f"""
        You are a professional assistant responsible for crafting a clear and visually compelling prompt for an image generation model. Your task is to generate a high-quality prompt for creating both the **main subject** and the **background** of the image.

        Image Specifications:
        - **Subject**: Focus on **{request.subject}**, highlighting its defining features, expressions, and textures.
        - **Style**: Emphasize the **{request.style}**, capturing its key characteristics.
        - **Background**: Create a background with **{request.background_details}** that complements and enhances the subject. Ensure it aligns with the color theme and overall composition.
        - **Camera and Lighting**:
          - Lighting: Apply **{request.lighting_conditions}**, emphasizing depth, highlights, and shadows to accentuate the subject and harmonize the background.
        - **Framing**: Use a **{request.framing_style}** to enhance the composition around both the subject and the background.
        - **Materials**: Highlight textures like **{request.material_details}**, with realistic details and natural imperfections on the subject and background.
        - **Key Elements**: Include **{request.elements}** to enrich the subject’s details and add cohesive elements to the background.
        - **Color Theme**: Follow the **{request.color_theme}** to set the mood and tone for the entire scene.
        - Negative Prompt: Avoid grainy, blurry, or deformed outputs.
        - **Text to Include in Image**: Clearly display the text **"{request.text}"** as part of the composition (e.g., on a card, badge, or banner) attached to the subject in a realistic and contextually appropriate way.
        """
        refined_prompt = llm.invoke(prompt).content.strip()

        # Save the prompt in MongoDB
        collection.insert_one({"session_id": request.chat_id, "role": "user", "content": request.user_prompt})
        collection.insert_one({"session_id": request.chat_id, "role": "ai", "content": refined_prompt})

        # API call to image generation service
        url = "https://api.bfl.ml/v1/flux-pro-1.1"
        headers = {
            "accept": "application/json",
            "x-key": "4f69d408-4979-4812-8ad2-ec6d232c9ddf",
            "Content-Type": "application/json"
        }
        payload = {
            "prompt": refined_prompt,
            "width": 1024,
            "height": 1024,
            "guidance_scale": 1,
            "num_inference_steps": 50,
            "max_sequence_length": 512,
        }

        # Initial request to generate the image
        response = requests.post(url, headers=headers, json=payload).json()
        if "id" not in response:
            raise HTTPException(status_code=500, detail="Error generating image: ID missing from response")

        request_id = response["id"]

        # Poll for the image result
        while True:
            time.sleep(0.5)
            result = requests.get(
                "https://api.bfl.ml/v1/get_result",
                headers=headers,
                params={"id": request_id},
            ).json()

            if result["status"] == "Ready":
                if "result" in result and "sample" in result["result"]:
                    image_url = result["result"]["sample"]

                    # Download the image
                    image_response = requests.get(image_url)
                    if image_response.status_code == 200:
                        img = Image.open(BytesIO(image_response.content))
                        filename = f"generated_{uuid.uuid4()}.png"
                        filepath = save_image_locally(img, filename)
                        return filepath
                    else:
                        raise HTTPException(status_code=500, detail="Failed to download the image")
                else:
                    raise HTTPException(status_code=500, detail="Expected 'sample' key not found in the result")
            elif result["status"] == "Error":
                raise HTTPException(status_code=500, detail=f"Image generation failed: {result.get('error', 'Unknown error')}")

    # Run the request processing in a thread
    future = executor.submit(process_request)
    filepath = await run_in_threadpool(future.result)

    return {
        "status": "Image generated successfully",
        "file_path": filepath,
    }


@app.post("/upscale-image", response_model=dict)
async def upscale_image(image_url: str, background_tasks: BackgroundTasks):
    def process_image():
        response = requests.get(image_url)
        img = Image.open(BytesIO(response.content))
        upscaled_image = upscale_model.upscale_4x_overlapped(img)
        filename = f"upscaled_{uuid.uuid4()}.png"
        filepath = save_image_locally(upscaled_image, filename)
        return filepath

    task = executor.submit(process_image)
    background_tasks.add_task(task)
    return {"status": "Processing"}


@app.get("/")
async def root():
    return {"message": "API is up and running!"}