Spaces:
Sleeping
Sleeping
Maximilian Noichl
commited on
added 0gpu
Browse files
app.py
CHANGED
|
@@ -1,10 +1,19 @@
|
|
| 1 |
import os
|
| 2 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
-
from fastapi import FastAPI
|
| 5 |
from fastapi.staticfiles import StaticFiles
|
| 6 |
import uvicorn
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Create FastAPI app
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
@@ -18,34 +27,51 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
| 18 |
# Tell Gradio which paths are allowed to be served
|
| 19 |
os.environ["GRADIO_ALLOWED_PATHS"] = str(static_dir.resolve())
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
with open(file_path, "w") as f:
|
| 26 |
-
f.write(
|
| 27 |
|
| 28 |
-
# Return file path for download
|
| 29 |
return gr.File(value=file_path)
|
| 30 |
|
| 31 |
-
# Mark function as not requiring GPU
|
| 32 |
process_and_save.zerogpu = True
|
| 33 |
|
| 34 |
# Create Gradio interface
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
text_input = gr.Textbox(label="Enter some text")
|
| 37 |
-
submit_btn = gr.Button("
|
| 38 |
-
output = gr.File(label="Download File")
|
| 39 |
|
| 40 |
submit_btn.click(
|
| 41 |
fn=process_and_save,
|
| 42 |
-
inputs=text_input,
|
| 43 |
outputs=output
|
| 44 |
)
|
| 45 |
|
| 46 |
-
# Mount Gradio app to FastAPI
|
| 47 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
| 48 |
|
| 49 |
# Run server
|
| 50 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 51 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
from pathlib import Path
|
| 3 |
+
import json
|
| 4 |
+
import base64
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
# Standard imports
|
| 8 |
import gradio as gr
|
| 9 |
+
from fastapi import FastAPI, Request
|
| 10 |
from fastapi.staticfiles import StaticFiles
|
| 11 |
import uvicorn
|
| 12 |
|
| 13 |
+
# Hugging Face Spaces imports
|
| 14 |
+
import spaces
|
| 15 |
+
from spaces.zero.client import _get_token
|
| 16 |
+
|
| 17 |
# Create FastAPI app
|
| 18 |
app = FastAPI()
|
| 19 |
|
|
|
|
| 27 |
# Tell Gradio which paths are allowed to be served
|
| 28 |
os.environ["GRADIO_ALLOWED_PATHS"] = str(static_dir.resolve())
|
| 29 |
|
| 30 |
+
@spaces.GPU(duration=60) # Specify GPU duration in seconds
|
| 31 |
+
def process_text(text):
|
| 32 |
+
"""Example GPU function - in reality, this might be model inference"""
|
| 33 |
+
return text.upper()
|
| 34 |
+
|
| 35 |
+
def process_and_save(request: gr.Request, text):
|
| 36 |
+
"""Main processing function that handles tokens and calls GPU function"""
|
| 37 |
+
# Get and decode the authentication token
|
| 38 |
+
token = _get_token(request)
|
| 39 |
+
payload = token.split('.')[1]
|
| 40 |
+
payload = f"{payload}{'=' * ((4 - len(payload) % 4) % 4)}"
|
| 41 |
+
payload = json.loads(base64.urlsafe_b64decode(payload).decode())
|
| 42 |
+
print(f"Token payload: {payload}") # For debugging
|
| 43 |
+
|
| 44 |
+
# Process the text using GPU function
|
| 45 |
+
processed_text = process_text(text)
|
| 46 |
+
|
| 47 |
+
# Save to file
|
| 48 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 49 |
+
file_path = static_dir / f"output_{timestamp}.txt"
|
| 50 |
with open(file_path, "w") as f:
|
| 51 |
+
f.write(processed_text)
|
| 52 |
|
|
|
|
| 53 |
return gr.File(value=file_path)
|
| 54 |
|
| 55 |
+
# Mark main function as not requiring GPU
|
| 56 |
process_and_save.zerogpu = True
|
| 57 |
|
| 58 |
# Create Gradio interface
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
text_input = gr.Textbox(label="Enter some text")
|
| 61 |
+
submit_btn = gr.Button("Process and Download")
|
| 62 |
+
output = gr.File(label="Download Processed File")
|
| 63 |
|
| 64 |
submit_btn.click(
|
| 65 |
fn=process_and_save,
|
| 66 |
+
inputs=[text_input],
|
| 67 |
outputs=output
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# Mount Gradio app to FastAPI with SSR mode for Spaces
|
| 71 |
+
app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=True)
|
| 72 |
|
| 73 |
# Run server
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
# Set SSR mode for Spaces
|
| 76 |
+
os.environ["GRADIO_SSR_MODE"] = "True"
|
| 77 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|