Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,41 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
def predict(text, request: gr.Request):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
return {
|
5 |
-
"ip":
|
6 |
-
"user_agent": request.headers["user-agent"]
|
|
|
|
|
7 |
}
|
8 |
|
9 |
demo = gr.Interface(fn=predict, inputs="text", outputs="json")
|
10 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import pytz
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
def ts_to_str(timestamp, timezone):
|
7 |
+
# Create a timezone-aware datetime object from the UNIX timestamp
|
8 |
+
dt = datetime.fromtimestamp(timestamp, pytz.utc)
|
9 |
+
|
10 |
+
# Convert the timezone-aware datetime object to the target timezone
|
11 |
+
target_timezone = pytz.timezone(timezone)
|
12 |
+
localized_dt = dt.astimezone(target_timezone)
|
13 |
+
|
14 |
+
# Format the datetime object to the specified string format
|
15 |
+
return localized_dt.strftime('%Y-%m-%d %H:%M:%S (%Z)')
|
16 |
+
|
17 |
|
18 |
def predict(text, request: gr.Request):
|
19 |
+
# Get the IP address from the request object
|
20 |
+
ip_address = request.client.host
|
21 |
+
|
22 |
+
# Call the WorldTimeAPI to get the timezone for the user's IP address
|
23 |
+
response = requests.get(f'http://worldtimeapi.org/api/ip/{ip_address}')
|
24 |
+
time_data = response.json()
|
25 |
+
|
26 |
+
# Extract the timezone from the API response
|
27 |
+
timezone_str = time_data.get('timezone', 'UTC')
|
28 |
+
|
29 |
+
# Get the current timestamp and convert it to the user's local time
|
30 |
+
timestamp = datetime.now().timestamp()
|
31 |
+
current_time_local = ts_to_str(timestamp, timezone_str)
|
32 |
+
|
33 |
return {
|
34 |
+
"ip": ip_address,
|
35 |
+
"user_agent": request.headers["user-agent"],
|
36 |
+
"current_time_local": current_time_local,
|
37 |
+
"timezone": timezone_str
|
38 |
}
|
39 |
|
40 |
demo = gr.Interface(fn=predict, inputs="text", outputs="json")
|
41 |
+
demo.launch()
|