Spaces:
Sleeping
Sleeping
File size: 1,390 Bytes
d1ed09d 755230a d1ed09d 24e24f3 755230a d1ed09d 755230a d1ed09d 755230a d1ed09d 755230a d1ed09d 755230a d1ed09d 755230a d1ed09d 755230a d1ed09d 755230a d1ed09d 755230a 1ef8927 755230a d1ed09d |
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 |
import os
from pathlib import Path
import gradio as gr
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
import uvicorn
# Create FastAPI app
app = FastAPI()
# Create and configure static directory
static_dir = Path("./static")
static_dir.mkdir(parents=True, exist_ok=True)
# Mount static directory to FastAPI
app.mount("/static", StaticFiles(directory="static"), name="static")
# Tell Gradio which paths are allowed to be served
os.environ["GRADIO_ALLOWED_PATHS"] = str(static_dir.resolve())
def process_and_save(text):
"""Simple function that saves text to a file and returns file path"""
# Save text to a file in static directory
file_path = static_dir / "output.txt"
with open(file_path, "w") as f:
f.write(text)
# Return file path for download
return gr.File(value=file_path)
# Mark function as not requiring GPU
process_and_save.zerogpu = True
# Create Gradio interface
with gr.Blocks() as demo:
text_input = gr.Textbox(label="Enter some text")
submit_btn = gr.Button("Save and Download")
output = gr.File(label="Download File")
submit_btn.click(
fn=process_and_save,
inputs=text_input,
outputs=output
)
# Mount Gradio app to FastAPI
app = gr.mount_gradio_app(app, demo, path="/")
# Run server
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) |