fffiloni commited on
Commit
d8389e8
1 Parent(s): 7032d3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -43
app.py CHANGED
@@ -21,51 +21,13 @@ from utils.gradio_helpers import parse_outputs, process_outputs
21
 
22
  names = ['prompt', 'negative_prompt', 'subject', 'number_of_outputs', 'number_of_images_per_pose', 'randomise_poses', 'output_format', 'output_quality', 'seed']
23
 
24
- def predict(request: gr.Request, *args):
25
-
26
- headers = {'Content-Type': 'application/json'}
27
-
28
- payload = {"input": {}}
29
-
30
-
31
- base_url = "http://0.0.0.0:7860"
32
- for i, key in enumerate(names):
33
- value = args[i]
34
- if value and (os.path.exists(str(value))):
35
- value = f"{base_url}/file=" + value
36
- if value is not None and value != "":
37
- payload["input"][key] = value
38
-
39
- response = requests.post("http://0.0.0.0:5000/predictions", headers=headers, json=payload)
40
-
41
-
42
- if response.status_code == 201:
43
- follow_up_url = response.json()["urls"]["get"]
44
- response = requests.get(follow_up_url, headers=headers)
45
- while response.json()["status"] != "succeeded":
46
- if response.json()["status"] == "failed":
47
- raise gr.Error("The submission failed!")
48
- response = requests.get(follow_up_url, headers=headers)
49
- time.sleep(1)
50
- if response.status_code == 200:
51
- json_response = response.json()
52
- #If the output component is JSON return the entire output response
53
- if(outputs[0].get_config()["name"] == "json"):
54
- return json_response["output"]
55
- predict_outputs = parse_outputs(json_response["output"])
56
- processed_outputs = process_outputs(predict_outputs)
57
- return tuple(processed_outputs) if len(processed_outputs) > 1 else processed_outputs[0]
58
- else:
59
- if(response.status_code == 409):
60
- raise gr.Error(f"Sorry, the Cog image is still processing. Try again in a bit.")
61
- raise gr.Error(f"The submission failed! Error: {response.status_code}")
62
 
63
- def main(prompt, negative_prompt, subject, number_of_outputs, number_of_images_per_pose, randomise_poses, output_format, output_quality, seed):
64
  print(f"""
65
  —/n
66
- {prompt}
67
  """)
68
- is_safe = safety_check(prompt)
69
  print(is_safe)
70
 
71
  match = re.search(r'\bYes\b', is_safe)
@@ -78,7 +40,44 @@ def main(prompt, negative_prompt, subject, number_of_outputs, number_of_images_p
78
  if status == "Yes" :
79
  raise gr.Error("Don't ask for such things.")
80
  else:
81
- results = predict(prompt, negative_prompt, subject, number_of_outputs, number_of_images_per_pose, randomise_poses, output_format, output_quality, seed)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  title = "Demo for consistent-character cog image by fofr"
84
  description = "Create images of a given character in different poses • running cog image by fofr"
@@ -155,7 +154,7 @@ with gr.Blocks(css=css) as app:
155
  outputs = [consistent_results]
156
 
157
  submit_btn.click(
158
- fn = main,
159
  inputs = inputs,
160
  outputs = outputs,
161
  show_api = False
 
21
 
22
  names = ['prompt', 'negative_prompt', 'subject', 'number_of_outputs', 'number_of_images_per_pose', 'randomise_poses', 'output_format', 'output_quality', 'seed']
23
 
24
+ def predict(request: gr.Request, *args, progress=gr.Progress(track_tqdm=True)):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
 
26
  print(f"""
27
  —/n
28
+ {arg[0]}
29
  """)
30
+ is_safe = safety_check(arg[0])
31
  print(is_safe)
32
 
33
  match = re.search(r'\bYes\b', is_safe)
 
40
  if status == "Yes" :
41
  raise gr.Error("Don't ask for such things.")
42
  else:
43
+
44
+ headers = {'Content-Type': 'application/json'}
45
+
46
+ payload = {"input": {}}
47
+
48
+
49
+ base_url = "http://0.0.0.0:7860"
50
+ for i, key in enumerate(names):
51
+ value = args[i]
52
+ if value and (os.path.exists(str(value))):
53
+ value = f"{base_url}/file=" + value
54
+ if value is not None and value != "":
55
+ payload["input"][key] = value
56
+
57
+ response = requests.post("http://0.0.0.0:5000/predictions", headers=headers, json=payload)
58
+
59
+
60
+ if response.status_code == 201:
61
+ follow_up_url = response.json()["urls"]["get"]
62
+ response = requests.get(follow_up_url, headers=headers)
63
+ while response.json()["status"] != "succeeded":
64
+ if response.json()["status"] == "failed":
65
+ raise gr.Error("The submission failed!")
66
+ response = requests.get(follow_up_url, headers=headers)
67
+ time.sleep(1)
68
+ if response.status_code == 200:
69
+ json_response = response.json()
70
+ #If the output component is JSON return the entire output response
71
+ if(outputs[0].get_config()["name"] == "json"):
72
+ return json_response["output"]
73
+ predict_outputs = parse_outputs(json_response["output"])
74
+ processed_outputs = process_outputs(predict_outputs)
75
+ return tuple(processed_outputs) if len(processed_outputs) > 1 else processed_outputs[0]
76
+ else:
77
+ if(response.status_code == 409):
78
+ raise gr.Error(f"Sorry, the Cog image is still processing. Try again in a bit.")
79
+ raise gr.Error(f"The submission failed! Error: {response.status_code}")
80
+
81
 
82
  title = "Demo for consistent-character cog image by fofr"
83
  description = "Create images of a given character in different poses • running cog image by fofr"
 
154
  outputs = [consistent_results]
155
 
156
  submit_btn.click(
157
+ fn = predict,
158
  inputs = inputs,
159
  outputs = outputs,
160
  show_api = False