ysharma's picture
ysharma HF Staff
Update app.py
8f11b65 verified
#from pathlib import Path
import gradio as gr
import os
def upload_file(filepath):
"""
Handle uploaded file and generate its accessible URL. Function constructs a public-facing URL using the `SPACE_HOST` environment variable.
Args:
filepath: A list of file paths uploaded through the Gradio `File` component.
Returns:
file_url: An accessible constructed URL pointing to the uploaded file. Use this for downstream processes.
"""
print("----- inside upload file -----")
print(f"filepath -- {filepath}")
# Get the space URL (if hosted on HF Spaces)
space_url = os.getenv("SPACE_HOST", "") # HF Spaces sets this
if space_url:
file_url = f"{space_url}/gradio_api/file={filepath[0]}"
print(f"File URL: {file_url}")
return file_url #filepath[0]
with gr.Blocks() as demo:
gr.Markdown("## Using gr.File")
with gr.Row():
f_in = gr.File(label="Upload a file", file_count="multiple")
#f_out = gr.File(label="Output file", file_count="multiple")
file_url = gr.Textbox(label="file_url")
f_in.upload(upload_file, f_in, file_url, api_name="output_file_url")
demo.launch(debug=True, mcp_server=True)