| import gradio as gr |
| import tempfile |
| import os |
|
|
| def upload_and_share(file): |
| if file is None: |
| return "No file uploaded" |
| |
| |
| temp_dir = tempfile.mkdtemp() |
| file_path = os.path.join(temp_dir, file.name) |
| |
| |
| with open(file_path, "wb") as f: |
| f.write(file.read()) |
| |
| |
| base_url = "https://coollsd-test.hf.space" |
| relative_path = file_path.replace("/tmp", "") |
| share_link = f"{base_url}/file={relative_path}" |
| return share_link |
|
|
| with gr.Blocks() as demo: |
| file_input = gr.File(label="Upload File") |
| output = gr.Textbox(label="Share Link") |
| upload_btn = gr.Button("Upload and Share") |
| upload_btn.click(fn=upload_and_share, inputs=file_input, outputs=output) |
|
|
| if __name__ == "__main__": |
| demo.launch(share=True) |