lexicalspace commited on
Commit
575a740
·
verified ·
1 Parent(s): d7a8ff6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py CHANGED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import json
4
+
5
+ # Initialize the client with a high-performance coding model
6
+ # Qwen 2.5 Coder is excellent for understanding code logic and generating structured JSON.
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(model=REPO_ID)
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
+ # Construct a strict prompt for the LLM
19
+ prompt = f"""
20
+ You are an expert SEO specialist and Software Engineer. Analyze the following {file_type} code.
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 it is Python code, use Schema.org 'SoftwareSourceCode'.
30
+ - If it is HTML, use Schema.org 'WebPage' or 'TechArticle'.
31
+
32
+ Strictly follow this output format (Markdown):
33
+
34
+ ## SEO Metadata
35
+ **Title:** [Title Here]
36
+ **Description:** [Description Here]
37
+ **Keywords:** [Keywords Here]
38
+
39
+ ## JSON-LD Structured Data
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
+ # Call the Inference API
53
+ response = client.text_generation(
54
+ prompt,
55
+ max_new_tokens=1024,
56
+ temperature=0.3, # Low temperature for factual/structural consistency
57
+ stop_sequences=["<|endoftext|>"]
58
+ )
59
+ return response
60
+ except Exception as e:
61
+ return f"Error: {str(e)}\n\nPossible fix: The free model tier might be busy. Wait 1 minute and try again, or create a 'HF_TOKEN' secret in your Space settings."
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 for Google Search.
69
+ """
70
+ )
71
+
72
+ with gr.Row():
73
+ with gr.Column():
74
+ input_type = gr.Radio(["python", "html"], label="File Type", value="python")
75
+ code_input = gr.Code(language="python", label="Paste Code Here", lines=10)
76
+ submit_btn = gr.Button("Generate SEO & JSON-LD", variant="primary")
77
+
78
+ with gr.Column():
79
+ output_markdown = gr.Markdown(label="Generated SEO Strategy")
80
+
81
+ # Update syntax highlighting based on radio button
82
+ input_type.change(lambda x: gr.Code(language=x), inputs=input_type, outputs=code_input)
83
+
84
+ # Run logic
85
+ submit_btn.click(
86
+ fn=generate_seo,
87
+ inputs=[code_input, input_type],
88
+ outputs=output_markdown
89
+ )
90
+
91
+ # Launch the app
92
+ if __name__ == "__main__":
93
+ demo.launch()