Walmart-the-bag commited on
Commit
f299b30
1 Parent(s): 2fc1e78
Files changed (1) hide show
  1. app.py +50 -25
app.py CHANGED
@@ -4,39 +4,64 @@ import json
4
  import time
5
 
6
  url = "https://nexra.aryahcr.cc/api/image/complements"
7
- headers = {"Content-Type": "application/json"}
8
-
 
9
 
10
  def generate_image(prompt, model):
11
- data = {"prompt": prompt, "model": model}
12
- response = requests.post(url, headers=headers, data=json.dumps(data))
13
-
14
- if response.status_code == 200:
15
- response_data = response.json()
16
- image_id = response_data.get("id")
 
 
 
 
 
 
 
17
 
18
- while True:
19
- status_response = requests.get(f"{url}/{image_id}")
20
- if status_response.status_code == 200:
21
- status_data = status_response.json()
22
- status = status_data.get("status")
23
 
24
- if status == "completed":
25
- images = status_data.get("images")
26
- if images:
27
- return images[0]
28
- return no_nsfw_image_path
29
- elif status in ["error", "not_found"]:
30
- return no_nsfw_image_path
31
- time.sleep(1)
32
- return no_nsfw_image_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  iface = gr.Interface(
35
  fn=generate_image,
36
- inputs=[gr.Textbox(label="Enter prompt"), gr.Radio(["dalle2"], label="Select Model")],
 
 
 
37
  outputs=gr.Image(type="filepath"),
38
  title="DALLE2 Generation",
39
  description="DALLE-2 Generation from Nextra API, NOTE: DALL-E 3 is not supported yet."
40
  )
41
-
42
- iface.launch(share=True)
 
4
  import time
5
 
6
  url = "https://nexra.aryahcr.cc/api/image/complements"
7
+ headers = {
8
+ "Content-Type": "application/json"
9
+ }
10
 
11
  def generate_image(prompt, model):
12
+ data = {
13
+ "prompt": prompt,
14
+ "model": model,
15
+ }
16
+ try:
17
+ response = requests.post(url, headers=headers, data=json.dumps(data))
18
+
19
+ if response.status_code == 200:
20
+ response_data = response.json()
21
+ image_id = response_data.get("id")
22
+
23
+ if not image_id:
24
+ return "Error: No image ID returned in the response."
25
 
26
+ while True:
27
+ status_response = requests.get(f"{url}/{image_id}")
 
 
 
28
 
29
+ if status_response.status_code == 200:
30
+ status_data = status_response.json()
31
+ status = status_data.get("status")
32
+
33
+ if status == "completed":
34
+ images = status_data.get("images")
35
+ if images and isinstance(images, list):
36
+ image_url = images[0]
37
+ return image_url
38
+ else:
39
+ return "Error: No images found in the response."
40
+ elif status == "error":
41
+ return "Error: Image generation failed."
42
+ elif status == "not_found":
43
+ return "Error: Image ID not found."
44
+ elif status == "pending":
45
+ time.sleep(1)
46
+ else:
47
+ return f"Error: Unexpected status '{status}'."
48
+ else:
49
+ return f"Error: Status check failed with code {status_response.status_code}."
50
+ else:
51
+ return f"Error: Initial request failed with code {response.status_code}."
52
+ except json.JSONDecodeError:
53
+ return "Error: Invalid JSON response."
54
+ except Exception as e:
55
+ return f"Exception occurred: {str(e)}"
56
 
57
  iface = gr.Interface(
58
  fn=generate_image,
59
+ inputs=[
60
+ gr.Textbox(label="Enter prompt"),
61
+ gr.Radio(["dalle2"], label="Select Model")
62
+ ],
63
  outputs=gr.Image(type="filepath"),
64
  title="DALLE2 Generation",
65
  description="DALLE-2 Generation from Nextra API, NOTE: DALL-E 3 is not supported yet."
66
  )
67
+ iface.launch()