JaishnaCodz commited on
Commit
eacf582
Β·
verified Β·
1 Parent(s): 3dcaa0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -28
app.py CHANGED
@@ -9,56 +9,55 @@ import tempfile
9
  import asyncio
10
  import time
11
 
12
- # Download NLTK tokenizer data
13
  download('punkt')
14
 
15
- # Set Gemini API key
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 (e.g., copyright infringement, defamation, incitement to violence).
35
  3. Crude/Abusive Language: Detect crude, offensive, or abusive language.
36
- 4. Sensitive Topics: Identify content related to sensitive topics such as racism, gender bias, or other forms of discrimination.
37
 
38
- Return the results in the following markdown format:
39
  # Blog Review Report
40
 
41
  ## Grammar Corrections
42
  1. [Heading of issue]
43
- - CONTENT: [Exact line or part of text with the issue]
44
- - SUGGESTION: [Suggested correction]
45
- - ISSUE: [Description of the issue]
46
-
47
- [Continue numbering for additional issues or state "None detected"]
48
 
49
  ## Legal Policy Violations
50
- - CONTENT: [Exact line or part of text with the issue]
51
- SUGGESTION: [Suggested action or correction]
52
- ISSUE: [Description of the legal violation]
53
- [Or state "None detected"]
54
 
55
  ## Crude/Abusive Language
56
- - [List instances of crude or abusive language or "None detected"]
57
 
58
  ## Sensitive Topics
59
- - [List instances of sensitive topics or "None detected"]
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.HTML("""
206
- <div style="background: #fff; padding: 20px 25px; border-radius: 15px; box-shadow: 0 4px 14px rgba(0,0,0,0.08);">
207
- <h3 style="color:#2b6cb0;">πŸš€ How It Works</h3>
208
- <ul style="font-size: 15px; color: #2d3748; line-height: 1.7;">
209
- <li>Paste blog text or a URL</li>
210
- <li>Gemini analyzes for quality and issues</li>
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")