Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,14 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
@app.get("/")
|
| 11 |
def root():
|
|
|
|
| 12 |
return {"status": "ok"}
|
| 13 |
-
|
| 14 |
-
@app.post("/generate")
|
| 15 |
-
async def generate_image(request: Request):
|
| 16 |
-
try:
|
| 17 |
-
body = await request.json()
|
| 18 |
-
base64_image = body.get("image")
|
| 19 |
-
prompt = body.get("prompt", "")
|
| 20 |
-
|
| 21 |
-
if not base64_image:
|
| 22 |
-
return JSONResponse(status_code=400, content={"success": False, "error": "image is required"})
|
| 23 |
-
|
| 24 |
-
# η»εγγ³γΌγγ¨ε€ζ
|
| 25 |
-
image_data = base64.b64decode(base64_image.split(",")[-1])
|
| 26 |
-
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 27 |
-
|
| 28 |
-
buffered = io.BytesIO()
|
| 29 |
-
image.save(buffered, format="PNG")
|
| 30 |
-
encoded_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 31 |
-
|
| 32 |
-
return JSONResponse(content={
|
| 33 |
-
"success": True,
|
| 34 |
-
"echo_prompt": prompt,
|
| 35 |
-
"image_base64": f"data:image/png;base64,{encoded_img}"
|
| 36 |
-
})
|
| 37 |
-
|
| 38 |
-
except Exception as e:
|
| 39 |
-
print("=== ERROR OCCURRED ===")
|
| 40 |
-
traceback.print_exc()
|
| 41 |
-
print("======================")
|
| 42 |
-
return JSONResponse(status_code=500, content={"success": False, "error": str(e)})
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from huggingface_hub import login
|
| 5 |
+
|
| 6 |
+
os.environ["HUGGINGFACE_HUB_CACHE"] = "/tmp/.cache"
|
| 7 |
+
os.environ["HF_HOME"] = "/tmp/.cache"
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
@app.get("/")
|
| 12 |
def root():
|
| 13 |
+
login(os.environ.get("HUGGINGFACE_HUB_TOKEN", ""))
|
| 14 |
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|