lexicalspace commited on
Commit
a5f8eda
·
verified ·
1 Parent(s): 843e4e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
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,7 @@ 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:
@@ -879,6 +984,8 @@ if __name__ == "__main__":
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()
884
  if st.button("📐 Image Resizer"): set_mode("resizer"); st.rerun()
 
17
 
18
 
19
  # --- BATCH 1: MEDIA & FILE FUNCTIONS ---
20
+ import streamlit as st
21
+ from huggingface_hub import InferenceClient
22
+ import os
23
+
24
+ # --- 1. The Logic Function (Backend) ---
25
+ # We use @st.cache_data so if the user clicks other buttons, we don't re-run the expensive API call.
26
+ @st.cache_data(show_spinner="Analyzing code with Qwen AI...")
27
+ def get_seo_data(code_snippet, file_type, api_key):
28
+ """
29
+ Sends code to Hugging Face Inference API and returns SEO/JSON-LD strategy.
30
+ """
31
+ if not code_snippet:
32
+ return None, "⚠️ Please paste some code first."
33
+
34
+ if not api_key:
35
+ return None, "❌ Error: HF_TOKEN not found in secrets."
36
+
37
+ try:
38
+ client = InferenceClient(api_key=api_key)
39
+
40
+ # Strict Prompt for Qwen 2.5 Coder
41
+ system_instruction = f"""
42
+ You are an expert Technical SEO Specialist. Analyze the user's {file_type} code.
43
+
44
+ Task: Generate Google-compliant JSON-LD structured data and SEO meta tags.
45
+
46
+ Output Format (Strict Markdown):
47
+ ## SEO Metadata
48
+ **Title:** [Engaging Title, max 60 chars]
49
+ **Description:** [Summary including keywords, max 160 chars]
50
+ **Keywords:** [5-8 comma-separated keywords]
51
+
52
+ ## JSON-LD Structured Data
53
+ ```json
54
+ [Insert VALID JSON-LD here.
55
+ - If Python: Use schema.org/SoftwareSourceCode
56
+ - If HTML: Use schema.org/WebPage or schema.org/TechArticle]
57
+ ```
58
+ """
59
+
60
+ user_message = f"Analyze this {file_type} code:\n\n{code_snippet}"
61
+
62
+ response = client.chat_completion(
63
+ model="Qwen/Qwen2.5-Coder-32B-Instruct",
64
+ messages=[
65
+ {"role": "system", "content": system_instruction},
66
+ {"role": "user", "content": user_message}
67
+ ],
68
+ max_tokens=1500,
69
+ temperature=0.2
70
+ )
71
+ return response.choices[0].message.content, None
72
+
73
+ except Exception as e:
74
+ return None, f"Error: {str(e)}"
75
+
76
+ # --- 2. The UI Function (Frontend) ---
77
+ def render_seo_ui():
78
+ """
79
+ Call this function in your main app.py where you want the SEO tool to show up.
80
+ """
81
+ st.header("🚀 AI Code-to-SEO Generator")
82
+ st.markdown("Generate **JSON-LD** and **Meta Tags** for your Python/HTML files using Qwen 2.5 Coder.")
83
+
84
+ # Get Token (Try secrets first, then env var)
85
+ try:
86
+ hf_token = st.secrets["HF_TOKEN"]
87
+ except:
88
+ hf_token = os.getenv("HF_TOKEN")
89
+
90
+ with st.form("seo_form"):
91
+ col1, col2 = st.columns([1, 3])
92
+
93
+ with col1:
94
+ file_type = st.radio("File Type", ["Python", "HTML"])
95
+
96
+ with col2:
97
+ code_input = st.text_area("Paste Code Here", height=300, placeholder="import os...")
98
+
99
+ submitted = st.form_submit_button("✨ Generate SEO Data")
100
+
101
+ if submitted:
102
+ if not hf_token:
103
+ st.error("Authentication Error: Please add `HF_TOKEN` to your Streamlit secrets.")
104
+ else:
105
+ result, error = get_seo_data(code_input, file_type, hf_token)
106
+
107
+ if error:
108
+ st.error(error)
109
+ else:
110
+ st.success("SEO Data Generated Successfully!")
111
+ st.markdown("---")
112
+ st.markdown(result)
113
+
114
+ # Optional: Add a copy button for the raw result
115
+ st.download_button(
116
+ label="📥 Download Result",
117
+ data=result,
118
+ file_name="seo_strategy.md",
119
+ mime="text/markdown"
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": render_seo_ui()
974
 
975
  # 5. HOME DASHBOARD (Button Grid)
976
  else:
 
984
  with c1:
985
  st.info("**📂 Media & Files**")
986
  if st.button("🎥 YouTube Downloader"): set_mode("youtube"); st.rerun()
987
+ if st.button("🎥 Seo Generator"): set_mode("seo"); st.rerun()
988
+
989
  if st.button("🔄 Smart File Converter"): set_mode("smart_converter"); st.rerun()
990
  if st.button("📉 Image Compressor"): set_mode("compressor"); st.rerun()
991
  if st.button("📐 Image Resizer"): set_mode("resizer"); st.rerun()