Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -118,53 +118,73 @@ async def generate_image(request: ImageRequest):
|
|
118 |
- **Color Theme**: Follow the **{request.color_theme}** to set the mood and tone for the entire scene.
|
119 |
- Negative Prompt: Avoid grainy, blurry, or deformed outputs.
|
120 |
- **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.
|
121 |
-
- Write the prompt only in the output. Do not include anything else except the prompt. Do not write Generated Image Prompt: in the output.
|
122 |
-
|
123 |
-
Chat History:
|
124 |
-
{chat_history}
|
125 |
-
|
126 |
-
User Prompt:
|
127 |
-
{request.user_prompt}
|
128 |
-
|
129 |
-
Generated Image Prompt:
|
130 |
"""
|
131 |
refined_prompt = llm.invoke(prompt).content.strip()
|
|
|
|
|
132 |
collection.insert_one({"session_id": request.chat_id, "role": "user", "content": request.user_prompt})
|
133 |
collection.insert_one({"session_id": request.chat_id, "role": "ai", "content": refined_prompt})
|
134 |
|
135 |
-
#
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
# Run the request processing in a thread
|
150 |
future = executor.submit(process_request)
|
151 |
-
filepath = await run_in_threadpool(future.result)
|
152 |
-
|
153 |
-
# Load the image to return as a response
|
154 |
-
with open(filepath, "rb") as f:
|
155 |
-
image_data = f.read()
|
156 |
|
157 |
-
# Convert the file path into a downloadable format
|
158 |
-
file_url = f"/images/{os.path.basename(filepath)}"
|
159 |
-
|
160 |
-
# Return the response
|
161 |
return {
|
162 |
"status": "Image generated successfully",
|
163 |
"file_path": filepath,
|
164 |
-
"file_url": file_url,
|
165 |
-
"image": image_data,
|
166 |
}
|
167 |
|
|
|
168 |
@app.post("/upscale-image", response_model=dict)
|
169 |
async def upscale_image(image_url: str, background_tasks: BackgroundTasks):
|
170 |
def process_image():
|
|
|
118 |
- **Color Theme**: Follow the **{request.color_theme}** to set the mood and tone for the entire scene.
|
119 |
- Negative Prompt: Avoid grainy, blurry, or deformed outputs.
|
120 |
- **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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
"""
|
122 |
refined_prompt = llm.invoke(prompt).content.strip()
|
123 |
+
|
124 |
+
# Save the prompt in MongoDB
|
125 |
collection.insert_one({"session_id": request.chat_id, "role": "user", "content": request.user_prompt})
|
126 |
collection.insert_one({"session_id": request.chat_id, "role": "ai", "content": refined_prompt})
|
127 |
|
128 |
+
# API call to image generation service
|
129 |
+
url = "https://api.bfl.ml/v1/flux-pro-1.1"
|
130 |
+
headers = {
|
131 |
+
"accept": "application/json",
|
132 |
+
"x-key": "4f69d408-4979-4812-8ad2-ec6d232c9ddf",
|
133 |
+
"Content-Type": "application/json"
|
134 |
+
}
|
135 |
+
payload = {
|
136 |
+
"prompt": refined_prompt,
|
137 |
+
"width": 1024,
|
138 |
+
"height": 1024,
|
139 |
+
"guidance_scale": 1,
|
140 |
+
"num_inference_steps": 50,
|
141 |
+
"max_sequence_length": 512,
|
142 |
+
}
|
143 |
+
|
144 |
+
# Initial request to generate the image
|
145 |
+
response = requests.post(url, headers=headers, json=payload).json()
|
146 |
+
if "id" not in response:
|
147 |
+
raise HTTPException(status_code=500, detail="Error generating image: ID missing from response")
|
148 |
+
|
149 |
+
request_id = response["id"]
|
150 |
+
|
151 |
+
# Poll for the image result
|
152 |
+
while True:
|
153 |
+
time.sleep(0.5)
|
154 |
+
result = requests.get(
|
155 |
+
"https://api.bfl.ml/v1/get_result",
|
156 |
+
headers=headers,
|
157 |
+
params={"id": request_id},
|
158 |
+
).json()
|
159 |
+
|
160 |
+
if result["status"] == "Ready":
|
161 |
+
if "result" in result and "sample" in result["result"]:
|
162 |
+
image_url = result["result"]["sample"]
|
163 |
+
|
164 |
+
# Download the image
|
165 |
+
image_response = requests.get(image_url)
|
166 |
+
if image_response.status_code == 200:
|
167 |
+
img = Image.open(BytesIO(image_response.content))
|
168 |
+
filename = f"generated_{uuid.uuid4()}.png"
|
169 |
+
filepath = save_image_locally(img, filename)
|
170 |
+
return filepath
|
171 |
+
else:
|
172 |
+
raise HTTPException(status_code=500, detail="Failed to download the image")
|
173 |
+
else:
|
174 |
+
raise HTTPException(status_code=500, detail="Expected 'sample' key not found in the result")
|
175 |
+
elif result["status"] == "Error":
|
176 |
+
raise HTTPException(status_code=500, detail=f"Image generation failed: {result.get('error', 'Unknown error')}")
|
177 |
|
178 |
# Run the request processing in a thread
|
179 |
future = executor.submit(process_request)
|
180 |
+
filepath = await run_in_threadpool(future.result)
|
|
|
|
|
|
|
|
|
181 |
|
|
|
|
|
|
|
|
|
182 |
return {
|
183 |
"status": "Image generated successfully",
|
184 |
"file_path": filepath,
|
|
|
|
|
185 |
}
|
186 |
|
187 |
+
|
188 |
@app.post("/upscale-image", response_model=dict)
|
189 |
async def upscale_image(image_url: str, background_tasks: BackgroundTasks):
|
190 |
def process_image():
|