Walmart-the-bag commited on
Commit
65fe587
·
verified ·
1 Parent(s): 18a5673
Files changed (1) hide show
  1. app.py +44 -13
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import requests
3
  import json
 
4
 
5
  url = "https://nexra.aryahcr.cc/api/image/complements"
6
  headers = {
@@ -14,23 +15,53 @@ def generate_image(prompt, model):
14
  }
15
  try:
16
  response = requests.post(url, headers=headers, data=json.dumps(data))
17
- raw_response = response.text.strip()
18
-
19
  if response.status_code == 200:
20
- cleaned_response = raw_response.lstrip('_')
21
- response_data = json.loads(cleaned_response)
22
-
23
- if 'images' in response_data and isinstance(response_data['images'], list):
24
- image_url = response_data['images'][0]
25
- return image_url
26
- else:
27
- return "Error: No image data in the response."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  else:
29
- return f"Error: {response.status_code}"
30
  except json.JSONDecodeError:
31
  return "Error: Invalid JSON response."
32
  except Exception as e:
33
- return str(e)
34
 
35
- iface = gr.Interface(fn=generate_image, inputs=[gr.Textbox(label="Enter prompt"), gr.Radio(["dalle2"], label="Select Model")], outputs="image", title="DALLE2 Generation", description="Disclaimer: This uses the Nexra API for image generation. I cannot guarantee rate limits, if you do not recieve an image please try again. I will change to use a different API soon. DALL-E 3 is not supported yet.")
 
 
 
 
 
 
 
 
 
36
  iface.launch()
 
1
  import gradio as gr
2
  import requests
3
  import json
4
+ import time
5
 
6
  url = "https://nexra.aryahcr.cc/api/image/complements"
7
  headers = {
 
15
  }
16
  try:
17
  response = requests.post(url, headers=headers, data=json.dumps(data))
 
 
18
  if response.status_code == 200:
19
+ response_data = response.json()
20
+ image_id = response_data.get("id")
21
+ if not image_id:
22
+ return "Error: No image ID returned in the response."
23
+
24
+ while True:
25
+ status_response = requests.get(f"{url}/{image_id}")
26
+ if status_response.status_code == 200:
27
+ status_data = status_response.json()
28
+ status = status_data.get("status")
29
+ if status == "completed":
30
+ images = status_data.get("images")
31
+ if images and isinstance(images, list):
32
+ image_data = images[0]
33
+ if image_data.startswith("data:image"):
34
+ base64_data = image_data.split(",", 1)[1]
35
+ return base64_data
36
+ else:
37
+ return "Error: Unexpected image data format."
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="pil"),
64
+ title="DALLE2 Generation",
65
+ description="Disclaimer: This uses the Nexra API for image generation. I cannot guarantee rate limits, if you do not recieve an image please try again. I will change to use a different API soon. DALL-E 3 is not supported yet."
66
+ )
67
  iface.launch()