Spaces:
Configuration error
Configuration error
Upload 5 files
Browse filesFiles changed (5)
- DockerfileAdded+30
- README.md+1-10
- main.pyAdded+106
- requirements.txtAdded+6
- templates/index.htmlAdded+441
123456789101112131415161718192021222324252627282930# Use Python 3.9 slim image as baseFROM python:3.9-slim
# Set working directoryWORKDIR /app
# Install system dependenciesRUN apt-get update && apt-get install -y \ build-essential \ && rm -rf /var/lib/apt/lists/*
# Copy requirements first to leverage Docker cacheCOPY requirements.txt .
# Install Python dependenciesRUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the applicationCOPY . .
# Create a non-root user for securityRUN adduser --disabled-password --gecos '' appuserRUN chown -R appuser:appuser /appUSER appuser
# Expose port 7860EXPOSE 7860
# Run the applicationCMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"] 123456789101---title: Shortneremoji: β‘colorFrom: indigocolorTo: pinksdk: dockerpinned: false---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference# link-shortner123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import sqlite3import randomimport stringfrom typing import Optionalfrom contextlib import contextmanagerfrom fastapi import FastAPI, HTTPException, Requestfrom fastapi.responses import HTMLResponse, RedirectResponsefrom fastapi.templating import Jinja2Templatesfrom fastapi.staticfiles import StaticFilesfrom pydantic import BaseModel, HttpUrlimport validators
app = FastAPI()templates = Jinja2Templates(directory="templates")
BASE_URL = "https://arevedudaa-shortner.hf.space"DB_FILE = "shortener.db"
class URLRequest(BaseModel): original_url: str
@contextmanagerdef get_db_connection(): conn = sqlite3.connect(DB_FILE) try: yield conn finally: conn.close()
def init_db(): with get_db_connection() as conn: cursor = conn.cursor() cursor.execute('''CREATE TABLE IF NOT EXISTS links ( id INTEGER PRIMARY KEY AUTOINCREMENT, original_url TEXT NOT NULL, short_code TEXT UNIQUE NOT NULL, short_url TEXT UNIQUE NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )''') conn.commit()
def generate_short_code(length: int = 6) -> str: return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
def store_link(original_url: str) -> str: with get_db_connection() as conn: cursor = conn.cursor() # Check if URL already exists cursor.execute("SELECT short_url FROM links WHERE original_url = ?", (original_url,)) existing = cursor.fetchone() if existing: return existing[0] # Generate unique short code while True: short_code = generate_short_code() short_url = f"{BASE_URL}/{short_code}" cursor.execute("SELECT id FROM links WHERE short_code = ?", (short_code,)) if not cursor.fetchone(): break # Store new shortened URL cursor.execute( "INSERT INTO links (original_url, short_code, short_url) VALUES (?, ?, ?)", (original_url, short_code, short_url) ) conn.commit() return short_url
@app.get("/", response_class=HTMLResponse)async def read_root(request: Request): return templates.TemplateResponse("index.html", {"request": request})
@app.post("/shorten/")async def shorten_url(url_request: URLRequest): if not url_request.original_url: raise HTTPException(status_code=400, detail="URL is required") if not url_request.original_url.startswith(('http://', 'https://')): raise HTTPException(status_code=400, detail="URL must start with http:// or https://") try: short_url = store_link(url_request.original_url) return {"original_url": url_request.original_url, "shortened_url": short_url} except Exception as e: raise HTTPException(status_code=500, detail="Error creating shortened URL")
@app.get("/{short_code}")async def redirect_to_url(short_code: str): with get_db_connection() as conn: cursor = conn.cursor() cursor.execute("SELECT original_url FROM links WHERE short_code = ?", (short_code,)) result = cursor.fetchone() if not result: raise HTTPException(status_code=404, detail="Short URL not found") return RedirectResponse(url=result[0])
# Initialize database on startupinit_db()
if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=7860) 123456fastapiuvicornpydanticpython-multipartvalidatorsjinja2123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>URL Shortener</title> <script src="https://cdn.tailwindcss.com"></script> <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet"> <style> :root { --bg-primary: #000000; --text-primary: #ffffff; --text-secondary: #888888; --accent: #FF3B30; } body { font-family: 'Roboto', sans-serif; background-color: var(--bg-primary); color: var(--text-primary); min-height: 100vh; overflow-x: hidden; } .input-container { transition: transform 0.3s ease-out; } .input-container.focused { transform: translateY(-60vh); } .history-item { transition: all 0.3s ease; } .history-item:hover { background: rgba(255, 255, 255, 0.05); } @keyframes glowAnimation { 0% { box-shadow: 0 0 0 rgba(255, 59, 48, 0); } 50% { box-shadow: 0 0 20px rgba(255, 59, 48, 0.3); } 100% { box-shadow: 0 0 0 rgba(255, 59, 48, 0); } } .success-animation { animation: glowAnimation 1s ease-out; } #developerInfo { transition: all 0.3s ease-out; } #developerInfo > div { transition: all 0.3s ease-out; } .social-link { transition: all 0.2s ease; } .social-link:hover { transform: translateY(-2px); } </style></head><body class="p-0 m-0"> <!-- Time and Location --> <div class="fixed top-0 left-0 right-0 p-6 z-10"> <div class="text-2xl font-light" id="time"></div> <div class="text-sm text-gray-400" id="date"></div> </div>
<!-- History Section --> <div class="pt-24 px-6 pb-32"> <div class="text-xs uppercase tracking-wider text-gray-500 mb-4">Recent Links</div> <div id="history" class="space-y-4"> <!-- History items will be added here --> </div> </div>
<!-- Bottom Input Section --> <div id="inputContainer" class="input-container fixed bottom-0 left-0 right-0 p-6 bg-black"> <form id="urlForm" class="space-y-4"> <div class="relative"> <input type="text" id="url" class="w-full bg-white/10 text-white px-4 py-3 rounded-2xl border-0 focus:outline-none focus:ring-2 focus:ring-red-500/30 placeholder-gray-500 text-base" placeholder="Paste your URL here" autocomplete="off"> </div> <button type="submit" class="w-full bg-white/10 text-white py-4 rounded-2xl font-medium active:scale-[0.98] transition-all duration-300 hover:bg-red-500/20 focus:outline-none"> Shorten URL </button> </form>
<!-- Result Section --> <div id="result" class="mt-4 hidden"> <div class="bg-white/5 rounded-2xl p-4"> <div class="flex items-center justify-between"> <input type="text" id="shortUrl" readonly class="flex-1 bg-transparent border-none focus:outline-none text-white font-light"> <button onclick="copyToClipboard()" class="ml-2 p-2 rounded-xl bg-white/5 text-white/80 hover:bg-white/10 transition-colors duration-200"> Copy </button> </div> </div> </div> </div>
<!-- Developer Info Popup --> <div id="developerInfo" class="fixed inset-0 z-50 flex items-center justify-center px-4 bg-black/80 backdrop-blur-sm"> <div class="bg-white/10 rounded-3xl p-6 max-w-md w-full backdrop-blur-md border border-white/10"> <div class="flex justify-between items-start mb-6"> <div class="text-xl font-light">About Developer</div> <button onclick="closeDeveloperInfo()" class="text-white/60 hover:text-white transition-colors"> <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/> </svg> </button> </div> <div class="space-y-4"> <p class="text-gray-300 leading-relaxed"> Aditya Devarshi is an AI Engineer passionate about creating innovative projects and continuous learning. This URL shortener is one of his many projects showcasing modern design principles and functionality. </p> <div class="space-y-3 mt-6"> <div class="text-sm text-gray-400 uppercase tracking-wider">Connect & Follow</div> <div class="grid grid-cols-2 gap-3"> <a href="https://www.adityadevarshi.online/" target="_blank" rel="noopener" class="flex items-center gap-2 px-4 py-3 rounded-xl bg-white/5 hover:bg-white/10 transition-colors text-sm text-white/80 hover:text-white"> <span>π</span> <span>Portfolio</span> </a> <a href="https://www.linkedin.com/in/aditya-devarshi/" target="_blank" rel="noopener" class="flex items-center gap-2 px-4 py-3 rounded-xl bg-white/5 hover:bg-white/10 transition-colors text-sm text-white/80 hover:text-white"> <span>πΌ</span> <span>LinkedIn</span> </a> <a href="https://github.com/devarshiadi/" target="_blank" rel="noopener" class="flex items-center gap-2 px-4 py-3 rounded-xl bg-white/5 hover:bg-white/10 transition-colors text-sm text-white/80 hover:text-white"> <span>π¦</span> <span>GitHub</span> </a> <a href="https://medium.com/@devarshia5" target="_blank" rel="noopener" class="flex items-center gap-2 px-4 py-3 rounded-xl bg-white/5 hover:bg-white/10 transition-colors text-sm text-white/80 hover:text-white"> <span>π</span> <span>Medium</span> </a> </div> </div>
<div class="mt-6 text-center"> <button onclick="closeDeveloperInfo()" class="px-6 py-2 rounded-xl bg-white/10 text-white/80 hover:bg-white/20 transition-all duration-300 text-sm"> Close </button> </div> </div> </div> </div>
<script> // Initialize localStorage history let urlHistory = JSON.parse(localStorage.getItem('urlHistory') || '[]'); updateHistoryDisplay(); // Show saved history on load // Time and Date Update function updateDateTime() { const now = new Date(); const timeString = now.toLocaleTimeString('en-US', { hour12: false, hour: '2-digit', minute: '2-digit' }); const dateString = now.toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric' }); document.getElementById('time').textContent = timeString; document.getElementById('date').textContent = dateString; } updateDateTime(); setInterval(updateDateTime, 1000); // Input Focus Behavior with improved UX const inputContainer = document.getElementById('inputContainer'); const urlInput = document.getElementById('url'); const historySection = document.querySelector('.pt-24'); urlInput.addEventListener('focus', () => { inputContainer.classList.add('focused'); historySection.style.opacity = '0.3'; navigator.vibrate(1); }); document.addEventListener('click', (e) => { if (!inputContainer.contains(e.target)) { inputContainer.classList.remove('focused'); historySection.style.opacity = '1'; } }); // Clear input on successful submission function resetForm() { urlInput.value = ''; document.getElementById('result').classList.add('hidden'); } // Form Submission with improved error handling document.getElementById('urlForm').addEventListener('submit', async (e) => { e.preventDefault(); const url = urlInput.value.trim(); if (!url) { showNotification('Please enter a URL', 'error'); return; } if (!url.startsWith('http://') && !url.startsWith('https://')) { showNotification('Please enter a valid URL starting with http:// or https://', 'error'); return; } const submitButton = e.target.querySelector('button'); submitButton.disabled = true; submitButton.innerHTML = '<span class="inline-block animate-pulse">Shortening...</span>'; try { const response = await fetch('/shorten/', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ original_url: url }) }); const data = await response.json(); if (response.ok) { document.getElementById('result').classList.remove('hidden'); document.getElementById('shortUrl').value = data.shortened_url; // Add to history and save to localStorage addToHistory(url, data.shortened_url); // Success animation and feedback inputContainer.classList.add('success-animation'); setTimeout(() => inputContainer.classList.remove('success-animation'), 1000); navigator.vibrate([50, 50, 50]); // Clear input after short delay setTimeout(resetForm, 3000); } else { showNotification(data.detail || 'Error shortening URL', 'error'); } } catch (error) { showNotification('Network error occurred', 'error'); } finally { submitButton.disabled = false; submitButton.textContent = 'Shorten URL'; } }); function addToHistory(originalUrl, shortUrl) { const historyItem = { original: originalUrl, shortened: shortUrl, timestamp: new Date().toISOString() }; urlHistory.unshift(historyItem); if (urlHistory.length > 10) urlHistory.pop(); // Keep last 10 items // Save to localStorage localStorage.setItem('urlHistory', JSON.stringify(urlHistory)); updateHistoryDisplay(); } function updateHistoryDisplay() { const historyContainer = document.getElementById('history'); if (urlHistory.length === 0) { historyContainer.innerHTML = ` <div class="text-center text-gray-500 py-8"> No shortened URLs yet </div> `; return; } historyContainer.innerHTML = urlHistory.map(item => ` <div class="history-item rounded-xl p-4 bg-white/5 backdrop-blur-sm"> <div class="flex justify-between items-start"> <div class="text-sm font-light text-gray-400 truncate flex-1"> ${item.original} </div> <div class="text-xs text-gray-500 ml-2"> ${new Date(item.timestamp).toLocaleDateString()} </div> </div> <div class="flex justify-between items-center mt-2"> <div class="text-white truncate flex-1">${item.shortened}</div> <div class="flex gap-2"> <button onclick="copyUrl('${item.shortened}')" class="text-xs text-white/60 hover:text-white px-3 py-1 rounded-lg bg-white/10 transition-colors"> Copy </button> <button onclick="deleteHistoryItem('${item.timestamp}')" class="text-xs text-red-400/60 hover:text-red-400 px-3 py-1 rounded-lg bg-red-500/10 transition-colors"> Delete </button> </div> </div> </div> `).join(''); } function deleteHistoryItem(timestamp) { urlHistory = urlHistory.filter(item => item.timestamp !== timestamp); localStorage.setItem('urlHistory', JSON.stringify(urlHistory)); updateHistoryDisplay(); navigator.vibrate(50); } function copyUrl(url) { navigator.clipboard.writeText(url).then(() => { showNotification('Copied to clipboard', 'success'); navigator.vibrate(50); }); } function copyToClipboard() { const shortUrl = document.getElementById('shortUrl'); navigator.clipboard.writeText(shortUrl.value).then(() => { showNotification('Copied to clipboard', 'success'); navigator.vibrate(50); }); } function showNotification(message, type) { const notification = document.createElement('div'); notification.className = `fixed bottom-32 left-1/2 transform -translate-x-1/2 px-6 py-3 rounded-2xl text-white text-center text-sm ${type === 'error' ? 'bg-red-500/90' : 'bg-white/10'} opacity-0 transition-opacity duration-300 z-50 backdrop-blur-sm shadow-lg`; notification.textContent = message; document.body.appendChild(notification); requestAnimationFrame(() => notification.classList.add('opacity-100')); setTimeout(() => { notification.classList.remove('opacity-100'); setTimeout(() => notification.remove(), 300); }, 2000); } function closeDeveloperInfo() { const popup = document.getElementById('developerInfo'); popup.style.opacity = '0'; popup.style.transform = 'scale(0.95)'; setTimeout(() => { popup.style.display = 'none'; }, 300); // Save to localStorage so it doesn't show again in this session localStorage.setItem('developerInfoShown', 'true'); } // Check if we should show the popup window.addEventListener('DOMContentLoaded', () => { const popup = document.getElementById('developerInfo'); if (!localStorage.getItem('developerInfoShown')) { popup.style.display = 'flex'; popup.style.opacity = '0'; popup.style.transform = 'scale(0.95)'; // Trigger animation setTimeout(() => { popup.style.opacity = '1'; popup.style.transform = 'scale(1)'; }, 100); } else { popup.style.display = 'none'; } // Console Developer Info console.log(`%cβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ Aditya Devarshi ββ AI Engineer & Full Stack Developer ββ βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£β ββ π Portfolio: https://www.adityadevarshi.online ββ πΌ LinkedIn: https://www.linkedin.com/in/aditya-devarshi ββ π¦ GitHub: https://github.com/devarshiadi ββ π Medium: https://medium.com/@devarshia5 ββ ββ βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£β ββ π Skills: AI/ML, Full Stack Development, Cloud Computing ββ π§ Contact: devarshia5@gmail.com ββ ββ π‘ "Building the future with AI and innovation" ββ βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ`, 'font-family: monospace; color: #00ff00; font-size: 12px;'); console.log('%cWelcome to URL Shortener!', 'color: #FF3B30; font-size: 20px; font-weight: bold;'); console.log('%cDeveloped with β€οΈ by Aditya Devarshi', 'color: #888; font-size: 14px;'); // Tech stack info console.log('%cTech Stack:', 'color: #00ff00; font-size: 16px; font-weight: bold;'); console.table({ 'Frontend': ['HTML5', 'TailwindCSS', 'JavaScript'], 'Backend': ['Python', 'FastAPI', 'SQLite'], 'Deployment': ['Docker', 'Cloud Ready'], 'Design': ['Nothing OS Inspired', 'Modern Minimal UI'] }); // Contact info as an object console.log('%cContact Information:', 'color: #00ff00; font-size: 16px; font-weight: bold;'); console.table({ 'Email': 'devarshia5@gmail.com', 'LinkedIn': 'https://www.linkedin.com/in/aditya-devarshi', 'Portfolio': 'https://www.adityadevarshi.online', 'GitHub': 'https://github.com/devarshiadi', 'Medium': 'https://medium.com/@devarshia5' }); // Project info console.log('%cProject Information:', 'color: #00ff00; font-size: 16px; font-weight: bold;'); console.table({ 'Name': 'Modern URL Shortener', 'Version': '1.0.0', 'License': 'MIT', 'Repository': 'https://github.com/devarshiadi/url-shortener', 'Design Inspiration': 'Nothing OS', 'Features': [ 'Modern UI/UX', 'URL Shortening', 'History Management', 'Responsive Design', 'Dark Mode', 'Local Storage' ] }); // Easter egg message console.log('%cπ Hey there, curious developer!', 'color: #FF3B30; font-size: 16px; font-weight: bold;'); console.log('%cLooking for opportunities or collaboration? Feel free to reach out!', 'color: #888; font-size: 14px;'); }); </script></body></html> DockerfileAdded+30
README.md+1-10
main.pyAdded+106
requirements.txtAdded+6
templates/index.htmlAdded+441