| from fastapi import FastAPI, File, UploadFile, HTTPException |
| from fastapi.responses import JSONResponse |
| from gradio_client import Client, handle_file |
| import os |
| import base64 |
|
|
| |
| app = FastAPI() |
|
|
| |
| HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
| |
| client = Client("Makhinur/Its2", hf_token=HF_TOKEN) |
|
|
| @app.post("/upload/") |
| async def upload_image(file: UploadFile = File(...)): |
| try: |
| |
| file_location = f"temp_{file.filename}" |
| with open(file_location, "wb") as f: |
| f.write(await file.read()) |
|
|
| |
| result = client.predict( |
| img=handle_file(file_location), |
| api_name="/predict" |
| ) |
|
|
| |
| os.remove(file_location) |
|
|
| |
| sketch_image_path = result[0] |
|
|
| |
| with open(sketch_image_path, "rb") as img_file: |
| sketch_image_base64 = base64.b64encode(img_file.read()).decode('utf-8') |
|
|
| |
| response = { |
| "sketch_image_base64": f"data:image/jpeg;base64,{sketch_image_base64}" |
| } |
|
|
| return JSONResponse(content=response) |
|
|
| except Exception as e: |
| raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|