JaishnaCodz commited on
Commit
5c898fe
Β·
verified Β·
1 Parent(s): 2324254

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -5,6 +5,7 @@ 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")
@@ -27,22 +28,33 @@ def extract_text_from_url(url):
27
  else:
28
  return "❌ Blog Error: Could not fetch content from the URL."
29
 
30
- # Review line-by-line
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  def review_lines(text):
32
  lines = text.strip().split('\n')
33
  suggestions = []
34
  for line in lines:
35
  if line.strip() == "":
36
  continue
37
- prompt = (
38
- f"Review this line for grammar, tone, and offensive language. "
39
- f"Show suggested corrections with changes **highlighted** in Markdown (e.g., ~~wrong~~ β†’ **right**):\n\n{line}"
40
- )
41
- suggestion = reviewer(prompt, max_new_tokens=100)[0]['generated_text']
42
- suggestions.append((line, suggestion.strip()))
43
  return suggestions
44
 
45
- # Finalize text
46
  def finalize_text(originals, suggestions, decisions):
47
  final = []
48
  for orig, sugg, keep in zip(originals, suggestions, decisions):
@@ -65,33 +77,36 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as app:
65
  finalize_btn = gr.Button("βœ… Finalize Clean Blog")
66
 
67
  review_section = gr.Column(visible=False)
68
- review_boxes = [] # Will store tuples: (orig_box, sugg_box, accept_checkbox)
69
 
70
  final_output = gr.Textbox(label="πŸ“¦ Final Clean Blog", lines=10)
71
 
 
72
  def extract_both(url, img_url):
73
  blog = extract_text_from_url(url)
74
  ocr = extract_text_from_image_url(img_url) if img_url else ""
75
  return blog + ("\n" + ocr if ocr else "")
76
 
 
77
  def process_review(text):
78
  results = review_lines(text)
79
  review_section.children.clear()
80
  review_boxes.clear()
81
 
82
- for i, (orig, sugg) in enumerate(results):
83
  with review_section:
84
  orig_box = gr.Textbox(value=orig, label=f"Original Line {i+1}", interactive=False)
85
- sugg_box = gr.Markdown(value=sugg, label=f"Suggested Edit {i+1}")
86
  accept_box = gr.Checkbox(label="βœ… Accept Suggestion", value=False)
87
- review_boxes.append((orig_box, sugg_box, accept_box))
88
  return gr.update(visible=True)
89
 
 
90
  def collect_dynamic_decisions():
91
  originals_vals = [box[0].value for box in review_boxes]
92
- suggestions_vals = [box[1].value for box in review_boxes]
93
  accepts_vals = [box[2].value for box in review_boxes]
94
- return finalize_text(originals_vals, suggestions_vals, accepts_vals)
95
 
96
  # Wire actions
97
  extract_btn.click(fn=extract_both, inputs=[blog_url, image_url], outputs=combined_text)
 
5
  from PIL import Image
6
  import requests
7
  from io import BytesIO
8
+ import difflib
9
 
10
  # Load model
11
  reviewer = pipeline("text2text-generation", model="google/flan-t5-base")
 
28
  else:
29
  return "❌ Blog Error: Could not fetch content from the URL."
30
 
31
+ # Highlight differences using difflib
32
+ def highlight_diffs(original, suggestion):
33
+ diff = difflib.ndiff(original.split(), suggestion.split())
34
+ result = []
35
+ for token in diff:
36
+ if token.startswith("- "):
37
+ result.append(f"~~{token[2:]}~~")
38
+ elif token.startswith("+ "):
39
+ result.append(f"**{token[2:]}**")
40
+ elif token.startswith(" "):
41
+ result.append(token[2:])
42
+ return " ".join(result)
43
+
44
+ # Review lines with diffs
45
  def review_lines(text):
46
  lines = text.strip().split('\n')
47
  suggestions = []
48
  for line in lines:
49
  if line.strip() == "":
50
  continue
51
+ prompt = f"Rewrite this to fix grammar, tone, and remove any offensive language:\n\n{line}"
52
+ suggestion = reviewer(prompt, max_new_tokens=100)[0]['generated_text'].strip()
53
+ highlighted = highlight_diffs(line.strip(), suggestion)
54
+ suggestions.append((line, highlighted, suggestion))
 
 
55
  return suggestions
56
 
57
+ # Finalize accepted suggestions
58
  def finalize_text(originals, suggestions, decisions):
59
  final = []
60
  for orig, sugg, keep in zip(originals, suggestions, decisions):
 
77
  finalize_btn = gr.Button("βœ… Finalize Clean Blog")
78
 
79
  review_section = gr.Column(visible=False)
80
+ review_boxes = [] # Will store tuples: (original_box, highlighted_markdown_box, accept_checkbox, clean_suggestion)
81
 
82
  final_output = gr.Textbox(label="πŸ“¦ Final Clean Blog", lines=10)
83
 
84
+ # Text extraction logic
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
+ # Review processing with diffs
91
  def process_review(text):
92
  results = review_lines(text)
93
  review_section.children.clear()
94
  review_boxes.clear()
95
 
96
+ for i, (orig, highlighted, clean_sugg) in enumerate(results):
97
  with review_section:
98
  orig_box = gr.Textbox(value=orig, label=f"Original Line {i+1}", interactive=False)
99
+ markdown_sugg = gr.Markdown(value=highlighted, label=f"Suggested Edit {i+1}")
100
  accept_box = gr.Checkbox(label="βœ… Accept Suggestion", value=False)
101
+ review_boxes.append((orig_box, markdown_sugg, accept_box, clean_sugg))
102
  return gr.update(visible=True)
103
 
104
+ # Finalization logic
105
  def collect_dynamic_decisions():
106
  originals_vals = [box[0].value for box in review_boxes]
107
+ clean_suggestions = [box[3] for box in review_boxes]
108
  accepts_vals = [box[2].value for box in review_boxes]
109
+ return finalize_text(originals_vals, clean_suggestions, accepts_vals)
110
 
111
  # Wire actions
112
  extract_btn.click(fn=extract_both, inputs=[blog_url, image_url], outputs=combined_text)