Update app.py
Browse files
app.py
CHANGED
|
@@ -17,6 +17,110 @@ from bs4 import BeautifulSoup
|
|
| 17 |
|
| 18 |
|
| 19 |
# --- BATCH 1: MEDIA & FILE FUNCTIONS ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def tool_youtube_downloader():
|
| 22 |
st.header("🎥 YouTube Media Extractor")
|
|
@@ -866,6 +970,8 @@ if __name__ == "__main__":
|
|
| 866 |
elif mode == "ratio": tool_aspect_ratio()
|
| 867 |
elif mode == "stopwatch": tool_stopwatch()
|
| 868 |
elif mode == "python": tool_python_checker()
|
|
|
|
|
|
|
| 869 |
|
| 870 |
# 5. HOME DASHBOARD (Button Grid)
|
| 871 |
else:
|
|
@@ -878,6 +984,7 @@ if __name__ == "__main__":
|
|
| 878 |
|
| 879 |
with c1:
|
| 880 |
st.info("**📂 Media & Files**")
|
|
|
|
| 881 |
if st.button("🎥 YouTube Downloader"): set_mode("youtube"); st.rerun()
|
| 882 |
if st.button("🔄 Smart File Converter"): set_mode("smart_converter"); st.rerun()
|
| 883 |
if st.button("📉 Image Compressor"): set_mode("compressor"); st.rerun()
|
|
@@ -915,4 +1022,6 @@ if __name__ == "__main__":
|
|
| 915 |
if st.button("💪 Password Strength"): set_mode("password"); st.rerun()
|
| 916 |
if st.button("🖥️ Aspect Ratio"): set_mode("ratio"); st.rerun()
|
| 917 |
if st.button("⏱️ Stopwatch"): set_mode("stopwatch"); st.rerun()
|
| 918 |
-
if st.button("Python Checker"): set_mode("python"); st.rerun()
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
# --- BATCH 1: MEDIA & FILE FUNCTIONS ---
|
| 20 |
+
import gradio as gr
|
| 21 |
+
import os
|
| 22 |
+
from huggingface_hub import InferenceClient
|
| 23 |
+
|
| 24 |
+
def run_seo_app():
|
| 25 |
+
"""
|
| 26 |
+
Encapsulates the entire SEO generation system, authentication, and UI
|
| 27 |
+
into a single executable function.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
# --- 1. SETUP & AUTHENTICATION ---
|
| 31 |
+
# Get the token from the Space secrets to fix "Auth" errors
|
| 32 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 33 |
+
|
| 34 |
+
# Initialize the client with Qwen 2.5 Coder 32B
|
| 35 |
+
model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 36 |
+
client = InferenceClient(token=hf_token)
|
| 37 |
+
|
| 38 |
+
# --- 2. LOGIC FUNCTION ---
|
| 39 |
+
def generate_seo(code_snippet, file_type):
|
| 40 |
+
if not code_snippet.strip():
|
| 41 |
+
return "⚠️ Error: Please paste some code first."
|
| 42 |
+
|
| 43 |
+
# Define the strict SEO prompt
|
| 44 |
+
system_instruction = f"""
|
| 45 |
+
You are an expert Technical SEO Specialist. Analyze the user's {file_type} code.
|
| 46 |
+
|
| 47 |
+
Your Goal: Generate Google-compliant JSON-LD structured data and SEO meta tags.
|
| 48 |
+
|
| 49 |
+
Output Format (Strict Markdown):
|
| 50 |
+
## SEO Metadata
|
| 51 |
+
**Title:** [Engaging Title, max 60 chars]
|
| 52 |
+
**Description:** [Summary including keywords, max 160 chars]
|
| 53 |
+
**Keywords:** [5-8 comma-separated keywords]
|
| 54 |
+
|
| 55 |
+
## JSON-LD Structured Data
|
| 56 |
+
```json
|
| 57 |
+
[Insert VALID JSON-LD here.
|
| 58 |
+
- If Python: Use schema.org/SoftwareSourceCode
|
| 59 |
+
- If HTML: Use schema.org/WebPage or schema.org/TechArticle]
|
| 60 |
+
```
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
user_message = f"Analyze this {file_type} code:\n\n{code_snippet}"
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
# Call the Chat Completion API
|
| 67 |
+
response = client.chat_completion(
|
| 68 |
+
model=model_id,
|
| 69 |
+
messages=[
|
| 70 |
+
{"role": "system", "content": system_instruction},
|
| 71 |
+
{"role": "user", "content": user_message}
|
| 72 |
+
],
|
| 73 |
+
max_tokens=1500,
|
| 74 |
+
temperature=0.2
|
| 75 |
+
)
|
| 76 |
+
return response.choices[0].message.content
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
# Error handling logic
|
| 80 |
+
error_msg = str(e)
|
| 81 |
+
if "401" in error_msg:
|
| 82 |
+
return "🔒 Authentication Error: Please check that you added 'HF_TOKEN' to your Space Secrets."
|
| 83 |
+
elif "429" in error_msg:
|
| 84 |
+
return "⏳ Rate Limit: The free model is busy. Please wait 1 minute and try again."
|
| 85 |
+
elif "504" in error_msg:
|
| 86 |
+
return "⏱️ Timeout: The code snippet might be too long. Try a shorter piece of code."
|
| 87 |
+
else:
|
| 88 |
+
return f"❌ System Error: {error_msg}"
|
| 89 |
+
|
| 90 |
+
# --- 3. UI CONSTRUCTION ---
|
| 91 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 92 |
+
gr.Markdown(
|
| 93 |
+
"""
|
| 94 |
+
# ⚡ SEO & JSON-LD Generator (Authenticated)
|
| 95 |
+
**Status:** ✅ Connected to Qwen 2.5 Coder
|
| 96 |
+
"""
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
with gr.Row():
|
| 100 |
+
with gr.Column(scale=1):
|
| 101 |
+
input_type = gr.Radio(["python", "html"], label="Select File Type", value="python")
|
| 102 |
+
code_input = gr.Code(language="python", label="Paste Code Here", lines=15)
|
| 103 |
+
submit_btn = gr.Button("✨ Generate SEO Data", variant="primary", size="lg")
|
| 104 |
+
|
| 105 |
+
with gr.Column(scale=1):
|
| 106 |
+
output_markdown = gr.Markdown(label="Results will appear here...")
|
| 107 |
+
|
| 108 |
+
# Dynamic syntax highlighting
|
| 109 |
+
input_type.change(lambda x: gr.Code(language=x), inputs=input_type, outputs=code_input)
|
| 110 |
+
|
| 111 |
+
# Button Click Action
|
| 112 |
+
submit_btn.click(
|
| 113 |
+
fn=generate_seo,
|
| 114 |
+
inputs=[code_input, input_type],
|
| 115 |
+
outputs=output_markdown
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# --- 4. LAUNCH ---
|
| 119 |
+
demo.launch()
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
|
| 125 |
def tool_youtube_downloader():
|
| 126 |
st.header("🎥 YouTube Media Extractor")
|
|
|
|
| 970 |
elif mode == "ratio": tool_aspect_ratio()
|
| 971 |
elif mode == "stopwatch": tool_stopwatch()
|
| 972 |
elif mode == "python": tool_python_checker()
|
| 973 |
+
elif mode == "seo": run_seo_app()
|
| 974 |
+
|
| 975 |
|
| 976 |
# 5. HOME DASHBOARD (Button Grid)
|
| 977 |
else:
|
|
|
|
| 984 |
|
| 985 |
with c1:
|
| 986 |
st.info("**📂 Media & Files**")
|
| 987 |
+
if st.button("Seo Writer"): set_mode("seo"); st.rerun()
|
| 988 |
if st.button("🎥 YouTube Downloader"): set_mode("youtube"); st.rerun()
|
| 989 |
if st.button("🔄 Smart File Converter"): set_mode("smart_converter"); st.rerun()
|
| 990 |
if st.button("📉 Image Compressor"): set_mode("compressor"); st.rerun()
|
|
|
|
| 1022 |
if st.button("💪 Password Strength"): set_mode("password"); st.rerun()
|
| 1023 |
if st.button("🖥️ Aspect Ratio"): set_mode("ratio"); st.rerun()
|
| 1024 |
if st.button("⏱️ Stopwatch"): set_mode("stopwatch"); st.rerun()
|
| 1025 |
+
if st.button("Python Checker"): set_mode("python"); st.rerun()
|
| 1026 |
+
|
| 1027 |
+
|