Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import textstat
|
3 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Set up Hugging Face API token and model endpoint
|
7 |
+
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN") # Ensure you have your token set in your environment
|
8 |
+
llm = HuggingFaceEndpoint(
|
9 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
10 |
+
huggingfacehub_api_token=HF_TOKEN.strip(),
|
11 |
+
temperature=0.7,
|
12 |
+
max_new_tokens=200
|
13 |
+
)
|
14 |
+
|
15 |
+
def check_and_improve_seo(content):
|
16 |
+
# Define basic SEO criteria
|
17 |
+
keywords = ["SEO", "content", "optimization", "keywords", "readability"]
|
18 |
+
keyword_found = any(keyword.lower() in content.lower() for keyword in keywords)
|
19 |
+
|
20 |
+
# Check readability score
|
21 |
+
readability_score = textstat.flesch_reading_ease(content)
|
22 |
+
|
23 |
+
# Prepare a prompt for the LLM to improve content
|
24 |
+
prompt = (
|
25 |
+
"Optimize the following content for SEO. Ensure it includes relevant keywords, "
|
26 |
+
"is easy to read, and meets SEO best practices.\n\n"
|
27 |
+
"Content:\n" + content
|
28 |
+
)
|
29 |
+
|
30 |
+
# Generate SEO-optimized content using the Hugging Face model
|
31 |
+
response = llm(prompt)
|
32 |
+
optimized_content = response['text'] if 'text' in response else response
|
33 |
+
|
34 |
+
# Define SEO checks
|
35 |
+
seo_checks = {
|
36 |
+
"Keywords Present": keyword_found,
|
37 |
+
"Readability Score (Flesch)": readability_score,
|
38 |
+
"Optimized Content": optimized_content
|
39 |
+
}
|
40 |
+
|
41 |
+
return seo_checks
|
42 |
+
|
43 |
+
# Define Gradio interface
|
44 |
+
interface = gr.Interface(
|
45 |
+
fn=check_and_improve_seo,
|
46 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter your content here..."),
|
47 |
+
outputs="json",
|
48 |
+
title="SEO Compatibility Checker and Optimizer",
|
49 |
+
description="Check if the given content is SEO compatible and get an improved version based on SEO best practices."
|
50 |
+
)
|
51 |
+
|
52 |
+
# Launch the app
|
53 |
+
if __name__ == "__main__":
|
54 |
+
interface.launch()
|