Spaces:
Sleeping
Sleeping
lukestanley
commited on
Commit
·
d2c07f7
1
Parent(s):
44bab49
Log to remote server in non-blocking way
Browse files
data.py
CHANGED
@@ -1,6 +1,36 @@
|
|
|
|
1 |
import json
|
|
|
|
|
2 |
|
3 |
def log_to_jsonl(file_path, data):
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
import json
|
3 |
+
import os
|
4 |
+
import threading
|
5 |
|
6 |
def log_to_jsonl(file_path, data):
|
7 |
+
def _log_to_jsonl():
|
8 |
+
# Read the URL of the Gradio app from an environment variable
|
9 |
+
url = os.environ.get("SAVE_URL")
|
10 |
+
if url is None:
|
11 |
+
raise ValueError("SAVE_URL environment variable not set")
|
12 |
+
|
13 |
+
# Serialize the data to a JSON string
|
14 |
+
json_data = json.dumps({"file_path": file_path, "data": data})
|
15 |
+
|
16 |
+
# Create a dictionary with the JSON data as the value of a field named "data"
|
17 |
+
request_body = {"data": json_data}
|
18 |
+
|
19 |
+
# Convert the request body to a JSON string
|
20 |
+
json_data = json.dumps(request_body)
|
21 |
+
|
22 |
+
# Make the HTTP POST request
|
23 |
+
try:
|
24 |
+
response = requests.post(url, data=json_data, headers={"Content-Type": "application/json"})
|
25 |
+
|
26 |
+
# Check if the request was successful
|
27 |
+
if response.status_code == 200:
|
28 |
+
print("Data saved successfully!")
|
29 |
+
else:
|
30 |
+
print("Error saving data:", response.text, response.status_code)
|
31 |
+
except Exception as e:
|
32 |
+
print("Unexpected error saving", e, url, json_data)
|
33 |
+
|
34 |
+
# Create a new thread and start it
|
35 |
+
thread = threading.Thread(target=_log_to_jsonl)
|
36 |
+
thread.start()
|