Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# --- 1. UI Configuration (Best UI Settings) ---
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="Professional Toolkit",
|
| 11 |
+
page_icon="⚡",
|
| 12 |
+
layout="centered", # Keeps UI focus in the center
|
| 13 |
+
initial_sidebar_state="collapsed"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Custom CSS for a cleaner look
|
| 17 |
+
st.markdown("""
|
| 18 |
+
<style>
|
| 19 |
+
.stButton>button {
|
| 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 |
+
# TOOL 1: SEO Text Cleaner (The "3rd" Request)
|
| 40 |
+
# ==========================================
|
| 41 |
+
with tab1:
|
| 42 |
+
st.header("Content Humanizer & Cleaner")
|
| 43 |
+
st.info("Paste your raw text, AI drafts, or messy HTML below to clean it instantly.")
|
| 44 |
+
|
| 45 |
+
# Input
|
| 46 |
+
raw_text = st.text_area("Input Text", height=200, placeholder="Paste text here...")
|
| 47 |
+
|
| 48 |
+
col1, col2, col3 = st.columns(3)
|
| 49 |
+
|
| 50 |
+
# Processing Logic
|
| 51 |
+
processed_text = ""
|
| 52 |
+
|
| 53 |
+
if raw_text:
|
| 54 |
+
# Option A: Strip HTML (Clean for pasting)
|
| 55 |
+
if col1.button("Strip HTML"):
|
| 56 |
+
soup = BeautifulSoup(raw_text, "html.parser")
|
| 57 |
+
processed_text = soup.get_text(separator=" ")
|
| 58 |
+
|
| 59 |
+
# Option B: Humanize / Clean Spacing
|
| 60 |
+
if col2.button("Fix Spacing & Case"):
|
| 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 |
+
# Option C: Markdown to HTML
|
| 67 |
+
if col3.button("MD to HTML"):
|
| 68 |
+
processed_text = markdown.markdown(raw_text)
|
| 69 |
+
|
| 70 |
+
# Output Display
|
| 71 |
+
if processed_text:
|
| 72 |
+
st.success("Processing Complete!")
|
| 73 |
+
st.text_area("Result", value=processed_text, height=200)
|
| 74 |
+
st.download_button("Download Result", data=processed_text, file_name="clean_text.txt")
|
| 75 |
+
|
| 76 |
+
# ==========================================
|
| 77 |
+
# TOOL 2: Media Optimizer (The "1st" Request)
|
| 78 |
+
# ==========================================
|
| 79 |
+
with tab2:
|
| 80 |
+
st.header("Image Optimizer (WebP Converter)")
|
| 81 |
+
st.info("Upload heavy images (JPG/PNG) and convert them to lightweight WebP for better SEO.")
|
| 82 |
+
|
| 83 |
+
uploaded_file = st.file_uploader("Upload an Image", type=['png', 'jpg', 'jpeg'])
|
| 84 |
+
|
| 85 |
+
if uploaded_file is not None:
|
| 86 |
+
# Show original stats
|
| 87 |
+
original_image = Image.open(uploaded_file)
|
| 88 |
+
st.write(f"**Original Size:** {original_image.size[0]}x{original_image.size[1]}")
|
| 89 |
+
|
| 90 |
+
# Settings
|
| 91 |
+
quality = st.slider("Compression Quality", 10, 100, 80)
|
| 92 |
+
resize_factor = st.slider("Resize (%)", 10, 100, 100)
|
| 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_container_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 |
+
)
|