Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,61 +1,93 @@
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from datetime import datetime
|
|
|
5 |
|
6 |
import gradio as gr
|
7 |
-
from fastapi import FastAPI
|
8 |
from fastapi.staticfiles import StaticFiles
|
9 |
import uvicorn
|
10 |
|
|
|
11 |
import spaces
|
12 |
from spaces.zero.client import _get_token
|
13 |
|
|
|
|
|
|
|
14 |
app = FastAPI()
|
15 |
|
16 |
-
# Create and mount a static directory if needed
|
17 |
static_dir = Path("./static")
|
18 |
static_dir.mkdir(parents=True, exist_ok=True)
|
19 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
@spaces.GPU(duration=4
|
25 |
def process_text(text):
|
|
|
26 |
time.sleep(10)
|
27 |
return text.upper()
|
28 |
|
29 |
-
def process_and_save(request: gr.Request, text):
|
|
|
30 |
token = _get_token(request)
|
31 |
-
payload = token.split(
|
32 |
payload = f"{payload}{'=' * ((4 - len(payload) % 4) % 4)}"
|
33 |
payload = json.loads(base64.urlsafe_b64decode(payload).decode())
|
34 |
print(f"Token payload: {payload}")
|
35 |
|
36 |
-
|
37 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
38 |
file_path = static_dir / f"output_{timestamp}.txt"
|
39 |
with open(file_path, "w") as f:
|
40 |
-
f.write(
|
|
|
41 |
return gr.File(value=str(file_path))
|
42 |
|
|
|
43 |
process_and_save.zerogpu = True
|
44 |
|
|
|
|
|
|
|
45 |
with gr.Blocks() as demo:
|
46 |
text_input = gr.Textbox(label="Enter some text")
|
47 |
submit_btn = gr.Button("Process and Download")
|
48 |
output = gr.File(label="Download Processed File")
|
49 |
|
50 |
-
submit_btn.click(
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
|
|
|
|
53 |
app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=True)
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
app.mount("/assets", StaticFiles(directory=str(assets_dir)), name="assets")
|
59 |
-
|
60 |
if __name__ == "__main__":
|
61 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
1 |
+
###############################################################################
|
2 |
+
# 1) Set environment variables BEFORE importing Gradio (if you actually need them).
|
3 |
+
# In many cases, you can omit these entirely.
|
4 |
+
###############################################################################
|
5 |
import os
|
6 |
+
|
7 |
+
# If you want Gradio to run on a particular host/port, you can do this:
|
8 |
+
os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
|
9 |
+
os.environ["GRADIO_SERVER_PORT"] = "7860"
|
10 |
+
|
11 |
+
# If you do NOT really need GRADIO_ROOT_PATH, don’t set it.
|
12 |
+
# If you do set it, do so BEFORE the Gradio import, e.g.:
|
13 |
+
# os.environ["GRADIO_ROOT_PATH"] = "/_app/immutable"
|
14 |
+
|
15 |
+
###############################################################################
|
16 |
+
# 2) Now import everything
|
17 |
+
###############################################################################
|
18 |
+
import time
|
19 |
+
import json
|
20 |
+
import base64
|
21 |
from datetime import datetime
|
22 |
+
from pathlib import Path
|
23 |
|
24 |
import gradio as gr
|
25 |
+
from fastapi import FastAPI, Request
|
26 |
from fastapi.staticfiles import StaticFiles
|
27 |
import uvicorn
|
28 |
|
29 |
+
# Hugging Face Spaces
|
30 |
import spaces
|
31 |
from spaces.zero.client import _get_token
|
32 |
|
33 |
+
###############################################################################
|
34 |
+
# 3) Create your FastAPI app and (optionally) mount a static folder for user files
|
35 |
+
###############################################################################
|
36 |
app = FastAPI()
|
37 |
|
|
|
38 |
static_dir = Path("./static")
|
39 |
static_dir.mkdir(parents=True, exist_ok=True)
|
40 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
41 |
|
42 |
+
###############################################################################
|
43 |
+
# 4) Define your GPU function and main processing function
|
44 |
+
###############################################################################
|
45 |
+
@spaces.GPU(duration=240) # specify GPU usage for 4 minutes
|
46 |
def process_text(text):
|
47 |
+
"""Simulate a GPU-based process."""
|
48 |
time.sleep(10)
|
49 |
return text.upper()
|
50 |
|
51 |
+
def process_and_save(request: gr.Request, text: str):
|
52 |
+
"""Handles GPU call and writes result to a file in ./static."""
|
53 |
token = _get_token(request)
|
54 |
+
payload = token.split(".")[1]
|
55 |
payload = f"{payload}{'=' * ((4 - len(payload) % 4) % 4)}"
|
56 |
payload = json.loads(base64.urlsafe_b64decode(payload).decode())
|
57 |
print(f"Token payload: {payload}")
|
58 |
|
59 |
+
result = process_text(text)
|
60 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
61 |
file_path = static_dir / f"output_{timestamp}.txt"
|
62 |
with open(file_path, "w") as f:
|
63 |
+
f.write(result)
|
64 |
+
|
65 |
return gr.File(value=str(file_path))
|
66 |
|
67 |
+
# Mark as not requiring GPU
|
68 |
process_and_save.zerogpu = True
|
69 |
|
70 |
+
###############################################################################
|
71 |
+
# 5) Build the Gradio Blocks interface
|
72 |
+
###############################################################################
|
73 |
with gr.Blocks() as demo:
|
74 |
text_input = gr.Textbox(label="Enter some text")
|
75 |
submit_btn = gr.Button("Process and Download")
|
76 |
output = gr.File(label="Download Processed File")
|
77 |
|
78 |
+
submit_btn.click(
|
79 |
+
fn=process_and_save,
|
80 |
+
inputs=[text_input],
|
81 |
+
outputs=output
|
82 |
+
)
|
83 |
|
84 |
+
###############################################################################
|
85 |
+
# 6) Mount the Gradio app WITH SSR. Don’t manually mount _app/immutable.
|
86 |
+
###############################################################################
|
87 |
app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=True)
|
88 |
|
89 |
+
###############################################################################
|
90 |
+
# 7) Run with Uvicorn
|
91 |
+
###############################################################################
|
|
|
|
|
92 |
if __name__ == "__main__":
|
93 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|