Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,53 @@
|
|
1 |
import gradio
|
|
|
2 |
|
3 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio
|
2 |
+
import os
|
3 |
|
4 |
+
# basic storage using /dat
|
5 |
+
def store_data(data):
|
6 |
+
if not data:
|
7 |
+
return "no request", 400
|
8 |
+
if not data["filename"]:
|
9 |
+
return "no filename", 400
|
10 |
+
if not data["content"]:
|
11 |
+
# delete the file
|
12 |
+
os.remove(f"/dat/{data['filename']}.txt")
|
13 |
+
return "file deleted", 200
|
14 |
+
with open(f"/dat/{data['filename']}.txt", "w") as f:
|
15 |
+
f.write(data["content"])
|
16 |
+
return "file saved", 200
|
17 |
+
return "error", 500 # should not happen
|
18 |
+
|
19 |
+
def load_data(data):
|
20 |
+
if not data:
|
21 |
+
return "no request", 400
|
22 |
+
if not data["filename"]:
|
23 |
+
return "no filename", 400
|
24 |
+
try:
|
25 |
+
with open(f"/dat/{data['filename']}.txt", "r") as f:
|
26 |
+
return f.read(), 200
|
27 |
+
except FileNotFoundError:
|
28 |
+
return "", 404 # file not found, it was last set to empty string
|
29 |
+
return "error", 500 # should not happen
|
30 |
+
|
31 |
+
# that's it, now we host the API
|
32 |
+
iface = gradio.Interface(
|
33 |
+
fn=store_data,
|
34 |
+
inputs=[
|
35 |
+
gradio.inputs.Textbox(label="filename"),
|
36 |
+
gradio.inputs.Textbox(label="content")
|
37 |
+
],
|
38 |
+
outputs=gradio.outputs.Textbox(label="status")
|
39 |
+
)
|
40 |
+
iface.launch()
|
41 |
+
|
42 |
+
iface = gradio.Interface(
|
43 |
+
fn=load_data,
|
44 |
+
inputs=[
|
45 |
+
gradio.inputs.Textbox(label="filename")
|
46 |
+
],
|
47 |
+
outputs=gradio.outputs.Textbox(label="content")
|
48 |
+
)
|
49 |
+
iface.launch()
|
50 |
+
|
51 |
+
# now you can use the API to store and load data
|
52 |
+
# POST /store_data {"filename": "test", "content": "hello world"}
|
53 |
+
# GET /load_data?filename=test
|