VanNguyen1214's picture
Update app.py
04ca05b verified
import gradio as gr
import requests
import random
import time
# Title and description
_TITLE = '''# Generate 3D from 2D image'''
_DESCRIPTION_HTML = '''
<div style="font-size: 24px;">
<a href="https://github.com/n2van" target="_blank" rel="noopener noreferrer">GitHub</a> |
<a href="https://linkedin.com/in/van-nguyen-a1b844254" target="_blank" rel="noopener noreferrer">LinkedIn</a> |
<a href="mailto:nnvan0311@gmail.com">Email</a>
</div>
'''
# List of Gradio API URLs
API_URLS = [
"https://api.zmkj.site/",
"https://api1.zmkj.site/",
]
# Function to check URL status
def check_url_status(url, timeout=5):
"""
Checks if a given URL responds within the specified timeout.
Returns True if the URL responds successfully, False otherwise.
"""
try:
response = requests.get(url, timeout=timeout)
return response.ok
except requests.exceptions.Timeout:
print(f"URL {url} took too long to respond (timeout: {timeout}s).")
return False
except requests.exceptions.RequestException as e:
print(f"Error checking URL {url}: {e}")
return False
# Main function to generate the HTML content for the Gradio iframe
def get_gradio_iframe_content():
"""
Checks the API URLs and returns the HTML for the iframe with an active URL.
If no URLs are active, it returns HTML with an error message.
"""
available_urls = []
# Check each URL and collect the ones that are active
for url in API_URLS:
if check_url_status(url):
available_urls.append(url)
iframe_src = "about:blank"
display_iframe = "none" # Default to hidden iframe
loading_message_style = "none"
error_message_style = "none"
if available_urls:
# If at least one URL is active, choose one randomly
selected_url = random.choice(available_urls)
iframe_src = selected_url
display_iframe = "block" # Show the iframe
loading_message_style = "none" # Hide the loading message
print(f"Loading iframe from URL: {selected_url}")
else:
# If no URLs are active
loading_message_style = "none"
error_message_style = "block" # Show the error message
print("Sorry, no API URL available.")
# Generate the HTML content for the iframe and status messages
# All custom CSS has been removed. Layout will rely on default browser/Gradio styles.
html_content = f'''
<iframe id="myIframe" src="{iframe_src}" width="100%" height="1185px" frameborder="0" scrolling="no" style="display: {display_iframe};"></iframe>
<div id="loadingMessage" style="display: {loading_message_style}; text-align: center; margin-top: 20px;">
Loading service...
</div>
<div id="errorMessage" style="display: {error_message_style}; text-align: center; margin-top: 20px; color: red;">
Sorry service not available
</div>
<script>
// This JavaScript is for handling the loading/error messages within the iframe's HTML
// It hides the loading message when the iframe finishes loading
// Or shows an error if the iframe fails to load after being selected.
const iframe = document.getElementById('myIframe');
const loadingMsg = document.getElementById('loadingMessage');
const errorMsg = document.getElementById('errorMessage');
if (iframe.src !== 'about:blank' && loadingMsg.style.display === 'block') {{
iframe.onload = function() {{
loadingMsg.style.display = 'none'; // Hide loading when iframe loads
iframe.style.display = 'block'; // Ensure iframe is visible
}};
// Fallback if the iframe encounters a loading error after being chosen
iframe.onerror = function() {{
loadingMsg.style.display = 'none';
iframe.style.display = 'none';
errorMsg.style.display = 'block'; // Show error
}};
}}
</script>
'''
return html_content
# Initialize the Gradio interface
with gr.Blocks() as demo:
gr.Markdown(_TITLE)
gr.HTML(_DESCRIPTION_HTML)
gr.HTML(value=get_gradio_iframe_content())
# Launch the Gradio application
if __name__ == "__main__":
demo.launch(share = True)