Spaces:
Configuration error
Configuration error
Download app.py from ryariyaz/URL-shortener: direct link, hf CLI and curl.
- Browser
- Download file 2.17 kB
-
https://huggingface.co/spaces/ryariyaz/URL-shortener/resolve/main/app.py
- Command line
-
hf download hf://spaces/ryariyaz/URL-shortener/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ryariyaz/URL-shortener/resolve/main/app.py
2.17 kB
| import gradio as gr | |
| from fastapi import FastAPI, Request | |
| from fastapi.responses import RedirectResponse, PlainTextResponse | |
| from shortener import shorten_url, resolve_url | |
| import gradio as gr | |
| from shortener import shorten_url | |
| # FastAPI app for redirects | |
| api = FastAPI() | |
| async def redirect(code: str, request: Request): | |
| long_url = resolve_url(code) | |
| if not long_url: | |
| return PlainTextResponse("Invalid or expired link", status_code=404) | |
| return RedirectResponse(url=long_url, status_code=302) | |
| async def redirect(request: Request): | |
| '''long_url = resolve_url(code) | |
| if not long_url: | |
| return PlainTextResponse("Invalid or expired link", status_code=404)''' | |
| return PlainTextResponse("Welcome", status_code=200) | |
| # Gradio UI | |
| def shorten(long_url,request: gr.Request,base_url=None): | |
| req = request.request | |
| base_url = "https://ryariyaz-url-shortener.hf.space/gradio_api/call/" | |
| if not long_url.startswith("http"): | |
| return "Invalid URL" | |
| if not base_url.startswith("http"): | |
| return "Invalid Base URL" | |
| return shorten_url(long_url, base_url) | |
| with gr.Blocks() as ui: | |
| gr.Markdown("# ๐ Affiliate URL Shortener") | |
| gr.Markdown("Convert long affiliate links into short, shareable URLs") | |
| #base_url_input = gr.Textbox(label="Base URL (e.g. https://your-space.hf.space)") | |
| url_input = gr.Textbox(label="Long Affiliate URL") | |
| url_output = gr.Textbox(label="Short URL") | |
| shorten_btn = gr.Button("Shorten") | |
| shorten_btn.click( | |
| shorten, | |
| inputs=[url_input], | |
| outputs=url_output | |
| ) | |
| # Mount Gradio inside FastAPI | |
| app = gr.mount_gradio_app(api, ui, path="/") | |
| with gr.Blocks() as app: | |
| gr.Markdown("# ๐ Affiliate URL Shortener") | |
| gr.Markdown("Convert long affiliate links into short, shareable URLs") | |
| #base_url_input = gr.Textbox(label="Base URL (e.g. https://your-space.hf.space)") | |
| url_input = gr.Textbox(label="Long Affiliate URL") | |
| url_output = gr.Textbox(label="Short URL") | |
| shorten_btn = gr.Button("Shorten") | |
| shorten_btn.click(shorten, inputs=url_input, outputs=url_output) | |
| app.launch(share=True) |