Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,56 +9,55 @@ 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 |
try:
|
| 23 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 24 |
except Exception as e:
|
| 25 |
print(f"Error initializing model: {e}")
|
| 26 |
-
print("Available models:")
|
| 27 |
for m in genai.list_models():
|
| 28 |
print(m.name)
|
| 29 |
raise
|
| 30 |
|
|
|
|
| 31 |
PROMPT = """
|
| 32 |
You are an AI content reviewer. Analyze the provided text for the following:
|
| 33 |
1. Grammar Issues: Identify and suggest corrections for grammatical errors.
|
| 34 |
-
2. Legal Policy Violations: Flag content that may violate common legal policies
|
| 35 |
3. Crude/Abusive Language: Detect crude, offensive, or abusive language.
|
| 36 |
-
4. Sensitive Topics: Identify content related to sensitive topics
|
| 37 |
|
| 38 |
-
Return the results in
|
| 39 |
# Blog Review Report
|
| 40 |
|
| 41 |
## Grammar Corrections
|
| 42 |
1. [Heading of issue]
|
| 43 |
-
- CONTENT: [
|
| 44 |
-
- SUGGESTION: [
|
| 45 |
-
- ISSUE: [
|
| 46 |
-
|
| 47 |
-
[Continue numbering for additional issues or state "None detected"]
|
| 48 |
|
| 49 |
## Legal Policy Violations
|
| 50 |
-
- CONTENT: [
|
| 51 |
-
SUGGESTION: [
|
| 52 |
-
ISSUE: [
|
| 53 |
-
[Or state "None detected"]
|
| 54 |
|
| 55 |
## Crude/Abusive Language
|
| 56 |
-
- [
|
| 57 |
|
| 58 |
## Sensitive Topics
|
| 59 |
-
- [
|
| 60 |
"""
|
| 61 |
|
|
|
|
| 62 |
async def fetch_url_content(url):
|
| 63 |
try:
|
| 64 |
response = requests.get(url, timeout=10)
|
|
@@ -69,6 +68,7 @@ async def fetch_url_content(url):
|
|
| 69 |
except Exception as e:
|
| 70 |
return f"Error fetching URL: {str(e)}"
|
| 71 |
|
|
|
|
| 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
|
|
@@ -82,7 +82,7 @@ async def review_blog(text_input, url_input, progress=gr.Progress()):
|
|
| 82 |
sentences = sent_tokenize(input_text)
|
| 83 |
analysis_text = "\n".join(sentences)
|
| 84 |
|
| 85 |
-
progress(0.2, desc="Analyzing...")
|
| 86 |
try:
|
| 87 |
response = await asyncio.to_thread(model.generate_content, PROMPT + "\n\nText to analyze:\n" + analysis_text)
|
| 88 |
report = response.text.strip()
|
|
@@ -202,16 +202,12 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 202 |
elem_classes=["input-url"]
|
| 203 |
)
|
| 204 |
with gr.Column(scale=2):
|
| 205 |
-
gr.
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
<li>Download the review as Markdown</li>
|
| 212 |
-
</ul>
|
| 213 |
-
</div>
|
| 214 |
-
""")
|
| 215 |
status_button = gr.Button("π§ Review Blog", elem_classes=["review-btn"])
|
| 216 |
|
| 217 |
gr.Markdown("### π Review Report")
|
|
|
|
| 9 |
import asyncio
|
| 10 |
import time
|
| 11 |
|
| 12 |
+
# Download NLTK tokenizer
|
| 13 |
download('punkt')
|
| 14 |
|
| 15 |
+
# Configure Gemini API
|
| 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 |
+
# Load Gemini model
|
| 23 |
try:
|
| 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)
|
|
|
|
| 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
|
|
|
|
| 82 |
sentences = sent_tokenize(input_text)
|
| 83 |
analysis_text = "\n".join(sentences)
|
| 84 |
|
| 85 |
+
progress(0.2, desc="Analyzing blog content...")
|
| 86 |
try:
|
| 87 |
response = await asyncio.to_thread(model.generate_content, PROMPT + "\n\nText to analyze:\n" + analysis_text)
|
| 88 |
report = response.text.strip()
|
|
|
|
| 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 issues, policy violations, and more
|
| 209 |
+
3. **Report**: Instantly download a clean, structured Markdown review
|
| 210 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
status_button = gr.Button("π§ Review Blog", elem_classes=["review-btn"])
|
| 212 |
|
| 213 |
gr.Markdown("### π Review Report")
|