Spaces:
Running
Running
Upload fluxai.py
Browse files
fluxai.py
CHANGED
@@ -17,6 +17,7 @@ from RyuzakiLib import GeminiLatest
|
|
17 |
|
18 |
class FluxAI(BaseModel):
|
19 |
user_id: int
|
|
|
20 |
args: str
|
21 |
auto_enhancer: bool = False
|
22 |
|
@@ -104,52 +105,67 @@ async def mistralai_(payload: MistralAI):
|
|
104 |
randydev={"error": f"An error occurred: {str(e)}"}
|
105 |
)
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
@router.post("/akeno/fluxai", response_model=SuccessResponse, responses={422: {"model": SuccessResponse}})
|
108 |
async def fluxai_image(payload: FluxAI, file: UploadFile):
|
109 |
if deduct_tokens_gpt(payload.user_id, amount=20):
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
return SuccessResponse(
|
114 |
status="False",
|
115 |
-
randydev={"error": "
|
116 |
-
)
|
117 |
-
if payload.auto_enhancer:
|
118 |
-
with Image.open(io.BytesIO(image_bytes)) as image:
|
119 |
-
enhancer = ImageEnhance.Sharpness(image)
|
120 |
-
image = enhancer.enhance(1.5)
|
121 |
-
enhancer = ImageEnhance.Contrast(image)
|
122 |
-
image = enhancer.enhance(1.2)
|
123 |
-
enhancer = ImageEnhance.Color(image)
|
124 |
-
image = enhancer.enhance(1.1)
|
125 |
-
enhanced_image_bytes = io.BytesIO()
|
126 |
-
image.save(enhanced_image_bytes, format="JPEG", quality=95)
|
127 |
-
enhanced_image_bytes.seek(0)
|
128 |
-
ext = file.filename.split(".")[-1]
|
129 |
-
unique_filename = f"{uuid.uuid4().hex}.{ext}"
|
130 |
-
file_path = os.path.join("uploads", unique_filename)
|
131 |
-
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
132 |
-
with open(file_path, "wb") as f:
|
133 |
-
f.write(enhanced_image_bytes.getvalue())
|
134 |
-
example_test = "Accurately identify the baked good in the image and provide an appropriate and recipe consistent with your analysis."
|
135 |
-
x = GeminiLatest(api_keys=GOOGLE_API_KEY)
|
136 |
-
response = x.get_response_image(example_test, file_path)
|
137 |
-
url = f"https://randydev-ryuzaki-api.hf.space/{file_path}"
|
138 |
-
return SuccessResponse(
|
139 |
-
status="True",
|
140 |
-
randydev={"url": url, "caption": response}
|
141 |
)
|
142 |
-
|
143 |
-
return StreamingResponse(io.BytesIO(image_bytes), media_type="image/jpeg")
|
144 |
-
|
145 |
-
except Exception as e:
|
146 |
return SuccessResponse(
|
147 |
status="False",
|
148 |
-
randydev={"error": f"
|
149 |
)
|
150 |
else:
|
151 |
tokens = get_user_tokens_gpt(payload.user_id)
|
152 |
return SuccessResponse(
|
153 |
status="False",
|
154 |
-
randydev={"error": f"Not enough tokens. Current tokens: {tokens}."}
|
155 |
)
|
|
|
17 |
|
18 |
class FluxAI(BaseModel):
|
19 |
user_id: int
|
20 |
+
api_key: str
|
21 |
args: str
|
22 |
auto_enhancer: bool = False
|
23 |
|
|
|
105 |
randydev={"error": f"An error occurred: {str(e)}"}
|
106 |
)
|
107 |
|
108 |
+
def get_all_api_keys():
|
109 |
+
user = collection.find({})
|
110 |
+
api_keys = []
|
111 |
+
for x in user:
|
112 |
+
api_key = x.get("ryuzaki_api_key")
|
113 |
+
if api_key:
|
114 |
+
api_keys.append(api_key)
|
115 |
+
return api_keys
|
116 |
+
|
117 |
@router.post("/akeno/fluxai", response_model=SuccessResponse, responses={422: {"model": SuccessResponse}})
|
118 |
async def fluxai_image(payload: FluxAI, file: UploadFile):
|
119 |
if deduct_tokens_gpt(payload.user_id, amount=20):
|
120 |
+
USERS_API_KEYS = get_all_api_keys()
|
121 |
+
if payload.api_key in USERS_API_KEYS:
|
122 |
+
try:
|
123 |
+
image_bytes = await schellwithflux(payload.args)
|
124 |
+
if image_bytes is None:
|
125 |
+
return SuccessResponse(
|
126 |
+
status="False",
|
127 |
+
randydev={"error": "Failed to generate an image"}
|
128 |
+
)
|
129 |
+
if payload.auto_enhancer:
|
130 |
+
with Image.open(io.BytesIO(image_bytes)) as image:
|
131 |
+
enhancer = ImageEnhance.Sharpness(image)
|
132 |
+
image = enhancer.enhance(1.5)
|
133 |
+
enhancer = ImageEnhance.Contrast(image)
|
134 |
+
image = enhancer.enhance(1.2)
|
135 |
+
enhancer = ImageEnhance.Color(image)
|
136 |
+
image = enhancer.enhance(1.1)
|
137 |
+
enhanced_image_bytes = io.BytesIO()
|
138 |
+
image.save(enhanced_image_bytes, format="JPEG", quality=95)
|
139 |
+
enhanced_image_bytes.seek(0)
|
140 |
+
ext = file.filename.split(".")[-1]
|
141 |
+
unique_filename = f"{uuid.uuid4().hex}.{ext}"
|
142 |
+
file_path = os.path.join("uploads", unique_filename)
|
143 |
+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
144 |
+
with open(file_path, "wb") as f:
|
145 |
+
f.write(enhanced_image_bytes.getvalue())
|
146 |
+
example_test = "Accurately identify the baked good in the image and provide an appropriate and recipe consistent with your analysis."
|
147 |
+
x = GeminiLatest(api_keys=GOOGLE_API_KEY)
|
148 |
+
response = x.get_response_image(example_test, file_path)
|
149 |
+
url = f"https://randydev-ryuzaki-api.hf.space/{file_path}"
|
150 |
+
return SuccessResponse(
|
151 |
+
status="True",
|
152 |
+
randydev={"url": url, "caption": response}
|
153 |
+
)
|
154 |
+
else:
|
155 |
+
return StreamingResponse(io.BytesIO(image_bytes), media_type="image/jpeg")
|
156 |
+
except Exception as e:
|
157 |
return SuccessResponse(
|
158 |
status="False",
|
159 |
+
randydev={"error": f"An error occurred: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
)
|
161 |
+
else:
|
|
|
|
|
|
|
162 |
return SuccessResponse(
|
163 |
status="False",
|
164 |
+
randydev={"error": f"Error required api_key"}
|
165 |
)
|
166 |
else:
|
167 |
tokens = get_user_tokens_gpt(payload.user_id)
|
168 |
return SuccessResponse(
|
169 |
status="False",
|
170 |
+
randydev={"error": f"Not enough tokens. Current tokens: {tokens} and required api_key."}
|
171 |
)
|