Spaces:
Sleeping
Sleeping
lukestanley
commited on
Commit
•
2c65c23
1
Parent(s):
cf4506f
Add data capture endpoint using Gradio's API hosted by HF's Gradio dynamic hostname
Browse files- data_saver_server.py +30 -0
data_saver_server.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import threading
|
4 |
+
|
5 |
+
# Function to save data to disk in a non-blocking manner
|
6 |
+
def save_data_to_disk(data):
|
7 |
+
# Append data to a JSONL file
|
8 |
+
with open("data.jsonl", "a") as file:
|
9 |
+
file.write(json.dumps(data) + "\n")
|
10 |
+
|
11 |
+
# Wrapper function to make `save_data_to_disk` non-blocking
|
12 |
+
def save_data(data):
|
13 |
+
# Start a new thread to handle the saving process
|
14 |
+
thread = threading.Thread(target=save_data_to_disk, args=(data,))
|
15 |
+
thread.start()
|
16 |
+
# Return a simple confirmation message
|
17 |
+
return "Data is being saved."
|
18 |
+
|
19 |
+
# Create a Gradio interface
|
20 |
+
interface = gr.Interface(
|
21 |
+
fn=save_data,
|
22 |
+
inputs=gr.JSON(label="Input JSON Data"),
|
23 |
+
outputs="text",
|
24 |
+
title="Data Saving Service",
|
25 |
+
description="A simple Gradio app to save arbitrary JSON data in the background.",
|
26 |
+
)
|
27 |
+
|
28 |
+
# Run the Gradio app
|
29 |
+
if __name__ == "__main__":
|
30 |
+
interface.launch(server_name="0.0.0.0", server_port=8435, share=True)
|