Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,10 +7,13 @@ import requests
|
|
7 |
from io import BytesIO
|
8 |
import difflib
|
9 |
|
10 |
-
# Load model
|
11 |
reviewer = pipeline("text2text-generation", model="google/flan-t5-base")
|
12 |
|
13 |
-
#
|
|
|
|
|
|
|
14 |
def extract_text_from_image_url(img_url):
|
15 |
try:
|
16 |
response = requests.get(img_url)
|
@@ -20,7 +23,7 @@ def extract_text_from_image_url(img_url):
|
|
20 |
except Exception as e:
|
21 |
return f"β OCR Error: {e}"
|
22 |
|
23 |
-
#
|
24 |
def extract_text_from_url(url):
|
25 |
downloaded = trafilatura.fetch_url(url)
|
26 |
if downloaded:
|
@@ -28,7 +31,7 @@ def extract_text_from_url(url):
|
|
28 |
else:
|
29 |
return "β Blog Error: Could not fetch content from the URL."
|
30 |
|
31 |
-
# Highlight
|
32 |
def highlight_diffs(orig, suggestion):
|
33 |
diff = difflib.ndiff(orig.split(), suggestion.split())
|
34 |
result = []
|
@@ -41,7 +44,7 @@ def highlight_diffs(orig, suggestion):
|
|
41 |
result.append(word[2:])
|
42 |
return " ".join(result)
|
43 |
|
44 |
-
#
|
45 |
def review_lines(text):
|
46 |
lines = text.strip().split('\n')
|
47 |
reviewed = []
|
@@ -61,7 +64,7 @@ def finalize_text(originals, suggestions, decisions):
|
|
61 |
output.append(sugg if accepted else orig)
|
62 |
return "\n".join(output)
|
63 |
|
64 |
-
# Build
|
65 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
66 |
gr.Markdown("## β¨ BlogChecker AI\nSmart blog reviewer with OCR + AI suggestions")
|
67 |
|
@@ -79,35 +82,37 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
79 |
review_area = gr.Column(visible=False)
|
80 |
final_output = gr.Textbox(label="π¦ Final Clean Blog", lines=10)
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
# Extract combined content
|
85 |
def extract_both(url, img_url):
|
86 |
blog = extract_text_from_url(url)
|
87 |
ocr = extract_text_from_image_url(img_url) if img_url else ""
|
88 |
return blog + ("\n" + ocr if ocr else "")
|
89 |
|
90 |
-
#
|
91 |
def do_review(text):
|
92 |
results = review_lines(text)
|
93 |
review_area.children.clear()
|
94 |
review_boxes.clear()
|
|
|
|
|
95 |
for idx, (orig, highlighted, clean) in enumerate(results):
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
101 |
return gr.update(visible=True)
|
102 |
|
103 |
-
#
|
104 |
def collect_results():
|
105 |
originals = [box[0].value for box in review_boxes]
|
106 |
suggestions = [box[1] for box in review_boxes]
|
107 |
accepts = [box[2].value for box in review_boxes]
|
108 |
return finalize_text(originals, suggestions, accepts)
|
109 |
|
110 |
-
# Wire
|
111 |
extract_btn.click(fn=extract_both, inputs=[blog_url, image_url], outputs=combined_text)
|
112 |
review_btn.click(fn=do_review, inputs=combined_text, outputs=review_area)
|
113 |
finalize_btn.click(fn=collect_results, outputs=final_output)
|
|
|
7 |
from io import BytesIO
|
8 |
import difflib
|
9 |
|
10 |
+
# Load the model
|
11 |
reviewer = pipeline("text2text-generation", model="google/flan-t5-base")
|
12 |
|
13 |
+
# Global storage for review UI
|
14 |
+
review_boxes = []
|
15 |
+
|
16 |
+
# OCR from image
|
17 |
def extract_text_from_image_url(img_url):
|
18 |
try:
|
19 |
response = requests.get(img_url)
|
|
|
23 |
except Exception as e:
|
24 |
return f"β OCR Error: {e}"
|
25 |
|
26 |
+
# Blog content from URL
|
27 |
def extract_text_from_url(url):
|
28 |
downloaded = trafilatura.fetch_url(url)
|
29 |
if downloaded:
|
|
|
31 |
else:
|
32 |
return "β Blog Error: Could not fetch content from the URL."
|
33 |
|
34 |
+
# Highlight word-level differences
|
35 |
def highlight_diffs(orig, suggestion):
|
36 |
diff = difflib.ndiff(orig.split(), suggestion.split())
|
37 |
result = []
|
|
|
44 |
result.append(word[2:])
|
45 |
return " ".join(result)
|
46 |
|
47 |
+
# Process each line
|
48 |
def review_lines(text):
|
49 |
lines = text.strip().split('\n')
|
50 |
reviewed = []
|
|
|
64 |
output.append(sugg if accepted else orig)
|
65 |
return "\n".join(output)
|
66 |
|
67 |
+
# Build UI
|
68 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
69 |
gr.Markdown("## β¨ BlogChecker AI\nSmart blog reviewer with OCR + AI suggestions")
|
70 |
|
|
|
82 |
review_area = gr.Column(visible=False)
|
83 |
final_output = gr.Textbox(label="π¦ Final Clean Blog", lines=10)
|
84 |
|
85 |
+
# Combine blog + OCR
|
|
|
|
|
86 |
def extract_both(url, img_url):
|
87 |
blog = extract_text_from_url(url)
|
88 |
ocr = extract_text_from_image_url(img_url) if img_url else ""
|
89 |
return blog + ("\n" + ocr if ocr else "")
|
90 |
|
91 |
+
# Generate suggestions and show UI
|
92 |
def do_review(text):
|
93 |
results = review_lines(text)
|
94 |
review_area.children.clear()
|
95 |
review_boxes.clear()
|
96 |
+
elems = []
|
97 |
+
|
98 |
for idx, (orig, highlighted, clean) in enumerate(results):
|
99 |
+
orig_box = gr.Textbox(value=orig, label=f"Original Line {idx+1}", interactive=False)
|
100 |
+
markdown_sugg = gr.Markdown(value=highlighted, label=f"Suggested Edit {idx+1}")
|
101 |
+
accept = gr.Checkbox(label="β
Accept Suggestion", value=False)
|
102 |
+
elems.extend([orig_box, markdown_sugg, accept])
|
103 |
+
review_boxes.append((orig_box, clean, accept))
|
104 |
+
|
105 |
+
review_area.children = elems
|
106 |
return gr.update(visible=True)
|
107 |
|
108 |
+
# Collect accepted decisions
|
109 |
def collect_results():
|
110 |
originals = [box[0].value for box in review_boxes]
|
111 |
suggestions = [box[1] for box in review_boxes]
|
112 |
accepts = [box[2].value for box in review_boxes]
|
113 |
return finalize_text(originals, suggestions, accepts)
|
114 |
|
115 |
+
# Wire events
|
116 |
extract_btn.click(fn=extract_both, inputs=[blog_url, image_url], outputs=combined_text)
|
117 |
review_btn.click(fn=do_review, inputs=combined_text, outputs=review_area)
|
118 |
finalize_btn.click(fn=collect_results, outputs=final_output)
|