File size: 1,065 Bytes
a01d89e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import requests
import gradio as gr
import time
from IPython.display import Javascript, display
def get_public_ip():
response = requests.get('https://api.ipify.org?format=json')
return response.json()["ip"]
def keep_alive():
# Inject JavaScript to simulate minor interaction within the notebook
js_code = """
var element = document.querySelector('.output_scroll');
if (element) {
element.scrollBy(0, 10);
}
"""
return jsonify(js=js_code)
# Gradio interface with public IP and keepalive endpoint
iface = gr.Interface(
fn=[get_public_ip, keep_alive],
inputs=[],
outputs=["text", "json"],
title="Public IP Retriever (And Experimental Keep-Alive)",
description="Displays the approximate public IP address associated with this Colab notebook. The '/keepalive' endpoint might help prevent idle timeouts (use with caution)."
)
iface.launch(share=True)
# Initial interaction
display(Javascript("""
var element = document.querySelector('.output_scroll');
if (element) {
element.scrollBy(0, 10);
}
"""))
|