Ashrafb commited on
Commit
5283239
·
verified ·
1 Parent(s): 06c8e28

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +33 -42
main.py CHANGED
@@ -1,50 +1,41 @@
1
- from fastapi import FastAPI
2
- from fastapi.staticfiles import StaticFiles
3
  from fastapi.responses import FileResponse
4
- import gradio as gr
5
- import insightface
6
- from insightface.app import FaceAnalysis
7
-
8
- assert insightface.__version__ >= '0.7'
9
 
10
  app = FastAPI()
11
 
12
- def prepare_app():
13
- app = FaceAnalysis(name='buffalo_l')
14
- app.prepare(ctx_id=0, det_size=(640, 640))
15
- swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True)
16
- return app, swapper
17
-
18
- def sort_faces(faces):
19
- return sorted(faces, key=lambda x: x.bbox[0])
20
-
21
- def get_face(faces, face_id):
22
- try:
23
- if len(faces) < face_id or face_id < 1:
24
- raise gr.Error(f"The image includes only {len(faces)} faces, however, you asked for face {face_id}")
25
- return faces[face_id-1]
26
- except Exception as e:
27
- raise gr.Error(f"An error occurred: {str(e)}")
28
-
29
- app, swapper = prepare_app()
30
-
31
- def perform_swap_faces(sourceImage, sourceFaceIndex, destinationImage, destinationFaceIndex):
32
- """Swaps faces between the source and destination images based on the specified face indices."""
33
- faces = sort_faces(app.get(sourceImage))
34
- source_face = get_face(faces, sourceFaceIndex)
35
-
36
- res_faces = sort_faces(app.get(destinationImage))
37
- res_face = get_face(res_faces, destinationFaceIndex)
38
-
39
- result = swapper.get(destinationImage, res_face, source_face, paste_back=True)
40
- return result
41
-
42
- @app.get("/swap_faces")
43
- def swap_faces_endpoint(sourceImage, sourceFaceIndex, destinationImage, destinationFaceIndex):
44
- return perform_swap_faces(sourceImage, sourceFaceIndex, destinationImage, destinationFaceIndex)
45
-
46
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
47
 
 
 
48
  @app.get("/")
49
- def index() -> FileResponse:
50
  return FileResponse(path="/app/static/index.html", media_type="text/html")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile
 
2
  from fastapi.responses import FileResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ from gradio_client import Client
 
 
 
5
 
6
  app = FastAPI()
7
 
8
+ # Mount the static files directory
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
10
 
11
+ client = Client("https://ashrafb-facesw.hf.space/--replicas/rtez3/")
12
+
13
  @app.get("/")
14
+ def index():
15
  return FileResponse(path="/app/static/index.html", media_type="text/html")
16
+
17
+ @app.post("/predict")
18
+ async def predict(source_image: UploadFile = File(...),
19
+ source_face_position: int,
20
+ destination_image: UploadFile = File(...),
21
+ destination_face_position: int):
22
+ # Save uploaded files locally
23
+ source_image_path = f"/tmp/{source_image.filename}"
24
+ with open(source_image_path, "wb") as buffer:
25
+ buffer.write(source_image.file.read())
26
+
27
+ destination_image_path = f"/tmp/{destination_image.filename}"
28
+ with open(destination_image_path, "wb") as buffer:
29
+ buffer.write(destination_image.file.read())
30
+
31
+ # Make prediction using gradio_client
32
+ result = client.predict(
33
+ source_image_path,
34
+ source_face_position,
35
+ destination_image_path,
36
+ destination_face_position,
37
+ api_name="/predict"
38
+ )
39
+
40
+ # Return the result image file
41
+ return FileResponse(result, media_type="image/jpeg")