Ashrafb commited on
Commit
af89b78
·
verified ·
1 Parent(s): 71f0a96

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +19 -43
main.py CHANGED
@@ -1,58 +1,34 @@
1
- import gradio as gr
2
- from fastapi import FastAPI, File, UploadFile, Form # Add Form to imports
3
- from fastapi.responses import FileResponse
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
- # Mount the static files directory
14
- app.mount("/", StaticFiles(directory="static", html=True), name="static")
15
-
16
- @app.get("/")
17
- def index():
18
- return FileResponse(path="/app/static/index.html", media_type="text/html")
19
 
20
- from fastapi import Request
 
 
 
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
- # Save uploaded files locally
37
- source_image_path = f"/tmp/{source_image.filename}"
38
- with open(source_image_path, "wb") as buffer:
39
- buffer.write(source_image.file.read())
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
- # Return the result image file
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"