|
|
import gradio as gr |
|
|
import requests |
|
|
import random |
|
|
import time |
|
|
|
|
|
|
|
|
_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> |
|
|
''' |
|
|
|
|
|
|
|
|
API_URLS = [ |
|
|
"https://api.zmkj.site/", |
|
|
"https://api1.zmkj.site/", |
|
|
] |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 = [] |
|
|
|
|
|
|
|
|
for url in API_URLS: |
|
|
if check_url_status(url): |
|
|
available_urls.append(url) |
|
|
|
|
|
iframe_src = "about:blank" |
|
|
display_iframe = "none" |
|
|
loading_message_style = "none" |
|
|
error_message_style = "none" |
|
|
|
|
|
if available_urls: |
|
|
|
|
|
selected_url = random.choice(available_urls) |
|
|
iframe_src = selected_url |
|
|
display_iframe = "block" |
|
|
loading_message_style = "none" |
|
|
print(f"Loading iframe from URL: {selected_url}") |
|
|
else: |
|
|
|
|
|
loading_message_style = "none" |
|
|
error_message_style = "block" |
|
|
print("Sorry, no API URL available.") |
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown(_TITLE) |
|
|
gr.HTML(_DESCRIPTION_HTML) |
|
|
|
|
|
gr.HTML(value=get_gradio_iframe_content()) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(share = True) |