File size: 3,519 Bytes
768c740
7d19342
 
b8a6b71
7d19342
 
6784902
20bfd0b
56f7cbb
b5a890a
b8a6b71
7d19342
b5a890a
7d19342
 
b5a890a
20bfd0b
7d19342
b5a890a
7d19342
b5a890a
 
7d19342
 
b5a890a
768c740
b5a890a
 
768c740
b5a890a
 
768c740
b5a890a
20bfd0b
b5a890a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20bfd0b
b5a890a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8a6b71
b5a890a
 
 
 
3dcaa0d
 
 
1d1f3c9
3dcaa0d
1d1f3c9
 
3dcaa0d
b5a890a
1d1f3c9
3dcaa0d
1d1f3c9
 
b5a890a
1d1f3c9
3dcaa0d
eacf582
 
 
b5a890a
 
 
 
3dcaa0d
b5a890a
 
7d19342
20bfd0b
b5a890a
20bfd0b
b5a890a
7d19342
768c740
3dcaa0d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import gradio as gr
import requests
from bs4 import BeautifulSoup
from nltk import download, sent_tokenize
import google.generativeai as genai
import os
import re
import tempfile

# Download NLTK tokenizer data
download('punkt')

# Set your Gemini API key
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
    raise ValueError("GEMINI_API_KEY not found in environment variables")

genai.configure(api_key=api_key)
model = genai.GenerativeModel("gemini-pro")

# Helper functions
def extract_text_from_url(url):
    try:
        response = requests.get(url, timeout=10)
        soup = BeautifulSoup(response.text, "html.parser")

        for script in soup(["script", "style", "header", "footer", "nav"]):
            script.decompose()

        paragraphs = soup.find_all("p")
        blog_text = "\n".join(p.get_text() for p in paragraphs if len(p.get_text()) > 40)

        return blog_text.strip()
    except Exception as e:
        return f"Error extracting content: {str(e)}"

def review_blog(blog_text):
    prompt = f"""
You are a professional AI blog reviewer and sensitive content analyst.

Carefully analyze the following blog content for:
1. Grammar, spelling, and clarity improvements
2. Crude, harsh, or unprofessional language
3. Sensitive, biased, offensive, or policy-violating content

Provide detailed suggestions and highlight what should be improved.

BLOG:
\"\"\"
{blog_text}
\"\"\"
"""
    response = model.generate_content(prompt)
    return response.text

def process_input(text_input, url_input):
    if url_input:
        blog_content = extract_text_from_url(url_input)
    else:
        blog_content = text_input

    if not blog_content or blog_content.strip() == "":
        return "Please provide blog content or a valid URL.", None

    analysis = review_blog(blog_content)

    # Create a temporary file with the markdown review
    with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".md") as tmp_file:
        tmp_file.write(analysis)
        file_path = tmp_file.name

    return analysis, file_path

# Gradio UI
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🧠 Blog Checker AI")
    gr.Markdown("Review blogs for grammar, clarity, and policy issues using Gemini AI.")

    with gr.Row(equal_height=True):
        with gr.Column(scale=3):
            with gr.Tabs():
                with gr.TabItem("✍️ Text Input"):
                    text_input = gr.Textbox(
                        label="Paste Your Blog Content",
                        placeholder="Write or paste your blog text here...",
                        lines=12
                    )
                with gr.TabItem("πŸ”— URL Input"):
                    url_input = gr.Textbox(
                        label="Enter Blog URL",
                        placeholder="https://example.com/blog"
                    )
        with gr.Column(scale=2):
            gr.Markdown("""
### πŸš€ How It Works
1. **Input**: Paste your blog text or provide a blog URL  
2. **Analysis**: Gemini AI checks for grammar, legal violations, and sensitive content  
3. **Report**: Instantly download a structured Markdown review
            """)
            status_button = gr.Button("🧠 Review Blog")

    output_text = gr.Textbox(label="Review Summary", lines=20)
    download_button = gr.File(label="Download Markdown Review")

    status_button.click(
        fn=process_input,
        inputs=[text_input, url_input],
        outputs=[output_text, download_button]
    )

demo.launch()