Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,14 +7,18 @@ import gradio as gr
|
|
| 7 |
# CONFIG
|
| 8 |
# -------------------------
|
| 9 |
|
| 10 |
-
SPACE_NAME = os.getenv("SPACE_ID", "your-username/your-space")
|
| 11 |
-
BASE_URL = f"https://{SPACE_NAME.split('/')[-1]}.hf.space"
|
| 12 |
-
|
| 13 |
FILE_DIR = "files"
|
| 14 |
os.makedirs(FILE_DIR, exist_ok=True)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# -------------------------
|
| 17 |
-
# FASTAPI
|
| 18 |
# -------------------------
|
| 19 |
|
| 20 |
server = FastAPI()
|
|
@@ -36,25 +40,24 @@ def download_file(filename: str):
|
|
| 36 |
# -------------------------
|
| 37 |
|
| 38 |
def generate_link(filename):
|
| 39 |
-
file_path = os.path.join(FILE_DIR, filename)
|
| 40 |
-
|
| 41 |
if not filename:
|
| 42 |
return "Enter filename"
|
| 43 |
|
|
|
|
|
|
|
| 44 |
if not os.path.exists(file_path):
|
| 45 |
-
return "File does not exist"
|
| 46 |
|
| 47 |
return f"{BASE_URL}/download/{filename}"
|
| 48 |
|
| 49 |
# -------------------------
|
| 50 |
-
#
|
| 51 |
# -------------------------
|
| 52 |
|
| 53 |
with gr.Blocks() as app:
|
|
|
|
| 54 |
|
| 55 |
-
gr.
|
| 56 |
-
|
| 57 |
-
filename_input = gr.Textbox(label="Filename (must exist in /files)")
|
| 58 |
link_output = gr.Textbox(label="Direct Link")
|
| 59 |
|
| 60 |
btn = gr.Button("Generate Link")
|
|
@@ -65,8 +68,9 @@ with gr.Blocks() as app:
|
|
| 65 |
outputs=link_output
|
| 66 |
)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
app = gr.mount_gradio_app(server, app)
|
| 70 |
|
| 71 |
# Launch for Spaces
|
|
|
|
| 72 |
app.launch()
|
|
|
|
| 7 |
# CONFIG
|
| 8 |
# -------------------------
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
FILE_DIR = "files"
|
| 11 |
os.makedirs(FILE_DIR, exist_ok=True)
|
| 12 |
|
| 13 |
+
# Your Space URL (auto-detect friendly)
|
| 14 |
+
SPACE_NAME = os.getenv("SPACE_ID")
|
| 15 |
+
if SPACE_NAME:
|
| 16 |
+
BASE_URL = f"https://{SPACE_NAME.split('/')[-1]}.hf.space"
|
| 17 |
+
else:
|
| 18 |
+
BASE_URL = "http://127.0.0.1:7860"
|
| 19 |
+
|
| 20 |
# -------------------------
|
| 21 |
+
# FASTAPI APP
|
| 22 |
# -------------------------
|
| 23 |
|
| 24 |
server = FastAPI()
|
|
|
|
| 40 |
# -------------------------
|
| 41 |
|
| 42 |
def generate_link(filename):
|
|
|
|
|
|
|
| 43 |
if not filename:
|
| 44 |
return "Enter filename"
|
| 45 |
|
| 46 |
+
file_path = os.path.join(FILE_DIR, filename)
|
| 47 |
+
|
| 48 |
if not os.path.exists(file_path):
|
| 49 |
+
return "File does not exist in /files"
|
| 50 |
|
| 51 |
return f"{BASE_URL}/download/{filename}"
|
| 52 |
|
| 53 |
# -------------------------
|
| 54 |
+
# GRADIO UI
|
| 55 |
# -------------------------
|
| 56 |
|
| 57 |
with gr.Blocks() as app:
|
| 58 |
+
gr.Markdown("# 🔗 Direct Link Generator (HF Spaces)")
|
| 59 |
|
| 60 |
+
filename_input = gr.Textbox(label="Filename")
|
|
|
|
|
|
|
| 61 |
link_output = gr.Textbox(label="Direct Link")
|
| 62 |
|
| 63 |
btn = gr.Button("Generate Link")
|
|
|
|
| 68 |
outputs=link_output
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# 🔥 Correct mounting (IMPORTANT FIX)
|
| 72 |
+
app = gr.mount_gradio_app(server, app, path="/")
|
| 73 |
|
| 74 |
# Launch for Spaces
|
| 75 |
+
app.queue()
|
| 76 |
app.launch()
|