Spaces:
Runtime error
Runtime error
Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243from pathlib import Pathfrom fastapi import FastAPIfrom fastapi.staticfiles import StaticFilesimport uvicornimport gradio as grfrom datetime import datetime
# create a FastAPI appapp = FastAPI()
# create a static directory to store the static filesstatic_dir = Path('./static')static_dir.mkdir(parents=True, exist_ok=True)
# mount FastAPI StaticFiles serverapp.mount("/static", StaticFiles(directory=static_dir), name="static")
# Gradio codedef predict(text_input): file_name = f"{datetime.utcnow().strftime('%s')}.html" file_path = static_dir / file_name print(file_path) with open(file_path, "w") as f: f.write(f"""<h2>Hello {text_input} </h2> <h3>{file_name}</h3> """)
return f'<a href="/static/{file_name}" target="_blank">{file_name}</a>'
with gr.Blocks() as block: text_input = gr.Textbox(label="Name") markdown = gr.Markdown(label="Output Box") new_btn = gr.Button("New") new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown])
# mount Gradio app to FastAPI appapp = gr.mount_gradio_app(app, block, path="/")
# serve the appif __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)app.pyAdded+43