Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,14 +5,15 @@ import base64
|
|
5 |
from datetime import datetime
|
6 |
|
7 |
import gradio as gr
|
8 |
-
from fastapi import FastAPI, Request
|
9 |
from fastapi.staticfiles import StaticFiles
|
10 |
import uvicorn
|
|
|
11 |
|
12 |
import spaces
|
13 |
from spaces.zero.client import _get_token
|
14 |
|
15 |
-
# Set
|
16 |
os.environ["GRADIO_SSR_MODE"] = "True"
|
17 |
os.environ["GRADIO_SERVER_PORT"] = "7860"
|
18 |
os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
|
@@ -24,47 +25,46 @@ app = FastAPI()
|
|
24 |
# Create and configure static directory
|
25 |
static_dir = Path("./static")
|
26 |
static_dir.mkdir(parents=True, exist_ok=True)
|
27 |
-
|
28 |
-
# Mount static directory to FastAPI
|
29 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
30 |
-
|
31 |
-
# Tell Gradio which paths are allowed to be served
|
32 |
os.environ["GRADIO_ALLOWED_PATHS"] = str(static_dir.resolve())
|
33 |
|
34 |
@spaces.GPU(duration=4*60)
|
35 |
def process_text(text):
|
36 |
-
"""Example GPU function - in reality, this might be model inference"""
|
37 |
return text.upper()
|
38 |
|
39 |
def process_and_save(request: gr.Request, text):
|
40 |
-
"""Main processing function that handles tokens and calls GPU function"""
|
41 |
token = _get_token(request)
|
42 |
payload = token.split('.')[1]
|
43 |
payload = f"{payload}{'=' * ((4 - len(payload) % 4) % 4)}"
|
44 |
payload = json.loads(base64.urlsafe_b64decode(payload).decode())
|
45 |
print(f"Token payload: {payload}")
|
46 |
-
|
47 |
processed_text = process_text(text)
|
48 |
-
return processed_text
|
49 |
|
50 |
-
# Mark main function as not requiring GPU
|
51 |
process_and_save.zerogpu = True
|
52 |
|
53 |
-
# Create Gradio interface
|
54 |
with gr.Blocks() as demo:
|
55 |
text_input = gr.Textbox(label="Enter some text")
|
56 |
submit_btn = gr.Button("Process and Download")
|
57 |
output = gr.Textbox(label="Output")
|
58 |
|
59 |
-
submit_btn.click(
|
60 |
-
fn=process_and_save,
|
61 |
-
inputs=[text_input],
|
62 |
-
outputs=output
|
63 |
-
)
|
64 |
|
65 |
-
# Mount Gradio app
|
66 |
app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=True, node_port=7861)
|
67 |
|
68 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
if __name__ == "__main__":
|
70 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
5 |
from datetime import datetime
|
6 |
|
7 |
import gradio as gr
|
8 |
+
from fastapi import FastAPI, Request, Response
|
9 |
from fastapi.staticfiles import StaticFiles
|
10 |
import uvicorn
|
11 |
+
import httpx
|
12 |
|
13 |
import spaces
|
14 |
from spaces.zero.client import _get_token
|
15 |
|
16 |
+
# Set environment variables for SSR
|
17 |
os.environ["GRADIO_SSR_MODE"] = "True"
|
18 |
os.environ["GRADIO_SERVER_PORT"] = "7860"
|
19 |
os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
|
|
|
25 |
# Create and configure static directory
|
26 |
static_dir = Path("./static")
|
27 |
static_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
28 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
|
29 |
os.environ["GRADIO_ALLOWED_PATHS"] = str(static_dir.resolve())
|
30 |
|
31 |
@spaces.GPU(duration=4*60)
|
32 |
def process_text(text):
|
|
|
33 |
return text.upper()
|
34 |
|
35 |
def process_and_save(request: gr.Request, text):
|
|
|
36 |
token = _get_token(request)
|
37 |
payload = token.split('.')[1]
|
38 |
payload = f"{payload}{'=' * ((4 - len(payload) % 4) % 4)}"
|
39 |
payload = json.loads(base64.urlsafe_b64decode(payload).decode())
|
40 |
print(f"Token payload: {payload}")
|
|
|
41 |
processed_text = process_text(text)
|
42 |
+
return processed_text
|
43 |
|
|
|
44 |
process_and_save.zerogpu = True
|
45 |
|
|
|
46 |
with gr.Blocks() as demo:
|
47 |
text_input = gr.Textbox(label="Enter some text")
|
48 |
submit_btn = gr.Button("Process and Download")
|
49 |
output = gr.Textbox(label="Output")
|
50 |
|
51 |
+
submit_btn.click(fn=process_and_save, inputs=[text_input], outputs=output)
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
# Mount the Gradio app with SSR mode, using node_port 7861
|
54 |
app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=True, node_port=7861)
|
55 |
|
56 |
+
# Manual reverse proxy for SSR assets
|
57 |
+
@app.get("/_app/{full_path:path}")
|
58 |
+
async def proxy_to_node(full_path: str, request: Request):
|
59 |
+
node_server = "http://127.0.0.1:7861"
|
60 |
+
url = f"{node_server}/_app/{full_path}"
|
61 |
+
async with httpx.AsyncClient() as client:
|
62 |
+
node_response = await client.get(url)
|
63 |
+
return Response(
|
64 |
+
content=node_response.content,
|
65 |
+
status_code=node_response.status_code,
|
66 |
+
media_type=node_response.headers.get("content-type"),
|
67 |
+
)
|
68 |
+
|
69 |
if __name__ == "__main__":
|
70 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|