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