File size: 8,556 Bytes
df5fc1f
db93af0
3555d2b
69138eb
df5fc1f
 
4dd92b9
df5fc1f
 
 
 
 
 
 
db93af0
 
df5fc1f
544fc6a
 
 
 
db93af0
 
 
df5fc1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ba2980
df5fc1f
 
 
db93af0
3555d2b
 
 
df5fc1f
 
db93af0
df5fc1f
 
873d584
df5fc1f
 
 
 
 
 
873d584
 
 
 
 
 
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3555d2b
 
 
 
d205093
 
 
 
 
 
df5fc1f
d95a2ab
3555d2b
d95a2ab
 
 
 
3555d2b
d95a2ab
df5fc1f
9abdb46
 
 
 
857b344
873d584
 
 
857b344
 
873d584
6a44c93
 
 
 
873d584
857b344
 
 
 
6a44c93
873d584
df5fc1f
9abdb46
857b344
 
df5fc1f
1308b02
 
9abdb46
857b344
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from fastapi import FastAPI, HTTPException, BackgroundTasks, Depends
from pydantic import BaseModel
from fastapi.staticfiles import StaticFiles
from fastapi.concurrency import run_in_threadpool
from fastapi.middleware.cors import CORSMiddleware
import uuid
import time
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
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}")

# Serve the temporary image directory as a static file directory
app.mount("/images", StaticFiles(directory=image_storage_dir), name="images")

# Dictionary to store images temporarily
image_store = {}

@app.on_event("startup")
async def startup():
    global llm, aura_sr
    llm = ChatGroq(
        model="llama-3.3-70b-versatile",
        temperature=0.7,
        max_tokens=1024,
        api_key="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",
    )
    # Ensure AuraSR is initialized properly
    try:
        aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
    except Exception as e:
        print(f"Error initializing AuraSR: {e}")
        aura_sr = None


@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,
        }

        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)

                        # Generate a URL for the image
                        file_url = f"/images/{filename}"
                        return filepath, file_url
                    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')}")

    future = executor.submit(process_request)
    filepath, file_url = await run_in_threadpool(future.result)

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

class UpscaleRequest(BaseModel):
    image_url: str


def process_image(image_url):
    if aura_sr is None:
        raise RuntimeError("aura_sr is None. Ensure it's initialized during startup.")

    response = requests.get(image_url)
    img = Image.open(BytesIO(response.content))

    try:
        upscaled_image = aura_sr.upscale_4x_overlapped(img)
    except Exception as e:
        raise RuntimeError(f"Error during upscaling: {str(e)}")
    
    filename = f"upscaled_{uuid.uuid4()}.png"
    filepath = save_image_locally(upscaled_image, filename)
    return filepath



@app.post("/upscale-image", response_model=dict)
async def upscale_image(request: UpscaleRequest, background_tasks: BackgroundTasks):
    # Add the task directly to the background
    background_tasks.add_task(process_image, request.image_url)
    return {"status": "Processing"}




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