craftgamesnetwork commited on
Commit
7c2df3c
1 Parent(s): 4e82bc1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from urllib.parse import urlparse
3
+ import requests
4
+ import time
5
+ import os
6
+
7
+ from utils.gradio_helpers import parse_outputs, process_outputs
8
+
9
+ inputs = []
10
+ inputs.append(gr.Image(
11
+ label="Target Image", type="filepath"
12
+ ))
13
+
14
+ inputs.append(gr.Image(
15
+ label="Swap Image", type="filepath"
16
+ ))
17
+
18
+ names = ['target_image', 'swap_image']
19
+
20
+ outputs = []
21
+ outputs.append(gr.Image())
22
+
23
+ expected_outputs = len(outputs)
24
+ def predict(request: gr.Request, *args, progress=gr.Progress(track_tqdm=True)):
25
+ headers = {'Content-Type': 'application/json'}
26
+
27
+ payload = {"input": {}}
28
+
29
+
30
+ base_url = "http://0.0.0.0:7860"
31
+ for i, key in enumerate(names):
32
+ value = args[i]
33
+ if value and (os.path.exists(str(value))):
34
+ value = f"{base_url}/file=" + value
35
+ if value is not None and value != "":
36
+ payload["input"][key] = value
37
+
38
+ response = requests.post("http://0.0.0.0:5000/predictions", headers=headers, json=payload)
39
+
40
+
41
+ if response.status_code == 201:
42
+ follow_up_url = response.json()["urls"]["get"]
43
+ response = requests.get(follow_up_url, headers=headers)
44
+ while response.json()["status"] != "succeeded":
45
+ if response.json()["status"] == "failed":
46
+ raise gr.Error("The submission failed!")
47
+ response = requests.get(follow_up_url, headers=headers)
48
+ time.sleep(1)
49
+ if response.status_code == 200:
50
+ json_response = response.json()
51
+ #If the output component is JSON return the entire output response
52
+ if(outputs[0].get_config()["name"] == "json"):
53
+ return json_response["output"]
54
+ predict_outputs = parse_outputs(json_response["output"])
55
+ processed_outputs = process_outputs(predict_outputs)
56
+ difference_outputs = expected_outputs - len(processed_outputs)
57
+ # If less outputs than expected, hide the extra ones
58
+ if difference_outputs > 0:
59
+ extra_outputs = [gr.update(visible=False)] * difference_outputs
60
+ processed_outputs.extend(extra_outputs)
61
+ # If more outputs than expected, cap the outputs to the expected number
62
+ elif difference_outputs < 0:
63
+ processed_outputs = processed_outputs[:difference_outputs]
64
+
65
+ return tuple(processed_outputs) if len(processed_outputs) > 1 else processed_outputs[0]
66
+ else:
67
+ if(response.status_code == 409):
68
+ raise gr.Error(f"Sorry, the Cog image is still processing. Try again in a bit.")
69
+ raise gr.Error(f"The submission failed! Error: {response.status_code}")
70
+
71
+ title = "Demo for face-swap cog image by omniedgeio"
72
+ model_description = "Face Swap"
73
+
74
+ app = gr.Interface(
75
+ fn=predict,
76
+ inputs=inputs,
77
+ outputs=outputs,
78
+ title=title,
79
+ description=model_description,
80
+ allow_flagging="never",
81
+ )
82
+ app.launch(share=True)