JaishnaCodz commited on
Commit
0425ea5
·
verified ·
1 Parent(s): 56f7cbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -16
app.py CHANGED
@@ -9,10 +9,10 @@ from nltk.tokenize import sent_tokenize
9
  # Download punkt for sentence tokenization
10
  nltk.download("punkt")
11
 
12
- # Load the grammar tool
13
- grammar_tool = language_tool_python.LanguageToolPublicAPI('en-US')
14
 
15
- # Load models
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
 
@@ -25,16 +25,20 @@ def extract_text(input_type, text_input, url_input):
25
  return text_input
26
 
27
  def check_grammar(text):
28
- matches = grammar_tool.check(text)
29
- return [
30
- {
31
- "text": match.context,
32
- "error": match.message,
33
- "suggestions": match.replacements,
34
- "offset": match.offset,
35
- "length": match.errorLength
36
- } for match in matches
37
- ]
 
 
 
 
38
 
39
  def detect_sensitive_content(text):
40
  sentences = sent_tokenize(text)
@@ -72,7 +76,6 @@ def highlight_text(text, grammar_issues, sensitive_issues):
72
  highlighted = highlighted[:start] + span + highlighted[end:]
73
  offset_adjust += len(span) - len(error_text)
74
 
75
- sentences = sent_tokenize(text)
76
  for issue in sensitive_issues:
77
  sentence = issue['sentence']
78
  highlighted = highlighted.replace(sentence, f"<span style='background-color: red'>{sentence}</span>")
@@ -116,9 +119,10 @@ def apply_changes(text, suggestions, approved_indices):
116
  continue
117
  return text
118
 
 
119
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
120
- gr.Markdown("## 🧠 AI Blog Reviewer")
121
- gr.Markdown("Analyze blog text or URL for grammar issues and sensitive content (bias, toxicity, etc.).")
122
 
123
  input_type = gr.Radio(["Text", "URL"], label="Input Type", value="Text")
124
  text_input = gr.Textbox(label="Blog Text", lines=10, visible=True)
 
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
 
 
25
  return text_input
26
 
27
  def check_grammar(text):
28
+ try:
29
+ matches = grammar_tool.check(text)
30
+ return [
31
+ {
32
+ "text": match.context,
33
+ "error": match.message,
34
+ "suggestions": match.replacements,
35
+ "offset": match.offset,
36
+ "length": match.errorLength
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)
 
76
  highlighted = highlighted[:start] + span + highlighted[end:]
77
  offset_adjust += len(span) - len(error_text)
78
 
 
79
  for issue in sensitive_issues:
80
  sentence = issue['sentence']
81
  highlighted = highlighted.replace(sentence, f"<span style='background-color: red'>{sentence}</span>")
 
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.")
126
 
127
  input_type = gr.Radio(["Text", "URL"], label="Input Type", value="Text")
128
  text_input = gr.Textbox(label="Blog Text", lines=10, visible=True)