Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,183 +6,75 @@ import google.generativeai as genai
|
|
6 |
import os
|
7 |
import re
|
8 |
import tempfile
|
9 |
-
import asyncio
|
10 |
-
import time
|
11 |
|
12 |
-
# Download NLTK tokenizer
|
13 |
download('punkt')
|
14 |
|
15 |
-
#
|
16 |
api_key = os.environ.get("GEMINI_API_KEY")
|
17 |
if not api_key:
|
18 |
-
raise ValueError("GEMINI_API_KEY not found in environment variables
|
19 |
|
20 |
genai.configure(api_key=api_key)
|
|
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
model = genai.GenerativeModel('gemini-1.5-flash')
|
25 |
-
except Exception as e:
|
26 |
-
print(f"Error initializing model: {e}")
|
27 |
-
for m in genai.list_models():
|
28 |
-
print(m.name)
|
29 |
-
raise
|
30 |
-
|
31 |
-
# Prompt template
|
32 |
-
PROMPT = """
|
33 |
-
You are an AI content reviewer. Analyze the provided text for the following:
|
34 |
-
1. Grammar Issues: Identify and suggest corrections for grammatical errors.
|
35 |
-
2. Legal Policy Violations: Flag content that may violate common legal policies.
|
36 |
-
3. Crude/Abusive Language: Detect crude, offensive, or abusive language.
|
37 |
-
4. Sensitive Topics: Identify content related to sensitive topics like racism or discrimination.
|
38 |
-
|
39 |
-
Return the results in this markdown format:
|
40 |
-
# Blog Review Report
|
41 |
-
|
42 |
-
## Grammar Corrections
|
43 |
-
1. [Heading of issue]
|
44 |
-
- CONTENT: [Text]
|
45 |
-
- SUGGESTION: [Fix]
|
46 |
-
- ISSUE: [Explanation]
|
47 |
-
|
48 |
-
## Legal Policy Violations
|
49 |
-
- CONTENT: [...]
|
50 |
-
SUGGESTION: [...]
|
51 |
-
ISSUE: [...]
|
52 |
-
|
53 |
-
## Crude/Abusive Language
|
54 |
-
- [Text or “None detected”]
|
55 |
-
|
56 |
-
## Sensitive Topics
|
57 |
-
- [Text or “None detected”]
|
58 |
-
"""
|
59 |
-
|
60 |
-
# Fetch blog content from URL
|
61 |
-
async def fetch_url_content(url):
|
62 |
try:
|
63 |
response = requests.get(url, timeout=10)
|
64 |
-
response.
|
65 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
66 |
-
content = ' '.join([p.get_text(strip=True) for p in soup.find_all(['p', 'article', 'div'])])
|
67 |
-
return content or "No readable content found on the page."
|
68 |
-
except Exception as e:
|
69 |
-
return f"Error fetching URL: {str(e)}"
|
70 |
-
|
71 |
-
# Review function
|
72 |
-
async def review_blog(text_input, url_input, progress=gr.Progress()):
|
73 |
-
if text_input and not url_input:
|
74 |
-
input_text = text_input
|
75 |
-
elif url_input and not text_input:
|
76 |
-
input_text = await fetch_url_content(url_input)
|
77 |
-
if input_text.startswith("Error"):
|
78 |
-
return "Review Blog", input_text, gr.update(visible=False)
|
79 |
-
else:
|
80 |
-
return "Review Blog", "Error: Provide either text or URL, not both.", gr.update(visible=False)
|
81 |
|
82 |
-
|
83 |
-
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
response = await asyncio.to_thread(model.generate_content, PROMPT + "\n\nText to analyze:\n" + analysis_text)
|
88 |
-
report = response.text.strip()
|
89 |
-
report = re.sub(r'^markdown\n|$', '', report, flags=re.MULTILINE)
|
90 |
-
except Exception as e:
|
91 |
-
return "Review Blog", f"Error with Gemini API: {str(e)}", gr.update(visible=False)
|
92 |
|
93 |
-
|
94 |
-
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False, encoding='utf-8') as temp_file:
|
95 |
-
temp_file.write(report)
|
96 |
-
file_path = temp_file.name
|
97 |
-
progress(1.0, desc="Report Ready!")
|
98 |
-
return "Review Blog", report, gr.update(visible=True, value=file_path)
|
99 |
except Exception as e:
|
100 |
-
return
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
}
|
118 |
-
|
119 |
-
input, textarea {
|
120 |
-
border: 2px solid #cbd5e0;
|
121 |
-
border-radius: 12px;
|
122 |
-
padding: 15px;
|
123 |
-
background-color: #ffffff;
|
124 |
-
font-size: 16px;
|
125 |
-
color: #2d3748;
|
126 |
-
transition: border 0.3s ease;
|
127 |
-
}
|
128 |
-
|
129 |
-
input:focus, textarea:focus {
|
130 |
-
border-color: #3182ce;
|
131 |
-
outline: none;
|
132 |
-
}
|
133 |
-
|
134 |
-
.review-btn {
|
135 |
-
background: linear-gradient(45deg, #3182ce, #63b3ed);
|
136 |
-
padding: 14px 28px;
|
137 |
-
border-radius: 12px;
|
138 |
-
font-size: 16px;
|
139 |
-
color: #fff;
|
140 |
-
font-weight: 600;
|
141 |
-
border: none;
|
142 |
-
cursor: pointer;
|
143 |
-
transition: all 0.3s ease;
|
144 |
-
}
|
145 |
-
|
146 |
-
.review-btn:hover {
|
147 |
-
transform: scale(1.04);
|
148 |
-
background: linear-gradient(45deg, #63b3ed, #3182ce);
|
149 |
-
}
|
150 |
-
|
151 |
-
.markdown-container {
|
152 |
-
background: #ffffff;
|
153 |
-
padding: 25px;
|
154 |
-
border-radius: 16px;
|
155 |
-
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
|
156 |
-
margin-top: 25px;
|
157 |
-
font-size: 15px;
|
158 |
-
line-height: 1.6;
|
159 |
-
}
|
160 |
-
|
161 |
-
.download-btn {
|
162 |
-
background-color: #38a169;
|
163 |
-
color: white;
|
164 |
-
padding: 12px 20px;
|
165 |
-
border-radius: 10px;
|
166 |
-
font-weight: 600;
|
167 |
-
transition: background 0.3s ease;
|
168 |
-
}
|
169 |
-
|
170 |
-
.download-btn:hover {
|
171 |
-
background-color: #2f855a;
|
172 |
-
}
|
173 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
-
#
|
176 |
-
with gr.Blocks(
|
177 |
-
|
178 |
-
|
179 |
-
<div style="text-align: center; margin-bottom: 10px;">
|
180 |
-
<h1 style="color:#2b6cb0; font-size: 2.5rem;">✨ AI Blog Reviewer</h1>
|
181 |
-
<p style="font-size: 1.1rem; color: #4a5568;">
|
182 |
-
Analyze your blog for grammar, legal violations, abusive content, and more.
|
183 |
-
</p>
|
184 |
-
</div>
|
185 |
-
""")
|
186 |
|
187 |
with gr.Row(equal_height=True):
|
188 |
with gr.Column(scale=3):
|
@@ -191,35 +83,29 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
191 |
text_input = gr.Textbox(
|
192 |
label="Paste Your Blog Content",
|
193 |
placeholder="Write or paste your blog text here...",
|
194 |
-
lines=12
|
195 |
-
elem_classes=["input-text"]
|
196 |
)
|
197 |
with gr.TabItem("🔗 URL Input"):
|
198 |
url_input = gr.Textbox(
|
199 |
label="Enter Blog URL",
|
200 |
-
placeholder="https://example.com/blog"
|
201 |
-
lines=1,
|
202 |
-
elem_classes=["input-url"]
|
203 |
)
|
204 |
with gr.Column(scale=2):
|
205 |
gr.Markdown("""
|
206 |
### 🚀 How It Works
|
207 |
1. **Input**: Paste your blog text or provide a blog URL
|
208 |
-
2. **Analysis**: Gemini AI checks for grammar
|
209 |
-
3. **Report**: Instantly download a
|
210 |
-
""")
|
211 |
-
status_button = gr.Button("🧠 Review Blog"
|
212 |
|
213 |
-
gr.
|
214 |
-
|
215 |
-
with gr.Column():
|
216 |
-
report_output = gr.Markdown(elem_classes=["markdown-container"])
|
217 |
-
download_btn = gr.File(label="Download Report", visible=False, elem_classes=["download-btn"])
|
218 |
|
219 |
status_button.click(
|
220 |
-
fn=
|
221 |
inputs=[text_input, url_input],
|
222 |
-
outputs=[
|
223 |
)
|
224 |
|
225 |
demo.launch()
|
|
|
6 |
import os
|
7 |
import re
|
8 |
import tempfile
|
|
|
|
|
9 |
|
10 |
+
# Download NLTK tokenizer data
|
11 |
download('punkt')
|
12 |
|
13 |
+
# Set your Gemini API key
|
14 |
api_key = os.environ.get("GEMINI_API_KEY")
|
15 |
if not api_key:
|
16 |
+
raise ValueError("GEMINI_API_KEY not found in environment variables")
|
17 |
|
18 |
genai.configure(api_key=api_key)
|
19 |
+
model = genai.GenerativeModel("gemini-pro")
|
20 |
|
21 |
+
# Helper functions
|
22 |
+
def extract_text_from_url(url):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
try:
|
24 |
response = requests.get(url, timeout=10)
|
25 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
for script in soup(["script", "style", "header", "footer", "nav"]):
|
28 |
+
script.decompose()
|
29 |
|
30 |
+
paragraphs = soup.find_all("p")
|
31 |
+
blog_text = "\n".join(p.get_text() for p in paragraphs if len(p.get_text()) > 40)
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
return blog_text.strip()
|
|
|
|
|
|
|
|
|
|
|
34 |
except Exception as e:
|
35 |
+
return f"Error extracting content: {str(e)}"
|
36 |
+
|
37 |
+
def review_blog(blog_text):
|
38 |
+
prompt = f"""
|
39 |
+
You are a professional AI blog reviewer and sensitive content analyst.
|
40 |
+
|
41 |
+
Carefully analyze the following blog content for:
|
42 |
+
1. Grammar, spelling, and clarity improvements
|
43 |
+
2. Crude, harsh, or unprofessional language
|
44 |
+
3. Sensitive, biased, offensive, or policy-violating content
|
45 |
+
|
46 |
+
Provide detailed suggestions and highlight what should be improved.
|
47 |
+
|
48 |
+
BLOG:
|
49 |
+
\"\"\"
|
50 |
+
{blog_text}
|
51 |
+
\"\"\"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
"""
|
53 |
+
response = model.generate_content(prompt)
|
54 |
+
return response.text
|
55 |
+
|
56 |
+
def process_input(text_input, url_input):
|
57 |
+
if url_input:
|
58 |
+
blog_content = extract_text_from_url(url_input)
|
59 |
+
else:
|
60 |
+
blog_content = text_input
|
61 |
+
|
62 |
+
if not blog_content or blog_content.strip() == "":
|
63 |
+
return "Please provide blog content or a valid URL.", None
|
64 |
+
|
65 |
+
analysis = review_blog(blog_content)
|
66 |
+
|
67 |
+
# Create a temporary file with the markdown review
|
68 |
+
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".md") as tmp_file:
|
69 |
+
tmp_file.write(analysis)
|
70 |
+
file_path = tmp_file.name
|
71 |
+
|
72 |
+
return analysis, file_path
|
73 |
|
74 |
+
# Gradio UI
|
75 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
76 |
+
gr.Markdown("# 🧠 Blog Checker AI")
|
77 |
+
gr.Markdown("Review blogs for grammar, clarity, and policy issues using Gemini AI.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
with gr.Row(equal_height=True):
|
80 |
with gr.Column(scale=3):
|
|
|
83 |
text_input = gr.Textbox(
|
84 |
label="Paste Your Blog Content",
|
85 |
placeholder="Write or paste your blog text here...",
|
86 |
+
lines=12
|
|
|
87 |
)
|
88 |
with gr.TabItem("🔗 URL Input"):
|
89 |
url_input = gr.Textbox(
|
90 |
label="Enter Blog URL",
|
91 |
+
placeholder="https://example.com/blog"
|
|
|
|
|
92 |
)
|
93 |
with gr.Column(scale=2):
|
94 |
gr.Markdown("""
|
95 |
### 🚀 How It Works
|
96 |
1. **Input**: Paste your blog text or provide a blog URL
|
97 |
+
2. **Analysis**: Gemini AI checks for grammar, legal violations, and sensitive content
|
98 |
+
3. **Report**: Instantly download a structured Markdown review
|
99 |
+
""")
|
100 |
+
status_button = gr.Button("🧠 Review Blog")
|
101 |
|
102 |
+
output_text = gr.Textbox(label="Review Summary", lines=20)
|
103 |
+
download_button = gr.File(label="Download Markdown Review")
|
|
|
|
|
|
|
104 |
|
105 |
status_button.click(
|
106 |
+
fn=process_input,
|
107 |
inputs=[text_input, url_input],
|
108 |
+
outputs=[output_text, download_button]
|
109 |
)
|
110 |
|
111 |
demo.launch()
|