Spaces:
Runtime error
Runtime error
fix err
Browse files
app.py
CHANGED
|
@@ -4,39 +4,64 @@ import json
|
|
| 4 |
import time
|
| 5 |
|
| 6 |
url = "https://nexra.aryahcr.cc/api/image/complements"
|
| 7 |
-
headers = {
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
def generate_image(prompt, model):
|
| 11 |
-
data = {
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
if status_response.status_code == 200:
|
| 21 |
-
status_data = status_response.json()
|
| 22 |
-
status = status_data.get("status")
|
| 23 |
|
| 24 |
-
if
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
iface = gr.Interface(
|
| 35 |
fn=generate_image,
|
| 36 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
| 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()
|
|
|