Spaces:
Running
on
Zero
Running
on
Zero
pure fastapi test
Browse files
app.py
CHANGED
|
@@ -6,38 +6,68 @@ import torch
|
|
| 6 |
from spaces.zero.client import _get_token
|
| 7 |
from fastapi import FastAPI
|
| 8 |
from fastapi.staticfiles import StaticFiles
|
| 9 |
-
import uvicorn
|
| 10 |
from pathlib import Path
|
| 11 |
|
| 12 |
# FastAPI setup
|
| 13 |
app = FastAPI()
|
|
|
|
| 14 |
static_dir = Path('./static')
|
| 15 |
static_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
app.mount("/static", StaticFiles(directory=static_dir), name="static")
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
def inner():
|
| 20 |
return "ok"
|
| 21 |
|
|
|
|
| 22 |
def greet(request: gr.Request, n):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
token = _get_token(request)
|
| 24 |
-
print(token)
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
payload = token.split('.')[1]
|
| 27 |
payload = f"{payload}{'=' * ((4 - len(payload) % 4) % 4)}"
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.JSON())
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
def start_server(app):
|
| 38 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
|
|
|
| 42 |
if __name__ == "__main__":
|
| 43 |
-
|
|
|
|
|
|
|
|
|
| 6 |
from spaces.zero.client import _get_token
|
| 7 |
from fastapi import FastAPI
|
| 8 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 9 |
from pathlib import Path
|
| 10 |
|
| 11 |
# FastAPI setup
|
| 12 |
app = FastAPI()
|
| 13 |
+
|
| 14 |
static_dir = Path('./static')
|
| 15 |
static_dir.mkdir(parents=True, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
# Create a sample HTML file in the static directory for demonstration
|
| 18 |
+
with open(static_dir / "test.html", "w", encoding="utf-8") as f:
|
| 19 |
+
f.write("<html><body><h1>Hello from static!</h1><p>We're serving this file without uvicorn!</p></body></html>")
|
| 20 |
+
|
| 21 |
app.mount("/static", StaticFiles(directory=static_dir), name="static")
|
| 22 |
|
| 23 |
+
|
| 24 |
+
@spaces.GPU(duration=4*60) # Not possible with IP-based quotas on certain Spaces
|
| 25 |
def inner():
|
| 26 |
return "ok"
|
| 27 |
|
| 28 |
+
|
| 29 |
def greet(request: gr.Request, n):
|
| 30 |
+
"""
|
| 31 |
+
Example function that verifies GPU usage and decodes a token.
|
| 32 |
+
"""
|
| 33 |
+
from spaces.zero.client import _get_token
|
| 34 |
token = _get_token(request)
|
| 35 |
+
print("Token:", token)
|
| 36 |
+
# Check that the GPU-decorated function still works
|
| 37 |
+
assert inner() == "ok"
|
| 38 |
+
|
| 39 |
+
# A small example of token decoding
|
| 40 |
payload = token.split('.')[1]
|
| 41 |
payload = f"{payload}{'=' * ((4 - len(payload) % 4) % 4)}"
|
| 42 |
+
try:
|
| 43 |
+
decoded = base64.urlsafe_b64decode(payload).decode()
|
| 44 |
+
return json.loads(decoded)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return {"error": str(e)}
|
| 47 |
|
|
|
|
| 48 |
|
| 49 |
+
# Build a simple Gradio Blocks interface that also shows a static HTML iframe
|
| 50 |
+
with gr.Blocks() as demo:
|
| 51 |
+
gr.Markdown("## Testing Static File Serving Without uvicorn")
|
| 52 |
|
| 53 |
+
# Show the static HTML file inside an iframe
|
| 54 |
+
gr.HTML(
|
| 55 |
+
value='<iframe src="/static/test.html" width="100%" height="300px"></iframe>',
|
| 56 |
+
label="Static HTML Demo"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Add the original demonstration interface
|
| 60 |
+
# (just a numeric input feeding into the greet() function)
|
| 61 |
+
greet_interface = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.JSON())
|
| 62 |
+
greet_interface.render()
|
| 63 |
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Mount the Gradio app
|
| 66 |
+
app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=False)
|
| 67 |
+
app.zerogpu = True
|
| 68 |
|
| 69 |
+
# We do NOT manually run uvicorn here; HF Spaces will serve the FastAPI app automatically.
|
| 70 |
if __name__ == "__main__":
|
| 71 |
+
# This pass ensures that if you run it locally (e.g., python app.py),
|
| 72 |
+
# nothing breaks, but on Spaces it's auto-served via the 'app' object.
|
| 73 |
+
pass
|