Spaces:
Running
Running
Upload fluxai.py
Browse files
fluxai.py
CHANGED
@@ -10,6 +10,10 @@ from pydantic import BaseModel
|
|
10 |
from pymongo import MongoClient
|
11 |
from models import *
|
12 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
13 |
|
14 |
class FluxAI(BaseModel):
|
15 |
user_id: int
|
@@ -24,6 +28,7 @@ router = APIRouter()
|
|
24 |
load_dotenv()
|
25 |
MONGO_URL = os.environ["MONGO_URL"]
|
26 |
HUGGING_TOKEN = os.environ["HUGGING_TOKEN"]
|
|
|
27 |
|
28 |
client_mongo = MongoClient(MONGO_URL)
|
29 |
db = client_mongo["tiktokbot"]
|
@@ -85,17 +90,15 @@ async def mistralai_(payload: MistralAI):
|
|
85 |
)
|
86 |
|
87 |
@router.post("/akeno/fluxai", response_model=SuccessResponse, responses={422: {"model": SuccessResponse}})
|
88 |
-
async def fluxai_image(payload: FluxAI):
|
89 |
if deduct_tokens_gpt(payload.user_id, amount=20):
|
90 |
try:
|
91 |
-
# Generate the image from the flux AI model
|
92 |
image_bytes = await schellwithflux(payload.args)
|
93 |
if image_bytes is None:
|
94 |
return SuccessResponse(
|
95 |
status="False",
|
96 |
randydev={"error": "Failed to generate an image"}
|
97 |
)
|
98 |
-
|
99 |
if payload.auto_enhancer:
|
100 |
with Image.open(io.BytesIO(image_bytes)) as image:
|
101 |
enhancer = ImageEnhance.Sharpness(image)
|
@@ -107,7 +110,20 @@ async def fluxai_image(payload: FluxAI):
|
|
107 |
enhanced_image_bytes = io.BytesIO()
|
108 |
image.save(enhanced_image_bytes, format="JPEG", quality=95)
|
109 |
enhanced_image_bytes.seek(0)
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
else:
|
112 |
return StreamingResponse(io.BytesIO(image_bytes), media_type="image/jpeg")
|
113 |
|
|
|
10 |
from pymongo import MongoClient
|
11 |
from models import *
|
12 |
from huggingface_hub import InferenceClient
|
13 |
+
from fastapi import UploadFile
|
14 |
+
from fastapi.responses import JSONResponse
|
15 |
+
import uuid
|
16 |
+
from RyuzakiLib import GeminiLatest
|
17 |
|
18 |
class FluxAI(BaseModel):
|
19 |
user_id: int
|
|
|
28 |
load_dotenv()
|
29 |
MONGO_URL = os.environ["MONGO_URL"]
|
30 |
HUGGING_TOKEN = os.environ["HUGGING_TOKEN"]
|
31 |
+
GOOGLE_API_KEY = os.environ["GOOGLE_API_KEY"]
|
32 |
|
33 |
client_mongo = MongoClient(MONGO_URL)
|
34 |
db = client_mongo["tiktokbot"]
|
|
|
90 |
)
|
91 |
|
92 |
@router.post("/akeno/fluxai", response_model=SuccessResponse, responses={422: {"model": SuccessResponse}})
|
93 |
+
async def fluxai_image(payload: FluxAI, file: UploadFile):
|
94 |
if deduct_tokens_gpt(payload.user_id, amount=20):
|
95 |
try:
|
|
|
96 |
image_bytes = await schellwithflux(payload.args)
|
97 |
if image_bytes is None:
|
98 |
return SuccessResponse(
|
99 |
status="False",
|
100 |
randydev={"error": "Failed to generate an image"}
|
101 |
)
|
|
|
102 |
if payload.auto_enhancer:
|
103 |
with Image.open(io.BytesIO(image_bytes)) as image:
|
104 |
enhancer = ImageEnhance.Sharpness(image)
|
|
|
110 |
enhanced_image_bytes = io.BytesIO()
|
111 |
image.save(enhanced_image_bytes, format="JPEG", quality=95)
|
112 |
enhanced_image_bytes.seek(0)
|
113 |
+
ext = file.filename.split(".")[-1]
|
114 |
+
unique_filename = f"{uuid.uuid4().hex}.{ext}"
|
115 |
+
file_path = os.path.join("uploads", unique_filename)
|
116 |
+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
117 |
+
with open(file_path, "wb") as f:
|
118 |
+
f.write(enhanced_image_bytes.getvalue())
|
119 |
+
example_test = "Accurately identify the baked good in the image and provide an appropriate and recipe consistent with your analysis."
|
120 |
+
x = GeminiLatest(api_keys=GOOGLE_API_KEY)
|
121 |
+
response = x.get_response_image(example_test, file_path)
|
122 |
+
url = f"https://randydev-ryuzaki-api.hf.space/{file_path}"
|
123 |
+
return SuccessResponse(
|
124 |
+
status="True",
|
125 |
+
randydev={"url": url, "caption": response}
|
126 |
+
)
|
127 |
else:
|
128 |
return StreamingResponse(io.BytesIO(image_bytes), media_type="image/jpeg")
|
129 |
|