Spaces:
Running
Running
Update fluxai.py
Browse files
fluxai.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from fastapi import APIRouter, Depends
|
2 |
from fastapi.responses import StreamingResponse
|
3 |
from PIL import Image, ImageEnhance
|
|
|
4 |
import io
|
5 |
import requests
|
6 |
import os
|
@@ -55,12 +56,14 @@ def deduct_tokens_gpt(user_id, amount):
|
|
55 |
async def fluxai_image(payload: FluxAI):
|
56 |
if deduct_tokens_gpt(payload.user_id, amount=20):
|
57 |
try:
|
|
|
58 |
image_bytes = await schellwithflux(payload.args)
|
59 |
if image_bytes is None:
|
60 |
return SuccessResponse(
|
61 |
status="False",
|
62 |
randydev={"error": "Failed to generate an image"}
|
63 |
)
|
|
|
64 |
if payload.enhancer:
|
65 |
image = Image.open(io.BytesIO(image_bytes))
|
66 |
enhancer = ImageEnhance.Sharpness(image)
|
@@ -69,9 +72,10 @@ async def fluxai_image(payload: FluxAI):
|
|
69 |
image = enhancer.enhance(1.2)
|
70 |
enhancer = ImageEnhance.Color(image)
|
71 |
image = enhancer.enhance(1.1)
|
72 |
-
|
73 |
-
image.save(
|
74 |
-
|
|
|
75 |
else:
|
76 |
return StreamingResponse(io.BytesIO(image_bytes), media_type="image/jpeg")
|
77 |
|
|
|
1 |
from fastapi import APIRouter, Depends
|
2 |
from fastapi.responses import StreamingResponse
|
3 |
from PIL import Image, ImageEnhance
|
4 |
+
from fastapi import HTTPException
|
5 |
import io
|
6 |
import requests
|
7 |
import os
|
|
|
56 |
async def fluxai_image(payload: FluxAI):
|
57 |
if deduct_tokens_gpt(payload.user_id, amount=20):
|
58 |
try:
|
59 |
+
# Generate the image from the flux AI model
|
60 |
image_bytes = await schellwithflux(payload.args)
|
61 |
if image_bytes is None:
|
62 |
return SuccessResponse(
|
63 |
status="False",
|
64 |
randydev={"error": "Failed to generate an image"}
|
65 |
)
|
66 |
+
|
67 |
if payload.enhancer:
|
68 |
image = Image.open(io.BytesIO(image_bytes))
|
69 |
enhancer = ImageEnhance.Sharpness(image)
|
|
|
72 |
image = enhancer.enhance(1.2)
|
73 |
enhancer = ImageEnhance.Color(image)
|
74 |
image = enhancer.enhance(1.1)
|
75 |
+
enhanced_image_bytes = io.BytesIO()
|
76 |
+
image.save(enhanced_image_bytes, format="JPEG", quality=95)
|
77 |
+
enhanced_image_bytes.seek(0)
|
78 |
+
return StreamingResponse(enhanced_image_bytes, media_type="image/jpeg")
|
79 |
else:
|
80 |
return StreamingResponse(io.BytesIO(image_bytes), media_type="image/jpeg")
|
81 |
|