Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,27 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from io import BytesIO
|
| 6 |
import logging
|
| 7 |
|
| 8 |
-
#
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
RUNPOD_ENDPOINT = os.getenv("RUNPOD_ENDPOINT")
|
| 15 |
-
|
| 16 |
-
def generate_single_image(prompt):
|
| 17 |
-
"""Barebones image generation with maximum error handling"""
|
| 18 |
-
if not RUNPOD_API_KEY or not RUNPOD_ENDPOINT:
|
| 19 |
-
error = "API credentials not set! Check your Space secrets."
|
| 20 |
-
logger.error(error)
|
| 21 |
-
return None, error
|
| 22 |
-
|
| 23 |
try:
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
payload = {
|
| 26 |
"input": {
|
| 27 |
-
"prompt":
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
"height": 768,
|
| 31 |
-
"num_inference_steps": 25,
|
| 32 |
-
"guidance_scale": 7.5,
|
| 33 |
-
"return_base64": True # More reliable than URLs in Spaces
|
| 34 |
}
|
| 35 |
}
|
| 36 |
|
|
@@ -39,56 +30,31 @@ def generate_single_image(prompt):
|
|
| 39 |
"Content-Type": "application/json"
|
| 40 |
}
|
| 41 |
|
| 42 |
-
logger.info(f"
|
| 43 |
response = requests.post(
|
| 44 |
RUNPOD_ENDPOINT,
|
| 45 |
json=payload,
|
| 46 |
headers=headers,
|
| 47 |
-
timeout=
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
if response.status_code != 200:
|
| 53 |
-
error = f"API Error {response.status_code}: {response.text}"
|
| 54 |
-
logger.error(error)
|
| 55 |
-
return None, error
|
| 56 |
-
|
| 57 |
-
data = response.json()
|
| 58 |
-
logger.info("Got successful API response")
|
| 59 |
-
|
| 60 |
-
# Handle both base64 and URL responses
|
| 61 |
-
if "image" in data:
|
| 62 |
-
image_data = base64.b64decode(data["image"].split(",")[1])
|
| 63 |
-
return Image.open(BytesIO(image_data)), "Success!"
|
| 64 |
-
elif "output" in data and isinstance(data["output"], list):
|
| 65 |
-
image_url = data["output"][0]
|
| 66 |
-
img_data = requests.get(image_url, timeout=60).content
|
| 67 |
-
return Image.open(BytesIO(img_data)), "Success!"
|
| 68 |
else:
|
| 69 |
-
|
| 70 |
-
logger.error(error)
|
| 71 |
-
return None, error
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
-
|
| 75 |
-
logger.error(error, exc_info=True)
|
| 76 |
-
return None, error
|
| 77 |
|
| 78 |
-
# Simple interface
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
-
gr.Markdown("#
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
generate_single_image,
|
| 89 |
-
inputs=[prompt],
|
| 90 |
-
outputs=[output, status]
|
| 91 |
)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
demo.launch(debug=True)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
|
|
|
| 4 |
import logging
|
| 5 |
|
| 6 |
+
# Setup logging
|
| 7 |
logging.basicConfig(level=logging.INFO)
|
| 8 |
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
+
def test_connection():
|
| 11 |
+
"""Simplest possible connectivity test"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
+
RUNPOD_API_KEY = os.getenv("RUNPOD_API_KEY")
|
| 14 |
+
RUNPOD_ENDPOINT = os.getenv("RUNPOD_ENDPOINT")
|
| 15 |
+
|
| 16 |
+
if not RUNPOD_API_KEY or not RUNPOD_ENDPOINT:
|
| 17 |
+
return "β Missing API key or endpoint in secrets"
|
| 18 |
+
|
| 19 |
+
test_prompt = "A simple test image"
|
| 20 |
payload = {
|
| 21 |
"input": {
|
| 22 |
+
"prompt": test_prompt,
|
| 23 |
+
"width": 64,
|
| 24 |
+
"height": 64 # Minimal size for quick test
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
}
|
| 27 |
|
|
|
|
| 30 |
"Content-Type": "application/json"
|
| 31 |
}
|
| 32 |
|
| 33 |
+
logger.info(f"Testing endpoint: {RUNPOD_ENDPOINT}")
|
| 34 |
response = requests.post(
|
| 35 |
RUNPOD_ENDPOINT,
|
| 36 |
json=payload,
|
| 37 |
headers=headers,
|
| 38 |
+
timeout=10
|
| 39 |
)
|
| 40 |
|
| 41 |
+
if response.status_code == 200:
|
| 42 |
+
return "β
Connection successful! Response: " + str(response.json())[:100] + "..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
else:
|
| 44 |
+
return f"β API Error {response.status_code}: {response.text}"
|
|
|
|
|
|
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
+
return f"π₯ Critical failure: {str(e)}"
|
|
|
|
|
|
|
| 48 |
|
|
|
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# π¨ RunPod Connection Tester")
|
| 51 |
+
test_btn = gr.Button("Test Connection")
|
| 52 |
+
output = gr.Textbox(label="Diagnostic Output")
|
| 53 |
+
|
| 54 |
+
test_btn.click(
|
| 55 |
+
test_connection,
|
| 56 |
+
inputs=[],
|
| 57 |
+
outputs=[output]
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
+
demo.launch(debug=True)
|
|
|