Update main.py
Browse files
main.py
CHANGED
|
@@ -1,58 +1,34 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi import
|
| 3 |
-
|
| 4 |
-
from fastapi.staticfiles import StaticFiles
|
| 5 |
-
from gradio_client import Client
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
hf_token = os.environ.get('HF_TOKEN')
|
| 9 |
-
client = Client("https://ashrafb-facesw.hf.space/--replicas/j2fu5/", hf_token=hf_token)
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
@app.route("/predict", methods=["GET", "POST"])
|
| 23 |
-
async def predict(request: Request, source_image: UploadFile = str,
|
| 24 |
-
source_face_position: int ,
|
| 25 |
-
destination_image: str,
|
| 26 |
-
destination_face_position: int, :
|
| 27 |
-
if request.method == "POST":
|
| 28 |
print(f"Received POST request to /predict")
|
| 29 |
-
print(f"Source image filename: {source_image.filename}")
|
| 30 |
print(f"Source face position: {source_face_position}")
|
| 31 |
-
print(f"Destination image filename: {destination_image.filename}")
|
| 32 |
print(f"Destination face position: {destination_face_position}")
|
| 33 |
|
| 34 |
# Function body...
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
destination_image_path = f"/tmp/{destination_image.filename}"
|
| 42 |
-
with open(destination_image_path, "wb") as buffer:
|
| 43 |
-
buffer.write(destination_image.file.read())
|
| 44 |
-
|
| 45 |
-
# Make prediction using gradio_client
|
| 46 |
-
result = client.predict(
|
| 47 |
-
source_image_path,
|
| 48 |
-
source_face_position,
|
| 49 |
-
destination_image_path,
|
| 50 |
-
destination_face_position,
|
| 51 |
-
api_name="/predict"
|
| 52 |
-
)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
return FileResponse(result, media_type="image/jpeg")
|
| 56 |
else:
|
| 57 |
# Handle GET request, if necessary
|
| 58 |
return "Use POST method to predict"
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
@app.post("/predict")
|
| 8 |
+
async def predict(request: Request):
|
| 9 |
+
if request.method == "POST":
|
| 10 |
+
body = await request.body()
|
| 11 |
+
data = json.loads(body)
|
|
|
|
| 12 |
|
| 13 |
+
source_image_base64 = data.get("source_image_base64")
|
| 14 |
+
source_face_position = int(data.get("source_face_position"))
|
| 15 |
+
destination_image_base64 = data.get("destination_image_base64")
|
| 16 |
+
destination_face_position = int(data.get("destination_face_position"))
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
print(f"Received POST request to /predict")
|
|
|
|
| 19 |
print(f"Source face position: {source_face_position}")
|
|
|
|
| 20 |
print(f"Destination face position: {destination_face_position}")
|
| 21 |
|
| 22 |
# Function body...
|
| 23 |
+
# Perform image processing or any other task here
|
| 24 |
|
| 25 |
+
# For demonstration, simply return the received face positions
|
| 26 |
+
result = {
|
| 27 |
+
"source_face_position": source_face_position,
|
| 28 |
+
"destination_face_position": destination_face_position
|
| 29 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
return JSONResponse(content=result)
|
|
|
|
| 32 |
else:
|
| 33 |
# Handle GET request, if necessary
|
| 34 |
return "Use POST method to predict"
|