JaishnaCodz commited on
Commit
2867d5b
·
verified ·
1 Parent(s): cb66dfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -6,22 +6,25 @@ import nltk
6
  import re
7
  from nltk.tokenize import sent_tokenize
8
 
9
- # Download punkt for sentence tokenization
10
  nltk.download("punkt")
11
 
12
- # Use local LanguageTool server (runs via setup.sh)
13
- grammar_tool = language_tool_python.LanguageTool('en-US')
14
 
15
- # Load summarizer and hate-speech detection model
16
  summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-6-6")
17
  toxicity_classifier = pipeline("text-classification", model="Hate-speech-CNERG/bert-base-uncased-hatexplain")
18
 
19
  def extract_text(input_type, text_input, url_input):
20
  if input_type == "URL" and url_input:
21
- article = Article(url_input)
22
- article.download()
23
- article.parse()
24
- return article.text
 
 
 
25
  return text_input
26
 
27
  def check_grammar(text):
@@ -37,8 +40,7 @@ def check_grammar(text):
37
  } for match in matches
38
  ]
39
  except Exception as e:
40
- print(f"[Grammar Check Error] {e}")
41
- return [{"text": "", "error": "Grammar check skipped (server error)", "suggestions": [], "offset": 0, "length": 0}]
42
 
43
  def detect_sensitive_content(text):
44
  sentences = sent_tokenize(text)
@@ -87,6 +89,9 @@ def review_blog(input_type, text_input, url_input):
87
  return "Please provide text or a URL.", "", []
88
 
89
  text = extract_text(input_type, text_input, url_input)
 
 
 
90
  grammar_issues = check_grammar(text)
91
  sensitive_issues = detect_sensitive_content(text)
92
  suggestions = generate_suggestions(text, grammar_issues, sensitive_issues)
@@ -119,7 +124,7 @@ def apply_changes(text, suggestions, approved_indices):
119
  continue
120
  return text
121
 
122
- # Gradio Interface
123
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
124
  gr.Markdown("## 🧠 AI Blog Reviewer with Grammar & Bias Detection")
125
  gr.Markdown("Enter blog content or a URL. Detect grammar issues and sensitive (toxic, biased) content.")
 
6
  import re
7
  from nltk.tokenize import sent_tokenize
8
 
9
+ # Download punkt tokenizer
10
  nltk.download("punkt")
11
 
12
+ # Connect to local LanguageTool server (started via setup.sh)
13
+ grammar_tool = language_tool_python.LanguageTool('en-US', host='localhost', port=8081)
14
 
15
+ # Load transformers pipelines
16
  summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-6-6")
17
  toxicity_classifier = pipeline("text-classification", model="Hate-speech-CNERG/bert-base-uncased-hatexplain")
18
 
19
  def extract_text(input_type, text_input, url_input):
20
  if input_type == "URL" and url_input:
21
+ try:
22
+ article = Article(url_input)
23
+ article.download()
24
+ article.parse()
25
+ return article.text
26
+ except Exception as e:
27
+ return f"Error fetching URL: {e}"
28
  return text_input
29
 
30
  def check_grammar(text):
 
40
  } for match in matches
41
  ]
42
  except Exception as e:
43
+ return [{"text": "", "error": f"Grammar check failed: {str(e)}", "suggestions": [], "offset": 0, "length": 0}]
 
44
 
45
  def detect_sensitive_content(text):
46
  sentences = sent_tokenize(text)
 
89
  return "Please provide text or a URL.", "", []
90
 
91
  text = extract_text(input_type, text_input, url_input)
92
+ if text.startswith("Error"):
93
+ return text, "", []
94
+
95
  grammar_issues = check_grammar(text)
96
  sensitive_issues = detect_sensitive_content(text)
97
  suggestions = generate_suggestions(text, grammar_issues, sensitive_issues)
 
124
  continue
125
  return text
126
 
127
+ # Gradio UI
128
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
129
  gr.Markdown("## 🧠 AI Blog Reviewer with Grammar & Bias Detection")
130
  gr.Markdown("Enter blog content or a URL. Detect grammar issues and sensitive (toxic, biased) content.")