fffiloni commited on
Commit
3f6c118
1 Parent(s): 1bef25a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -44
app.py CHANGED
@@ -22,62 +22,67 @@ from utils.gradio_helpers import parse_outputs, process_outputs
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
- {args[0]}
29
- """)
30
- is_safe = safety_check(args[0])
31
- print(is_safe)
 
 
 
32
 
33
- match = re.search(r'\bYes\b', is_safe)
34
 
35
- if match:
36
- status = 'Yes'
37
- else:
38
- status = None
39
 
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"
 
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
  print(f"""
26
+ —/n
27
+ {args[0]}
28
+ """)
29
+
30
+ try:
31
+
32
+ is_safe = safety_check(args[0])
33
+ print(is_safe)
34
 
35
+ match = re.search(r'\bYes\b', is_safe)
36
 
37
+ if match:
38
+ status = 'Yes'
39
+ else:
40
+ status = None
41
 
42
+ if status == "Yes" :
43
+ raise gr.Error("Don't ask for such things.")
44
+ else:
45
 
46
+ headers = {'Content-Type': 'application/json'}
47
 
48
+ payload = {"input": {}}
49
 
50
 
51
+ base_url = "http://0.0.0.0:7860"
52
+ for i, key in enumerate(names):
53
+ value = args[i]
54
+ if value and (os.path.exists(str(value))):
55
+ value = f"{base_url}/file=" + value
56
+ if value is not None and value != "":
57
+ payload["input"][key] = value
58
 
59
+ response = requests.post("http://0.0.0.0:5000/predictions", headers=headers, json=payload)
60
 
61
 
62
+ if response.status_code == 201:
63
+ follow_up_url = response.json()["urls"]["get"]
 
 
 
 
64
  response = requests.get(follow_up_url, headers=headers)
65
+ while response.json()["status"] != "succeeded":
66
+ if response.json()["status"] == "failed":
67
+ raise gr.Error("The submission failed!")
68
+ response = requests.get(follow_up_url, headers=headers)
69
+ time.sleep(1)
70
+ if response.status_code == 200:
71
+ json_response = response.json()
72
+ #If the output component is JSON return the entire output response
73
+ if(outputs[0].get_config()["name"] == "json"):
74
+ return json_response["output"]
75
+ predict_outputs = parse_outputs(json_response["output"])
76
+ processed_outputs = process_outputs(predict_outputs)
77
+ return tuple(processed_outputs) if len(processed_outputs) > 1 else processed_outputs[0]
78
+ else:
79
+ if(response.status_code == 409):
80
+ raise gr.Error(f"Sorry, the Cog image is still processing. Try again in a bit.")
81
+ raise gr.Error(f"The submission failed! Error: {response.status_code}")
82
+
83
+ except Exception as e:
84
+ # Handle any other type of error
85
+ raise gr.Error(f"An error occurred: {e}")
86
 
87
  title = "Demo for consistent-character cog image by fofr"
88
  description = "Create images of a given character in different poses • running cog image by fofr"