Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,49 @@
|
|
1 |
import gradio as gr
|
2 |
from fastapi import FastAPI, Request
|
3 |
import uvicorn
|
|
|
4 |
|
5 |
-
# Initialize FastAPI and Gradio
|
6 |
app = FastAPI()
|
7 |
-
gr_interface = None
|
8 |
STORAGE_PATH = "storage.txt"
|
9 |
|
10 |
-
#
|
11 |
def init_storage():
|
12 |
-
|
13 |
-
with open(STORAGE_PATH, "
|
14 |
f.write("")
|
15 |
-
except FileExistsError:
|
16 |
-
pass
|
17 |
|
18 |
init_storage()
|
19 |
|
20 |
-
# Function to read stored data
|
21 |
def read_data():
|
22 |
with open(STORAGE_PATH, "r") as f:
|
23 |
return f.read()
|
24 |
|
25 |
-
# Function to write data
|
26 |
def write_data(text):
|
27 |
with open(STORAGE_PATH, "w") as f:
|
28 |
f.write(text)
|
29 |
|
30 |
-
# Gradio UI
|
31 |
def show_latest_data():
|
32 |
return read_data()
|
33 |
|
34 |
-
# Set up the Gradio Blocks interface
|
35 |
with gr.Blocks() as gr_app:
|
36 |
gr.Markdown("## Latest Sent Text")
|
37 |
output = gr.Textbox(label="Stored Text", value=read_data(), interactive=False)
|
38 |
refresh_btn = gr.Button("Refresh")
|
39 |
-
|
40 |
refresh_btn.click(fn=show_latest_data, inputs=[], outputs=output)
|
41 |
|
42 |
-
#
|
43 |
@app.get("/store")
|
44 |
async def store_text(request: Request):
|
45 |
text = request.query_params.get("text")
|
46 |
if text:
|
47 |
write_data(text)
|
48 |
return {"status": "success", "stored": text}
|
49 |
-
|
50 |
-
return {"status": "error", "message": "No text parameter provided."}
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
return gr_app
|
55 |
|
56 |
-
#
|
57 |
if __name__ == "__main__":
|
58 |
-
gr_interface = gr.mount_gradio_app(app, gr_app, path="/")
|
59 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
1 |
import gradio as gr
|
2 |
from fastapi import FastAPI, Request
|
3 |
import uvicorn
|
4 |
+
import os
|
5 |
|
|
|
6 |
app = FastAPI()
|
|
|
7 |
STORAGE_PATH = "storage.txt"
|
8 |
|
9 |
+
# Ensure storage file exists
|
10 |
def init_storage():
|
11 |
+
if not os.path.exists(STORAGE_PATH):
|
12 |
+
with open(STORAGE_PATH, "w") as f:
|
13 |
f.write("")
|
|
|
|
|
14 |
|
15 |
init_storage()
|
16 |
|
|
|
17 |
def read_data():
|
18 |
with open(STORAGE_PATH, "r") as f:
|
19 |
return f.read()
|
20 |
|
|
|
21 |
def write_data(text):
|
22 |
with open(STORAGE_PATH, "w") as f:
|
23 |
f.write(text)
|
24 |
|
25 |
+
# Gradio UI
|
26 |
def show_latest_data():
|
27 |
return read_data()
|
28 |
|
|
|
29 |
with gr.Blocks() as gr_app:
|
30 |
gr.Markdown("## Latest Sent Text")
|
31 |
output = gr.Textbox(label="Stored Text", value=read_data(), interactive=False)
|
32 |
refresh_btn = gr.Button("Refresh")
|
|
|
33 |
refresh_btn.click(fn=show_latest_data, inputs=[], outputs=output)
|
34 |
|
35 |
+
# Store endpoint
|
36 |
@app.get("/store")
|
37 |
async def store_text(request: Request):
|
38 |
text = request.query_params.get("text")
|
39 |
if text:
|
40 |
write_data(text)
|
41 |
return {"status": "success", "stored": text}
|
42 |
+
return {"status": "error", "message": "No text parameter provided."}
|
|
|
43 |
|
44 |
+
# Mount Gradio to FastAPI
|
45 |
+
gr.mount_gradio_app(app, gr_app, path="/")
|
|
|
46 |
|
47 |
+
# Local dev support
|
48 |
if __name__ == "__main__":
|
|
|
49 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|