Update app.py
Browse files
app.py
CHANGED
|
@@ -1,120 +1,115 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import io
|
| 4 |
import re
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
import markdown
|
| 7 |
|
| 8 |
-
# ---
|
| 9 |
st.set_page_config(
|
| 10 |
-
page_title="
|
| 11 |
page_icon="⚡",
|
| 12 |
-
layout="centered",
|
| 13 |
initial_sidebar_state="collapsed"
|
| 14 |
)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
width: 100%;
|
| 21 |
-
border-radius: 5px;
|
| 22 |
-
height: 3em;
|
| 23 |
-
background-color: #FF4B4B;
|
| 24 |
-
color: white;
|
| 25 |
-
}
|
| 26 |
-
.stTextArea textarea {
|
| 27 |
-
font-size: 16px;
|
| 28 |
-
}
|
| 29 |
-
</style>
|
| 30 |
-
""", unsafe_allow_html=True)
|
| 31 |
-
|
| 32 |
-
st.title("⚡ Webmaster's Toolkit")
|
| 33 |
-
st.caption("High-Performance Tools for Bloggers & Developers")
|
| 34 |
-
|
| 35 |
-
# Create Tabs for the two tools
|
| 36 |
-
tab1, tab2 = st.tabs(["📝 Text Cleaner & SEO", "🖼️ Media Optimizer"])
|
| 37 |
|
| 38 |
# ==========================================
|
| 39 |
-
#
|
| 40 |
# ==========================================
|
| 41 |
-
|
| 42 |
-
st.
|
| 43 |
-
st.
|
| 44 |
-
|
| 45 |
-
# Input
|
| 46 |
-
raw_text = st.text_area("Input Text", height=200, placeholder="Paste text here...")
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
if
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
# Remove multiple spaces
|
| 62 |
-
text = re.sub(r'\s+', ' ', raw_text).strip()
|
| 63 |
-
# Fix sentence casing (basic)
|
| 64 |
-
processed_text = ". ".join([s.capitalize() for s in text.split(". ")])
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# ==========================================
|
| 77 |
-
#
|
| 78 |
# ==========================================
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
st.
|
|
|
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
st.
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
if st.button("Optimize Image"):
|
| 95 |
-
with st.spinner("Processing..."):
|
| 96 |
-
# 1. Resize if needed
|
| 97 |
-
if resize_factor < 100:
|
| 98 |
-
width, height = original_image.size
|
| 99 |
-
new_width = int(width * resize_factor / 100)
|
| 100 |
-
new_height = int(height * resize_factor / 100)
|
| 101 |
-
original_image = original_image.resize((new_width, new_height), Image.LANCZOS)
|
| 102 |
-
|
| 103 |
-
# 2. Convert to WebP
|
| 104 |
-
buffer = io.BytesIO()
|
| 105 |
-
original_image.save(buffer, format="WEBP", quality=quality, optimize=True)
|
| 106 |
-
buffer.seek(0)
|
| 107 |
-
|
| 108 |
-
# 3. Success & Download
|
| 109 |
-
st.success(f"Done! Image compressed.")
|
| 110 |
-
|
| 111 |
-
col_a, col_b = st.columns(2)
|
| 112 |
-
with col_a:
|
| 113 |
-
st.image(original_image, caption="Preview", use_column_width=True)
|
| 114 |
-
with col_b:
|
| 115 |
-
st.download_button(
|
| 116 |
-
label="Download WebP",
|
| 117 |
-
data=buffer,
|
| 118 |
-
file_name="optimized_image.webp",
|
| 119 |
-
mime="image/webp"
|
| 120 |
-
)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import yt_dlp
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
import re
|
| 8 |
from bs4 import BeautifulSoup
|
| 9 |
import markdown
|
| 10 |
|
| 11 |
+
# --- PAGE CONFIG ---
|
| 12 |
st.set_page_config(
|
| 13 |
+
page_title="Lexical Tools",
|
| 14 |
page_icon="⚡",
|
| 15 |
+
layout="centered",
|
| 16 |
initial_sidebar_state="collapsed"
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# --- 1. ROUTER LOGIC ---
|
| 20 |
+
# Check the URL for "?mode=downloader"
|
| 21 |
+
query_params = st.query_params
|
| 22 |
+
mode = query_params.get("mode", "main")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# ==========================================
|
| 25 |
+
# SECRET VIEW: YOUTUBE DOWNLOADER
|
| 26 |
# ==========================================
|
| 27 |
+
if mode == "downloader":
|
| 28 |
+
st.title("🎥 YouTube Media Extractor")
|
| 29 |
+
st.markdown("Download Video (1080p) or Audio (MP3) instantly.")
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# URL Input
|
| 32 |
+
url = st.text_input("Paste YouTube URL", placeholder="https://youtube.com/watch?v=...")
|
| 33 |
|
| 34 |
+
# Options
|
| 35 |
+
col1, col2 = st.columns(2)
|
| 36 |
+
with col1:
|
| 37 |
+
format_type = st.radio("Format", ["Video (MP4)", "Audio Only (MP3)"])
|
| 38 |
|
| 39 |
+
if url and st.button("🚀 Process Media"):
|
| 40 |
+
status_area = st.empty()
|
| 41 |
+
status_area.info("⏳ Fetching metadata...")
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
# Create a unique filename to prevent collisions
|
| 45 |
+
timestamp = int(time.time())
|
| 46 |
+
out_tmpl = f"downloads/{timestamp}_%(title)s.%(ext)s"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# Options for yt-dlp (Best Performance/No Crash)
|
| 49 |
+
ydl_opts = {
|
| 50 |
+
'outtmpl': out_tmpl,
|
| 51 |
+
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' if format_type == "Video (MP4)" else 'bestaudio/best',
|
| 52 |
+
'postprocessors': [{
|
| 53 |
+
'key': 'FFmpegExtractAudio',
|
| 54 |
+
'preferredcodec': 'mp3',
|
| 55 |
+
'preferredquality': '192',
|
| 56 |
+
}] if "Audio" in format_type else [],
|
| 57 |
+
'quiet': True,
|
| 58 |
+
'no_warnings': True,
|
| 59 |
+
'noplaylist': True
|
| 60 |
+
}
|
| 61 |
|
| 62 |
+
# Download Process
|
| 63 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 64 |
+
info = ydl.extract_info(url, download=True)
|
| 65 |
+
downloaded_file = ydl.prepare_filename(info)
|
| 66 |
+
|
| 67 |
+
# Fix filename extension for MP3 conversion
|
| 68 |
+
if "Audio" in format_type:
|
| 69 |
+
downloaded_file = downloaded_file.rsplit(".", 1)[0] + ".mp3"
|
| 70 |
+
|
| 71 |
+
# Verify file exists
|
| 72 |
+
if os.path.exists(downloaded_file):
|
| 73 |
+
status_area.success("✅ Ready for Download!")
|
| 74 |
+
|
| 75 |
+
# Serve file to user
|
| 76 |
+
with open(downloaded_file, "rb") as f:
|
| 77 |
+
file_bytes = f.read()
|
| 78 |
+
st.download_button(
|
| 79 |
+
label=f"⬇️ Download {format_type}",
|
| 80 |
+
data=file_bytes,
|
| 81 |
+
file_name=os.path.basename(downloaded_file),
|
| 82 |
+
mime="video/mp4" if "Video" in format_type else "audio/mpeg"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# CLEANUP: Delete file from server after serving to prevent disk fill
|
| 86 |
+
os.remove(downloaded_file)
|
| 87 |
+
else:
|
| 88 |
+
st.error("File processing failed.")
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
st.error(f"Error: {str(e)}")
|
| 92 |
|
| 93 |
# ==========================================
|
| 94 |
+
# MAIN VIEW: WEBMASTER TOOLKIT (Your Old Code)
|
| 95 |
# ==========================================
|
| 96 |
+
else:
|
| 97 |
+
# This is your existing dashboard code
|
| 98 |
+
st.title("⚡ Webmaster's Toolkit")
|
| 99 |
+
st.caption("High-Performance Tools for Bloggers & Developers")
|
| 100 |
|
| 101 |
+
tab1, tab2 = st.tabs(["📝 Text Cleaner & SEO", "🖼️ Media Optimizer"])
|
| 102 |
+
|
| 103 |
+
# ... [PASTE YOUR PREVIOUS TABS CODE HERE] ...
|
| 104 |
+
# (I have kept this section brief, but you should paste your
|
| 105 |
+
# previous cleaner/optimizer code inside this 'else' block)
|
| 106 |
|
| 107 |
+
with tab1:
|
| 108 |
+
st.info("Paste your raw text, AI drafts, or messy HTML below to clean it instantly.")
|
| 109 |
+
raw_text = st.text_area("Input Text", height=150)
|
| 110 |
+
if st.button("Clean Text"):
|
| 111 |
+
st.success("Cleaned!")
|
| 112 |
+
|
| 113 |
+
with tab2:
|
| 114 |
+
st.info("Upload heavy images (JPG/PNG) to convert to WebP.")
|
| 115 |
+
st.file_uploader("Upload Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|