Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
import json
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
#
|
| 7 |
-
# If you experience rate limits, you can switch to "Qwen/Qwen2.5-7B-Instruct" or "meta-llama/Meta-Llama-3-8B-Instruct"
|
| 8 |
REPO_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 9 |
-
client = InferenceClient(
|
| 10 |
|
| 11 |
def generate_seo(code_snippet, file_type):
|
| 12 |
"""
|
| 13 |
-
Analyzes code and generates SEO metadata + JSON-LD.
|
| 14 |
"""
|
| 15 |
if not code_snippet.strip():
|
| 16 |
return "Please paste some code first."
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
You are an expert SEO specialist and Software Engineer.
|
| 21 |
-
|
| 22 |
-
Your task is to generate SEO metadata and Google Search compliant JSON-LD structured data.
|
| 23 |
|
| 24 |
Output Format requirements:
|
| 25 |
1. Title: A clickable, SEO-optimized title tag (max 60 chars).
|
| 26 |
2. Meta Description: A summary for search engines (max 160 chars).
|
| 27 |
3. Keywords: 5-8 comma-separated high-value keywords.
|
| 28 |
4. JSON-LD: A valid <script type="application/ld+json"> block.
|
| 29 |
-
- If
|
| 30 |
-
- If
|
| 31 |
|
| 32 |
Strictly follow this output format (Markdown):
|
| 33 |
|
|
@@ -40,32 +37,36 @@ def generate_seo(code_snippet, file_type):
|
|
| 40 |
```json
|
| 41 |
[Insert strictly valid JSON code here, no comments]
|
| 42 |
```
|
| 43 |
-
|
| 44 |
-
Here is the code to analyze:
|
| 45 |
-
|
| 46 |
-
```{file_type}
|
| 47 |
-
{code_snippet}
|
| 48 |
-
```
|
| 49 |
"""
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
try:
|
| 52 |
-
#
|
| 53 |
-
response = client.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
# Design the UI
|
| 64 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 65 |
gr.Markdown(
|
| 66 |
"""
|
| 67 |
# 🚀 AI Code-to-SEO Generator
|
| 68 |
-
Paste your **Python** or **HTML** code below to generate optimized meta tags and JSON-LD Schema
|
| 69 |
"""
|
| 70 |
)
|
| 71 |
|
|
@@ -78,7 +79,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 78 |
with gr.Column():
|
| 79 |
output_markdown = gr.Markdown(label="Generated SEO Strategy")
|
| 80 |
|
| 81 |
-
# Update syntax highlighting
|
| 82 |
input_type.change(lambda x: gr.Code(language=x), inputs=input_type, outputs=code_input)
|
| 83 |
|
| 84 |
# Run logic
|
|
@@ -88,6 +89,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 88 |
outputs=output_markdown
|
| 89 |
)
|
| 90 |
|
| 91 |
-
# Launch
|
| 92 |
if __name__ == "__main__":
|
| 93 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
+
# Use Qwen 2.5 Coder 32B (Excellent for code & JSON)
|
| 5 |
+
# We will access it via the Chat API to avoid "Task not supported" errors.
|
|
|
|
| 6 |
REPO_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 7 |
+
client = InferenceClient()
|
| 8 |
|
| 9 |
def generate_seo(code_snippet, file_type):
|
| 10 |
"""
|
| 11 |
+
Analyzes code and generates SEO metadata + JSON-LD using Chat API.
|
| 12 |
"""
|
| 13 |
if not code_snippet.strip():
|
| 14 |
return "Please paste some code first."
|
| 15 |
|
| 16 |
+
# 1. Define the System Prompt (The instructions)
|
| 17 |
+
system_instruction = f"""
|
| 18 |
+
You are an expert SEO specialist and Software Engineer.
|
| 19 |
+
Analyze the user's {file_type} code.
|
|
|
|
| 20 |
|
| 21 |
Output Format requirements:
|
| 22 |
1. Title: A clickable, SEO-optimized title tag (max 60 chars).
|
| 23 |
2. Meta Description: A summary for search engines (max 160 chars).
|
| 24 |
3. Keywords: 5-8 comma-separated high-value keywords.
|
| 25 |
4. JSON-LD: A valid <script type="application/ld+json"> block.
|
| 26 |
+
- If Python: use Schema.org 'SoftwareSourceCode'.
|
| 27 |
+
- If HTML: use Schema.org 'WebPage' or 'TechArticle'.
|
| 28 |
|
| 29 |
Strictly follow this output format (Markdown):
|
| 30 |
|
|
|
|
| 37 |
```json
|
| 38 |
[Insert strictly valid JSON code here, no comments]
|
| 39 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
"""
|
| 41 |
|
| 42 |
+
# 2. Define the User Message (The Code)
|
| 43 |
+
user_message = f"Here is the {file_type} code:\n\n{code_snippet}"
|
| 44 |
+
|
| 45 |
try:
|
| 46 |
+
# 3. Use chat_completion (matches 'conversational' task)
|
| 47 |
+
response = client.chat_completion(
|
| 48 |
+
model=REPO_ID,
|
| 49 |
+
messages=[
|
| 50 |
+
{"role": "system", "content": system_instruction},
|
| 51 |
+
{"role": "user", "content": user_message}
|
| 52 |
+
],
|
| 53 |
+
max_tokens=1024,
|
| 54 |
+
temperature=0.3
|
| 55 |
)
|
| 56 |
+
|
| 57 |
+
# Extract the actual message content
|
| 58 |
+
return response.choices[0].message.content
|
| 59 |
+
|
| 60 |
except Exception as e:
|
| 61 |
+
# Fallback error handling
|
| 62 |
+
return f"Error: {str(e)}\n\nTry switching the model in the code to 'meta-llama/Meta-Llama-3-8B-Instruct' if this persists."
|
| 63 |
|
| 64 |
# Design the UI
|
| 65 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 66 |
gr.Markdown(
|
| 67 |
"""
|
| 68 |
# 🚀 AI Code-to-SEO Generator
|
| 69 |
+
Paste your **Python** or **HTML** code below to generate optimized meta tags and JSON-LD Schema.
|
| 70 |
"""
|
| 71 |
)
|
| 72 |
|
|
|
|
| 79 |
with gr.Column():
|
| 80 |
output_markdown = gr.Markdown(label="Generated SEO Strategy")
|
| 81 |
|
| 82 |
+
# Update syntax highlighting dynamically
|
| 83 |
input_type.change(lambda x: gr.Code(language=x), inputs=input_type, outputs=code_input)
|
| 84 |
|
| 85 |
# Run logic
|
|
|
|
| 89 |
outputs=output_markdown
|
| 90 |
)
|
| 91 |
|
| 92 |
+
# Launch
|
| 93 |
if __name__ == "__main__":
|
| 94 |
demo.launch()
|