JaishnaCodz commited on
Commit
c873d13
Β·
verified Β·
1 Parent(s): e92aac5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -30
app.py CHANGED
@@ -1,11 +1,25 @@
1
  import gradio as gr
2
  from newspaper import Article
3
  from transformers import pipeline
 
 
 
 
4
 
5
- # Load small LLM
6
  reviewer = pipeline("text2text-generation", model="google/flan-t5-base")
7
 
8
- # Step 1: Extract blog content
 
 
 
 
 
 
 
 
 
 
9
  def extract_text_from_url(url):
10
  try:
11
  article = Article(url)
@@ -13,37 +27,80 @@ def extract_text_from_url(url):
13
  article.parse()
14
  return article.text
15
  except Exception as e:
16
- return f"Error: {e}"
17
-
18
- # Step 2: Review content
19
- def review_blog(text):
20
- prompt = f"""Please review the following blog content. Fix grammar, crude/offensive language, or policy-violating parts. Suggest replacements:
21
 
22
- {text}"""
23
- result = reviewer(prompt, max_new_tokens=300)[0]['generated_text']
24
- return result
 
 
 
 
 
 
 
 
25
 
26
- # Step 3: Finalize after approval
27
- def finalize_text(original_text, reviewed_suggestions):
28
- # For simplicity, we'll just return reviewed version for now
29
- # Later you can implement side-by-side diff + selective replacement
30
- return reviewed_suggestions
 
31
 
32
  # Gradio UI
33
- with gr.Blocks() as app:
34
- gr.Markdown("## 🧠 BlogChecker AI")
35
-
36
- url = gr.Textbox(label="Enter Blog URL")
37
- blog_content = gr.Textbox(label="Extracted Blog Text", lines=10)
38
- reviewed = gr.Textbox(label="Suggested Revisions", lines=10)
39
- final_output = gr.Textbox(label="Final Clean Blog", lines=10)
40
-
41
- extract_btn = gr.Button("1️⃣ Extract Blog Text")
42
- review_btn = gr.Button("2️⃣ Review Content")
43
- approve_btn = gr.Button("3️⃣ Approve and Finalize")
44
-
45
- extract_btn.click(fn=extract_text_from_url, inputs=url, outputs=blog_content)
46
- review_btn.click(fn=review_blog, inputs=blog_content, outputs=reviewed)
47
- approve_btn.click(fn=finalize_text, inputs=[blog_content, reviewed], outputs=final_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  app.launch()
 
1
  import gradio as gr
2
  from newspaper import Article
3
  from transformers import pipeline
4
+ import pytesseract
5
+ from PIL import Image
6
+ import requests
7
+ from io import BytesIO
8
 
9
+ # Load model
10
  reviewer = pipeline("text2text-generation", model="google/flan-t5-base")
11
 
12
+ # OCR
13
+ def extract_text_from_image_url(img_url):
14
+ try:
15
+ response = requests.get(img_url)
16
+ img = Image.open(BytesIO(response.content))
17
+ text = pytesseract.image_to_string(img)
18
+ return text
19
+ except Exception as e:
20
+ return f"❌ OCR Error: {e}"
21
+
22
+ # Extract blog
23
  def extract_text_from_url(url):
24
  try:
25
  article = Article(url)
 
27
  article.parse()
28
  return article.text
29
  except Exception as e:
30
+ return f"❌ Blog Error: {e}"
 
 
 
 
31
 
32
+ # Review line-by-line
33
+ def review_lines(text):
34
+ lines = text.strip().split('\n')
35
+ suggestions = []
36
+ for line in lines:
37
+ if line.strip() == "":
38
+ continue
39
+ prompt = f"Rewrite this to fix grammar, tone, and remove any offensive language:\n\n{line}"
40
+ suggestion = reviewer(prompt, max_new_tokens=100)[0]['generated_text']
41
+ suggestions.append((line, suggestion.strip()))
42
+ return suggestions
43
 
44
+ # Finalize by combining accepted lines
45
+ def finalize_text(originals, suggestions, decisions):
46
+ final = []
47
+ for orig, sugg, keep in zip(originals, suggestions, decisions):
48
+ final.append(sugg if keep else orig)
49
+ return "\n".join(final)
50
 
51
  # Gradio UI
52
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as app:
53
+ gr.Markdown("## ✨ BlogChecker AI\nSmart AI reviewer for blog content, with interactive approval and OCR image support.")
54
+
55
+ with gr.Row():
56
+ blog_url = gr.Textbox(label="πŸ“Ž Blog URL")
57
+ image_url = gr.Textbox(label="πŸ–ΌοΈ Image URL (optional)")
58
+ extract_btn = gr.Button("πŸ” Extract")
59
+
60
+ combined_text = gr.Textbox(label="πŸ“ Combined Blog + OCR Text", lines=10)
61
+
62
+ with gr.Row():
63
+ review_btn = gr.Button("🧠 Review Content")
64
+ finalize_btn = gr.Button("βœ… Finalize Clean Blog")
65
+
66
+ line_outputs = []
67
+ decisions = []
68
+ originals = []
69
+ suggestions = []
70
+
71
+ with gr.Column() as dynamic_review_section:
72
+ for i in range(5): # support 5 lines for demo; can be dynamic later
73
+ orig = gr.Textbox(label=f"Original Line {i+1}", interactive=False)
74
+ sugg = gr.Textbox(label=f"Suggested Line {i+1}", interactive=False)
75
+ accept = gr.Checkbox(label="βœ… Accept Suggestion")
76
+ originals.append(orig)
77
+ suggestions.append(sugg)
78
+ decisions.append(accept)
79
+ line_outputs.append((orig, sugg, accept))
80
+
81
+ final_output = gr.Textbox(label="πŸ“¦ Final Clean Blog", lines=10)
82
+
83
+ def extract_both(url, img_url):
84
+ blog = extract_text_from_url(url)
85
+ ocr = extract_text_from_image_url(img_url) if img_url else ""
86
+ return blog + ("\n" + ocr if ocr else "")
87
+
88
+ def process_review(text):
89
+ results = review_lines(text)
90
+ outputs = []
91
+ for i in range(len(line_outputs)):
92
+ if i < len(results):
93
+ outputs.extend([results[i][0], results[i][1], False]) # orig, suggestion, unselected
94
+ else:
95
+ outputs.extend(["", "", False]) # clear unused slots
96
+ return outputs
97
+
98
+ def collect_decisions(origs, suggs, accepts):
99
+ return finalize_text(origs, suggs, accepts)
100
+
101
+ # Wire actions
102
+ extract_btn.click(fn=extract_both, inputs=[blog_url, image_url], outputs=combined_text)
103
+ review_btn.click(fn=process_review, inputs=combined_text, outputs=[el for group in line_outputs for el in group])
104
+ finalize_btn.click(fn=collect_decisions, inputs=[originals, suggestions, decisions], outputs=final_output)
105
 
106
  app.launch()