JaishnaCodz's picture
Update app.py
b5a890a verified
raw
history blame
3.52 kB
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()