knnn11 commited on
Commit
c4311e1
Β·
verified Β·
1 Parent(s): e3cbb09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -62
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
- # Configure logging
9
  logging.basicConfig(level=logging.INFO)
10
  logger = logging.getLogger(__name__)
11
 
12
- # Get credentials from environment variables
13
- RUNPOD_API_KEY = os.getenv("RUNPOD_API_KEY")
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
- # Basic payload - adjust based on your RunPod template
 
 
 
 
 
 
25
  payload = {
26
  "input": {
27
- "prompt": f"Comic book style: {prompt}",
28
- "negative_prompt": "blurry, deformed, text, watermark",
29
- "width": 768,
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"Sending request to: {RUNPOD_ENDPOINT}")
43
  response = requests.post(
44
  RUNPOD_ENDPOINT,
45
  json=payload,
46
  headers=headers,
47
- timeout=120
48
  )
49
 
50
- logger.info(f"Received status: {response.status_code}")
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
- error = f"Unexpected response format: {data}"
70
- logger.error(error)
71
- return None, error
72
 
73
  except Exception as e:
74
- error = f"Generation failed: {str(e)}"
75
- logger.error(error, exc_info=True)
76
- return None, error
77
 
78
- # Simple interface
79
  with gr.Blocks() as demo:
80
- gr.Markdown("# πŸš€ RunPod Tester")
81
- with gr.Row():
82
- prompt = gr.Textbox(label="Prompt", value="A superhero standing on a rooftop")
83
- btn = gr.Button("Generate")
84
- output = gr.Image(label="Result")
85
- status = gr.Textbox(label="Status")
86
-
87
- btn.click(
88
- generate_single_image,
89
- inputs=[prompt],
90
- outputs=[output, status]
91
  )
92
 
93
- if __name__ == "__main__":
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)