Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,10 +8,10 @@ import os
|
|
| 8 |
from datetime import datetime
|
| 9 |
import re
|
| 10 |
import tempfile
|
|
|
|
| 11 |
|
| 12 |
# Try to import your existing OCI connector for direct access
|
| 13 |
try:
|
| 14 |
-
# This will work if we're in the same app context
|
| 15 |
from app import oci_connector
|
| 16 |
DIRECT_OCI_ACCESS = True
|
| 17 |
print("β
Direct OCI access available - using existing OCI connector")
|
|
@@ -27,7 +27,7 @@ def load_model():
|
|
| 27 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 28 |
"runwayml/stable-diffusion-v1-5",
|
| 29 |
torch_dtype=torch.float32,
|
| 30 |
-
safety_checker=None,
|
| 31 |
requires_safety_checker=False
|
| 32 |
).to("cpu")
|
| 33 |
print("β
Model loaded successfully!")
|
|
@@ -39,8 +39,39 @@ def load_model():
|
|
| 39 |
# Load the model once at startup
|
| 40 |
pipe = load_model()
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def save_to_oci_direct(image, prompt):
|
| 43 |
-
"""Save image using direct OCI connector access"""
|
| 44 |
try:
|
| 45 |
# Create temporary file
|
| 46 |
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
|
|
@@ -55,6 +86,8 @@ def save_to_oci_direct(image, prompt):
|
|
| 55 |
# Use project-based directory structure
|
| 56 |
object_name = f"storybook-generator/childrens-books/{filename}"
|
| 57 |
|
|
|
|
|
|
|
| 58 |
# Upload using existing OCI connector
|
| 59 |
success, message = oci_connector.upload_file(temp_path, object_name, None)
|
| 60 |
|
|
@@ -62,7 +95,13 @@ def save_to_oci_direct(image, prompt):
|
|
| 62 |
os.unlink(temp_path)
|
| 63 |
|
| 64 |
if success:
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
return f"β {message}"
|
| 68 |
|
|
@@ -70,11 +109,12 @@ def save_to_oci_direct(image, prompt):
|
|
| 70 |
return f"β Direct upload failed: {str(e)}"
|
| 71 |
|
| 72 |
def save_to_oci_via_api(image, prompt):
|
| 73 |
-
"""Save image using the OCI API endpoint"""
|
| 74 |
try:
|
| 75 |
# Convert image to bytes
|
| 76 |
img_bytes = io.BytesIO()
|
| 77 |
image.save(img_bytes, format='PNG')
|
|
|
|
| 78 |
img_bytes.seek(0)
|
| 79 |
|
| 80 |
# Create filename
|
|
@@ -82,22 +122,22 @@ def save_to_oci_via_api(image, prompt):
|
|
| 82 |
safe_prompt = "".join(c for c in prompt[:30] if c.isalnum() or c in (' ', '-', '_')).rstrip()
|
| 83 |
filename = f"story_{timestamp}_{safe_prompt}.png"
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
# Use relative URL since we're in the same space
|
| 87 |
api_url = "/api/upload"
|
| 88 |
|
| 89 |
-
# For Hugging Face deployment,
|
| 90 |
try:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
except:
|
| 95 |
-
# Fallback to relative URL
|
| 96 |
pass
|
| 97 |
|
|
|
|
|
|
|
| 98 |
# Prepare form data for API request
|
| 99 |
files = {
|
| 100 |
-
'file': (filename,
|
| 101 |
}
|
| 102 |
|
| 103 |
data = {
|
|
@@ -105,23 +145,33 @@ def save_to_oci_via_api(image, prompt):
|
|
| 105 |
'subfolder': 'childrens-books'
|
| 106 |
}
|
| 107 |
|
| 108 |
-
# Make the API request
|
| 109 |
-
response = requests.post(api_url, files=files, data=data)
|
|
|
|
|
|
|
| 110 |
|
| 111 |
if response.status_code == 200:
|
| 112 |
result = response.json()
|
| 113 |
if result['status'] == 'success':
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
| 115 |
else:
|
| 116 |
return f"β API Error: {result.get('message', 'Unknown error')}"
|
| 117 |
else:
|
| 118 |
return f"β HTTP Error: {response.status_code} - {response.text}"
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
return f"β API upload failed: {str(e)}"
|
| 122 |
|
| 123 |
def save_to_oci(image, prompt):
|
| 124 |
"""Main function to save image to OCI using best available method"""
|
|
|
|
| 125 |
if DIRECT_OCI_ACCESS:
|
| 126 |
return save_to_oci_direct(image, prompt)
|
| 127 |
else:
|
|
@@ -130,6 +180,9 @@ def save_to_oci(image, prompt):
|
|
| 130 |
def generate_storybook_image(prompt):
|
| 131 |
"""Generate an image from text prompt and save to OCI"""
|
| 132 |
try:
|
|
|
|
|
|
|
|
|
|
| 133 |
# Enhance the prompt for better children's book style
|
| 134 |
enhanced_prompt = f"children's book illustration, colorful, whimsical, cute, {prompt}"
|
| 135 |
|
|
@@ -138,8 +191,10 @@ def generate_storybook_image(prompt):
|
|
| 138 |
# Generate image
|
| 139 |
image = pipe(
|
| 140 |
enhanced_prompt,
|
| 141 |
-
num_inference_steps=20,
|
| 142 |
-
guidance_scale=7.5
|
|
|
|
|
|
|
| 143 |
).images[0]
|
| 144 |
|
| 145 |
print("β
Image generated successfully!")
|
|
@@ -147,99 +202,75 @@ def generate_storybook_image(prompt):
|
|
| 147 |
# Save to OCI
|
| 148 |
print("πΎ Saving image to OCI storage...")
|
| 149 |
save_status = save_to_oci(image, prompt)
|
| 150 |
-
print(save_status)
|
| 151 |
|
| 152 |
return image, save_status
|
| 153 |
|
| 154 |
except Exception as e:
|
| 155 |
error_msg = f"β Generation failed: {str(e)}"
|
| 156 |
print(error_msg)
|
|
|
|
|
|
|
| 157 |
return None, error_msg
|
| 158 |
|
| 159 |
-
def batch_generate_storybook(scenes):
|
| 160 |
-
"""Generate multiple images for a storybook"""
|
| 161 |
-
if not scenes:
|
| 162 |
-
return [], "β Please provide at least one scene"
|
| 163 |
-
|
| 164 |
-
results = []
|
| 165 |
-
status_messages = []
|
| 166 |
-
|
| 167 |
-
for i, scene in enumerate(scenes):
|
| 168 |
-
if not scene.strip():
|
| 169 |
-
continue
|
| 170 |
-
|
| 171 |
-
print(f"π Generating scene {i+1}/{len(scenes)}: {scene}")
|
| 172 |
-
image, status = generate_storybook_image(scene)
|
| 173 |
-
|
| 174 |
-
if image:
|
| 175 |
-
results.append((f"Scene {i+1}: {scene}", image))
|
| 176 |
-
status_messages.append(f"Scene {i+1}: {status}")
|
| 177 |
-
|
| 178 |
-
return results, "\n".join(status_messages)
|
| 179 |
-
|
| 180 |
# Create the Gradio interface
|
| 181 |
with gr.Blocks(title="Children's Book Illustrator", theme="soft") as demo:
|
| 182 |
gr.Markdown("# π Children's Book Illustrator")
|
| 183 |
gr.Markdown("Generate beautiful storybook images and automatically save them to your OCI storage")
|
| 184 |
|
| 185 |
-
with gr.
|
| 186 |
-
with gr.
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
generate_btn = gr.Button("π¨ Generate Image", variant="primary")
|
| 194 |
-
|
| 195 |
-
with gr.Column():
|
| 196 |
-
image_output = gr.Image(label="Generated Image", height=400)
|
| 197 |
-
status_output = gr.Textbox(label="Status", interactive=False)
|
| 198 |
-
|
| 199 |
-
with gr.Tab("Batch Storybook Generation"):
|
| 200 |
-
with gr.Row():
|
| 201 |
-
with gr.Column():
|
| 202 |
-
scenes_input = gr.Textbox(
|
| 203 |
-
label="Story Scenes (One per line)",
|
| 204 |
-
placeholder="Enter each scene on a separate line...\nExample:\nA brave knight approaches the castle\nThe dragon guards a treasure chest\nChildren celebrating with the villagers",
|
| 205 |
-
lines=6
|
| 206 |
-
)
|
| 207 |
-
batch_generate_btn = gr.Button("π Generate Storybook", variant="primary")
|
| 208 |
-
|
| 209 |
-
with gr.Column():
|
| 210 |
-
batch_status = gr.Textbox(label="Generation Status", interactive=False, lines=10)
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
label="
|
| 215 |
-
columns=2,
|
| 216 |
-
height=600
|
| 217 |
-
)
|
| 218 |
|
| 219 |
-
|
|
|
|
| 220 |
gr.Markdown("""
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
# Connect buttons to functions
|
| 245 |
generate_btn.click(
|
|
@@ -248,23 +279,17 @@ with gr.Blocks(title="Children's Book Illustrator", theme="soft") as demo:
|
|
| 248 |
outputs=[image_output, status_output]
|
| 249 |
)
|
| 250 |
|
| 251 |
-
|
| 252 |
-
fn=
|
| 253 |
-
inputs=
|
| 254 |
-
outputs=
|
| 255 |
)
|
| 256 |
|
| 257 |
# For Hugging Face Spaces deployment
|
| 258 |
def get_app():
|
| 259 |
-
"""Return the Gradio app for Hugging Face"""
|
| 260 |
return demo
|
| 261 |
|
| 262 |
-
# For local testing
|
| 263 |
if __name__ == "__main__":
|
| 264 |
print("π Starting Children's Book Illustrator...")
|
| 265 |
print(f"π¦ OCI Access: {'Direct' if DIRECT_OCI_ACCESS else 'API'}")
|
| 266 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 267 |
-
else:
|
| 268 |
-
# For Hugging Face deployment
|
| 269 |
-
print("π¦ Hugging Face Space detected")
|
| 270 |
-
print(f"π§ OCI Access: {'Direct' if DIRECT_OCI_ACCESS else 'API'}")
|
|
|
|
| 8 |
from datetime import datetime
|
| 9 |
import re
|
| 10 |
import tempfile
|
| 11 |
+
import time
|
| 12 |
|
| 13 |
# Try to import your existing OCI connector for direct access
|
| 14 |
try:
|
|
|
|
| 15 |
from app import oci_connector
|
| 16 |
DIRECT_OCI_ACCESS = True
|
| 17 |
print("β
Direct OCI access available - using existing OCI connector")
|
|
|
|
| 27 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 28 |
"runwayml/stable-diffusion-v1-5",
|
| 29 |
torch_dtype=torch.float32,
|
| 30 |
+
safety_checker=None,
|
| 31 |
requires_safety_checker=False
|
| 32 |
).to("cpu")
|
| 33 |
print("β
Model loaded successfully!")
|
|
|
|
| 39 |
# Load the model once at startup
|
| 40 |
pipe = load_model()
|
| 41 |
|
| 42 |
+
def verify_oci_upload(object_name):
|
| 43 |
+
"""Verify that the file actually exists in OCI"""
|
| 44 |
+
try:
|
| 45 |
+
if DIRECT_OCI_ACCESS:
|
| 46 |
+
# Use direct OCI client to check if file exists
|
| 47 |
+
namespace = oci_connector.config.get('namespace')
|
| 48 |
+
bucket_name = oci_connector.config.get('bucket_name')
|
| 49 |
+
|
| 50 |
+
if not namespace or not bucket_name:
|
| 51 |
+
return False, "Namespace or bucket name not configured"
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
response = oci_connector.client.head_object(
|
| 55 |
+
namespace_name=namespace,
|
| 56 |
+
bucket_name=bucket_name,
|
| 57 |
+
object_name=object_name
|
| 58 |
+
)
|
| 59 |
+
return True, "β
File verified in OCI"
|
| 60 |
+
except oci.exceptions.ServiceError as e:
|
| 61 |
+
if e.status == 404:
|
| 62 |
+
return False, "β File not found in OCI (404)"
|
| 63 |
+
else:
|
| 64 |
+
return False, f"β OCI Error: {e.message}"
|
| 65 |
+
|
| 66 |
+
else:
|
| 67 |
+
# For API mode, we can't easily verify, so just return optimistic
|
| 68 |
+
return True, "β
Upload reported successful (verification not available)"
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return False, f"β Verification failed: {str(e)}"
|
| 72 |
+
|
| 73 |
def save_to_oci_direct(image, prompt):
|
| 74 |
+
"""Save image using direct OCI connector access with verification"""
|
| 75 |
try:
|
| 76 |
# Create temporary file
|
| 77 |
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
|
|
|
|
| 86 |
# Use project-based directory structure
|
| 87 |
object_name = f"storybook-generator/childrens-books/{filename}"
|
| 88 |
|
| 89 |
+
print(f"π€ Uploading to OCI: {object_name}")
|
| 90 |
+
|
| 91 |
# Upload using existing OCI connector
|
| 92 |
success, message = oci_connector.upload_file(temp_path, object_name, None)
|
| 93 |
|
|
|
|
| 95 |
os.unlink(temp_path)
|
| 96 |
|
| 97 |
if success:
|
| 98 |
+
# Verify the upload actually worked
|
| 99 |
+
print("π Verifying OCI upload...")
|
| 100 |
+
verified, verify_msg = verify_oci_upload(object_name)
|
| 101 |
+
if verified:
|
| 102 |
+
return f"β
{message} | {verify_msg}"
|
| 103 |
+
else:
|
| 104 |
+
return f"β οΈ {message} BUT {verify_msg}"
|
| 105 |
else:
|
| 106 |
return f"β {message}"
|
| 107 |
|
|
|
|
| 109 |
return f"β Direct upload failed: {str(e)}"
|
| 110 |
|
| 111 |
def save_to_oci_via_api(image, prompt):
|
| 112 |
+
"""Save image using the OCI API endpoint with better error handling"""
|
| 113 |
try:
|
| 114 |
# Convert image to bytes
|
| 115 |
img_bytes = io.BytesIO()
|
| 116 |
image.save(img_bytes, format='PNG')
|
| 117 |
+
img_data = img_bytes.getvalue()
|
| 118 |
img_bytes.seek(0)
|
| 119 |
|
| 120 |
# Create filename
|
|
|
|
| 122 |
safe_prompt = "".join(c for c in prompt[:30] if c.isalnum() or c in (' ', '-', '_')).rstrip()
|
| 123 |
filename = f"story_{timestamp}_{safe_prompt}.png"
|
| 124 |
|
| 125 |
+
# Use relative URL for same-space API calls
|
|
|
|
| 126 |
api_url = "/api/upload"
|
| 127 |
|
| 128 |
+
# For Hugging Face deployment, use absolute URL if needed
|
| 129 |
try:
|
| 130 |
+
space_name = os.environ.get('SPACE_NAME', 'yukee1992-sd-xl-book-illustrator')
|
| 131 |
+
if 'HF_SPACE_ID' in os.environ:
|
| 132 |
+
api_url = f"https://{space_name}.hf.space/api/upload"
|
| 133 |
except:
|
|
|
|
| 134 |
pass
|
| 135 |
|
| 136 |
+
print(f"π Calling OCI API: {api_url}")
|
| 137 |
+
|
| 138 |
# Prepare form data for API request
|
| 139 |
files = {
|
| 140 |
+
'file': (filename, img_data, 'image/png')
|
| 141 |
}
|
| 142 |
|
| 143 |
data = {
|
|
|
|
| 145 |
'subfolder': 'childrens-books'
|
| 146 |
}
|
| 147 |
|
| 148 |
+
# Make the API request with timeout
|
| 149 |
+
response = requests.post(api_url, files=files, data=data, timeout=30)
|
| 150 |
+
|
| 151 |
+
print(f"π¨ API Response: {response.status_code}")
|
| 152 |
|
| 153 |
if response.status_code == 200:
|
| 154 |
result = response.json()
|
| 155 |
if result['status'] == 'success':
|
| 156 |
+
# Try to verify the file exists
|
| 157 |
+
object_name = result.get('object_name', f"storybook-generator/childrens-books/{filename}")
|
| 158 |
+
verified, verify_msg = verify_oci_upload(object_name)
|
| 159 |
+
return f"β
{result['message']} | {verify_msg}"
|
| 160 |
else:
|
| 161 |
return f"β API Error: {result.get('message', 'Unknown error')}"
|
| 162 |
else:
|
| 163 |
return f"β HTTP Error: {response.status_code} - {response.text}"
|
| 164 |
|
| 165 |
+
except requests.exceptions.Timeout:
|
| 166 |
+
return "β API call timed out (30s)"
|
| 167 |
+
except requests.exceptions.ConnectionError:
|
| 168 |
+
return "β Cannot connect to OCI API"
|
| 169 |
except Exception as e:
|
| 170 |
return f"β API upload failed: {str(e)}"
|
| 171 |
|
| 172 |
def save_to_oci(image, prompt):
|
| 173 |
"""Main function to save image to OCI using best available method"""
|
| 174 |
+
print("πΎ Starting OCI save process...")
|
| 175 |
if DIRECT_OCI_ACCESS:
|
| 176 |
return save_to_oci_direct(image, prompt)
|
| 177 |
else:
|
|
|
|
| 180 |
def generate_storybook_image(prompt):
|
| 181 |
"""Generate an image from text prompt and save to OCI"""
|
| 182 |
try:
|
| 183 |
+
if not prompt or not prompt.strip():
|
| 184 |
+
return None, "β Please enter a scene description"
|
| 185 |
+
|
| 186 |
# Enhance the prompt for better children's book style
|
| 187 |
enhanced_prompt = f"children's book illustration, colorful, whimsical, cute, {prompt}"
|
| 188 |
|
|
|
|
| 191 |
# Generate image
|
| 192 |
image = pipe(
|
| 193 |
enhanced_prompt,
|
| 194 |
+
num_inference_steps=20,
|
| 195 |
+
guidance_scale=7.5,
|
| 196 |
+
width=512,
|
| 197 |
+
height=512
|
| 198 |
).images[0]
|
| 199 |
|
| 200 |
print("β
Image generated successfully!")
|
|
|
|
| 202 |
# Save to OCI
|
| 203 |
print("πΎ Saving image to OCI storage...")
|
| 204 |
save_status = save_to_oci(image, prompt)
|
| 205 |
+
print(f"Save status: {save_status}")
|
| 206 |
|
| 207 |
return image, save_status
|
| 208 |
|
| 209 |
except Exception as e:
|
| 210 |
error_msg = f"β Generation failed: {str(e)}"
|
| 211 |
print(error_msg)
|
| 212 |
+
import traceback
|
| 213 |
+
print(f"Traceback: {traceback.format_exc()}")
|
| 214 |
return None, error_msg
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
# Create the Gradio interface
|
| 217 |
with gr.Blocks(title="Children's Book Illustrator", theme="soft") as demo:
|
| 218 |
gr.Markdown("# π Children's Book Illustrator")
|
| 219 |
gr.Markdown("Generate beautiful storybook images and automatically save them to your OCI storage")
|
| 220 |
|
| 221 |
+
with gr.Row():
|
| 222 |
+
with gr.Column():
|
| 223 |
+
prompt_input = gr.Textbox(
|
| 224 |
+
label="Scene Description",
|
| 225 |
+
placeholder="Describe a scene for your storybook...\nExample: A dragon reading a book under a magical tree",
|
| 226 |
+
lines=3
|
| 227 |
+
)
|
| 228 |
+
generate_btn = gr.Button("π¨ Generate Image", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
with gr.Column():
|
| 231 |
+
image_output = gr.Image(label="Generated Image", height=400)
|
| 232 |
+
status_output = gr.Textbox(label="Status", interactive=False, lines=3)
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
+
# Debug section
|
| 235 |
+
with gr.Accordion("π§ Debug Information", open=False):
|
| 236 |
gr.Markdown("""
|
| 237 |
+
**Current Configuration:**
|
| 238 |
+
- OCI Access: **{oci_mode}**
|
| 239 |
+
- Model: Stable Diffusion v1.5
|
| 240 |
+
- Storage Path: `storybook-generator/childrens-books/`
|
| 241 |
+
""".format(oci_mode="Direct" if DIRECT_OCI_ACCESS else "API"))
|
| 242 |
|
| 243 |
+
debug_btn = gr.Button("π Check OCI Connection", variant="secondary")
|
| 244 |
+
debug_output = gr.Textbox(label="Debug Info", interactive=False, lines=6)
|
| 245 |
+
|
| 246 |
+
def check_oci_connection():
|
| 247 |
+
"""Check OCI connection status"""
|
| 248 |
+
try:
|
| 249 |
+
if DIRECT_OCI_ACCESS:
|
| 250 |
+
# Test direct OCI connection
|
| 251 |
+
namespace = oci_connector.config.get('namespace', 'Not set')
|
| 252 |
+
bucket = oci_connector.config.get('bucket_name', 'Not set')
|
| 253 |
+
client_status = "β
Connected" if oci_connector.client else "β Disconnected"
|
| 254 |
+
|
| 255 |
+
return f"""**Direct OCI Connection:**
|
| 256 |
+
- Client: {client_status}
|
| 257 |
+
- Namespace: {namespace}
|
| 258 |
+
- Bucket: {bucket}
|
| 259 |
+
- Initialization Error: {oci_connector.initialization_error or 'None'}"""
|
| 260 |
+
else:
|
| 261 |
+
# Test API connection
|
| 262 |
+
try:
|
| 263 |
+
api_url = "/api/health"
|
| 264 |
+
response = requests.get(api_url, timeout=10)
|
| 265 |
+
if response.status_code == 200:
|
| 266 |
+
return "β
API Connection: Healthy"
|
| 267 |
+
else:
|
| 268 |
+
return f"β API Connection: HTTP {response.status_code}"
|
| 269 |
+
except Exception as e:
|
| 270 |
+
return f"β API Connection: {str(e)}"
|
| 271 |
+
|
| 272 |
+
except Exception as e:
|
| 273 |
+
return f"β Connection check failed: {str(e)}"
|
| 274 |
|
| 275 |
# Connect buttons to functions
|
| 276 |
generate_btn.click(
|
|
|
|
| 279 |
outputs=[image_output, status_output]
|
| 280 |
)
|
| 281 |
|
| 282 |
+
debug_btn.click(
|
| 283 |
+
fn=check_oci_connection,
|
| 284 |
+
inputs=None,
|
| 285 |
+
outputs=debug_output
|
| 286 |
)
|
| 287 |
|
| 288 |
# For Hugging Face Spaces deployment
|
| 289 |
def get_app():
|
|
|
|
| 290 |
return demo
|
| 291 |
|
|
|
|
| 292 |
if __name__ == "__main__":
|
| 293 |
print("π Starting Children's Book Illustrator...")
|
| 294 |
print(f"π¦ OCI Access: {'Direct' if DIRECT_OCI_ACCESS else 'API'}")
|
| 295 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|