Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
6 |
reviewer = pipeline("text2text-generation", model="google/flan-t5-base")
|
7 |
|
8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
#
|
27 |
-
def finalize_text(
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
31 |
|
32 |
# Gradio UI
|
33 |
-
with gr.Blocks() as app:
|
34 |
-
gr.Markdown("##
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|