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