Walmart-the-bag commited on
Commit
22dcbce
·
verified ·
1 Parent(s): f89f724

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -55
app.py CHANGED
@@ -5,77 +5,50 @@ import time
5
  from better_profanity import profanity
6
 
7
  url = "https://nexra.aryahcr.cc/api/image/complements"
8
- headers = {
9
- "Content-Type": "application/json"
10
- }
11
 
12
  profanity.load_censor_words()
13
- no_nsfw_image_path = "no_nsfw.png"
14
 
15
- # I have to add this due to OpenAI policy, which prohibits any of this content... wonder why. suckers; you can host this locally if u wanna be weird
16
  def is_nsfw(text):
17
- result = profanity.contains_profanity(text)
18
- print(f"NSFW Check for prompt '{text}': {result}")
19
- return result
20
-
21
 
22
  def generate_image(prompt, model):
23
  if is_nsfw(prompt):
24
- return no_nsfw_image_path
 
 
 
25
 
26
- data = {
27
- "prompt": prompt,
28
- "model": model,
29
- }
30
- try:
31
- response = requests.post(url, headers=headers, data=json.dumps(data))
32
 
33
- if response.status_code == 200:
34
- response_data = response.json()
35
- image_id = response_data.get("id")
36
-
37
- if not image_id:
38
- return no_nsfw_image_path
39
 
40
- while True:
41
- status_response = requests.get(f"{url}/{image_id}")
 
 
 
42
 
43
- if status_response.status_code == 200:
44
- status_data = status_response.json()
45
- status = status_data.get("status")
46
-
47
- if status == "completed":
48
- images = status_data.get("images")
49
- if images and isinstance(images, list):
50
- image_url = images[0]
51
- return image_url
52
- else:
53
- return no_nsfw_image_path
54
- elif status == "error":
55
- return no_nsfw_image_path
56
- elif status == "not_found":
57
- return no_nsfw_image_path
58
- elif status == "pending":
59
- time.sleep(1)
60
- else:
61
- return no_nsfw_image_path
62
- else:
63
  return no_nsfw_image_path
64
- else:
65
- return no_nsfw_image_path
66
- except json.JSONDecodeError:
67
- return no_nsfw_image_path
68
- except Exception as e:
69
- return no_nsfw_image_path
70
 
71
  iface = gr.Interface(
72
  fn=generate_image,
73
- inputs=[
74
- gr.Textbox(label="Enter prompt"),
75
- gr.Radio(["dalle2"], label="Select Model")
76
- ],
77
  outputs=gr.Image(type="filepath"),
78
  title="DALLE2 Generation",
79
  description="DALLE-2 Generation from Nextra API, NOTE: DALL-E 3 is not supported yet."
80
  )
81
- iface.launch()
 
 
5
  from better_profanity import profanity
6
 
7
  url = "https://nexra.aryahcr.cc/api/image/complements"
8
+ headers = {"Content-Type": "application/json"}
 
 
9
 
10
  profanity.load_censor_words()
11
+ no_nsfw_image_path = "no_nsfw.png"
12
 
 
13
  def is_nsfw(text):
14
+ return profanity.contains_profanity(text)
 
 
 
15
 
16
  def generate_image(prompt, model):
17
  if is_nsfw(prompt):
18
+ return no_nsfw_image_path
19
+
20
+ data = {"prompt": prompt, "model": model}
21
+ response = requests.post(url, headers=headers, data=json.dumps(data))
22
 
23
+ if response.status_code == 200:
24
+ response_data = response.json()
25
+ image_id = response_data.get("id")
 
 
 
26
 
27
+ if not image_id:
28
+ return no_nsfw_image_path
 
 
 
 
29
 
30
+ while True:
31
+ status_response = requests.get(f"{url}/{image_id}")
32
+ if status_response.status_code == 200:
33
+ status_data = status_response.json()
34
+ status = status_data.get("status")
35
 
36
+ if status == "completed":
37
+ images = status_data.get("images")
38
+ if images:
39
+ return images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  return no_nsfw_image_path
41
+ elif status in ["error", "not_found"]:
42
+ return no_nsfw_image_path
43
+ time.sleep(1)
44
+ return no_nsfw_image_path
 
 
45
 
46
  iface = gr.Interface(
47
  fn=generate_image,
48
+ inputs=[gr.Textbox(label="Enter prompt"), gr.Radio(["dalle2"], label="Select Model")],
 
 
 
49
  outputs=gr.Image(type="filepath"),
50
  title="DALLE2 Generation",
51
  description="DALLE-2 Generation from Nextra API, NOTE: DALL-E 3 is not supported yet."
52
  )
53
+
54
+ iface.launch(share=True, queue=True)