lexicalspace commited on
Commit
38e2e5b
·
verified ·
1 Parent(s): c78dcc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -35
app.py CHANGED
@@ -13,39 +13,43 @@ 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',
@@ -59,20 +63,17 @@ if mode == "downloader":
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(
@@ -82,34 +83,57 @@ if mode == "downloader":
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")
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  page_title="Lexical Tools",
14
  page_icon="⚡",
15
  layout="centered",
16
+ initial_sidebar_state="expanded"
17
  )
18
 
19
+ # --- SIDEBAR NAVIGATION ---
20
+ st.sidebar.title("Navigation")
21
+ # Default to "Toolkit" unless ?mode=downloader is in URL
22
  query_params = st.query_params
23
+ default_index = 1 if query_params.get("mode") == "downloader" else 0
24
+
25
+ app_mode = st.sidebar.radio(
26
+ "Choose Tool:",
27
+ ["Webmaster Toolkit", "YouTube Downloader"],
28
+ index=default_index
29
+ )
30
 
31
  # ==========================================
32
+ # VIEW 1: YOUTUBE DOWNLOADER
33
  # ==========================================
34
+ if app_mode == "YouTube Downloader":
35
  st.title("🎥 YouTube Media Extractor")
36
+ st.info("Download Video (1080p) or Audio (MP3) instantly.")
37
 
 
38
  url = st.text_input("Paste YouTube URL", placeholder="https://youtube.com/watch?v=...")
39
 
 
40
  col1, col2 = st.columns(2)
41
  with col1:
42
  format_type = st.radio("Format", ["Video (MP4)", "Audio Only (MP3)"])
43
 
44
  if url and st.button("🚀 Process Media"):
45
  status_area = st.empty()
46
+ status_area.info("⏳ Fetching metadata... (This may take 10-20s)")
47
 
48
  try:
 
49
  timestamp = int(time.time())
50
+ # Save to a temporary folder
51
+ out_tmpl = f"{timestamp}_%(title)s.%(ext)s"
52
 
 
53
  ydl_opts = {
54
  'outtmpl': out_tmpl,
55
  'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' if format_type == "Video (MP4)" else 'bestaudio/best',
 
63
  'noplaylist': True
64
  }
65
 
 
66
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
67
  info = ydl.extract_info(url, download=True)
68
  downloaded_file = ydl.prepare_filename(info)
69
 
 
70
  if "Audio" in format_type:
71
+ base, ext = os.path.splitext(downloaded_file)
72
+ downloaded_file = base + ".mp3"
73
 
 
74
  if os.path.exists(downloaded_file):
75
+ status_area.success("✅ Done!")
76
 
 
77
  with open(downloaded_file, "rb") as f:
78
  file_bytes = f.read()
79
  st.download_button(
 
83
  mime="video/mp4" if "Video" in format_type else "audio/mpeg"
84
  )
85
 
86
+ # Cleanup
87
  os.remove(downloaded_file)
88
  else:
89
+ st.error("File not found. Please try again.")
90
 
91
  except Exception as e:
92
  st.error(f"Error: {str(e)}")
93
 
94
  # ==========================================
95
+ # VIEW 2: WEBMASTER TOOLKIT (Original)
96
  # ==========================================
97
+ elif app_mode == "Webmaster Toolkit":
 
98
  st.title("⚡ Webmaster's Toolkit")
 
99
 
100
  tab1, tab2 = st.tabs(["📝 Text Cleaner & SEO", "🖼️ Media Optimizer"])
 
 
 
 
101
 
102
+ # --- TEXT CLEANER TAB ---
103
  with tab1:
104
+ st.write("Paste your raw text or messy HTML below.")
105
+ raw_text = st.text_area("Input Text", height=200)
106
+
107
+ c1, c2, c3 = st.columns(3)
108
+ processed_text = ""
109
+
110
+ if c1.button("Strip HTML"):
111
+ soup = BeautifulSoup(raw_text, "html.parser")
112
+ processed_text = soup.get_text(separator=" ")
113
 
114
+ if c2.button("Fix Spacing"):
115
+ processed_text = re.sub(r'\s+', ' ', raw_text).strip()
116
+
117
+ if c3.button("MD to HTML"):
118
+ processed_text = markdown.markdown(raw_text)
119
+
120
+ if processed_text:
121
+ st.success("Done!")
122
+ st.text_area("Result", value=processed_text, height=200)
123
+
124
+ # --- MEDIA OPTIMIZER TAB ---
125
  with tab2:
126
+ st.write("Convert heavy images to WebP.")
127
+ uploaded_file = st.file_uploader("Upload Image", type=['png', 'jpg', 'jpeg'])
128
+
129
+ if uploaded_file:
130
+ original_image = Image.open(uploaded_file)
131
+ # FIXED: Using use_column_width instead of container_width
132
+ st.image(original_image, caption="Original", use_column_width=True)
133
+
134
+ if st.button("Convert to WebP"):
135
+ buffer = io.BytesIO()
136
+ original_image.save(buffer, format="WEBP", quality=80, optimize=True)
137
+ buffer.seek(0)
138
+ st.success("Converted!")
139
+ st.download_button("Download WebP", data=buffer, file_name="image.webp")