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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -35
app.py CHANGED
@@ -27,7 +27,6 @@ def extract_text_from_url(url):
27
  else:
28
  return "❌ Blog Error: Could not fetch content from the URL."
29
 
30
-
31
  # Review line-by-line
32
  def review_lines(text):
33
  lines = text.strip().split('\n')
@@ -35,19 +34,22 @@ def review_lines(text):
35
  for line in lines:
36
  if line.strip() == "":
37
  continue
38
- prompt = f"Rewrite this to fix grammar, tone, and remove any offensive language:\n\n{line}"
 
 
 
39
  suggestion = reviewer(prompt, max_new_tokens=100)[0]['generated_text']
40
  suggestions.append((line, suggestion.strip()))
41
  return suggestions
42
 
43
- # Finalize by combining accepted lines
44
  def finalize_text(originals, suggestions, decisions):
45
  final = []
46
  for orig, sugg, keep in zip(originals, suggestions, decisions):
47
  final.append(sugg if keep else orig)
48
  return "\n".join(final)
49
 
50
- # Gradio UI
51
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as app:
52
  gr.Markdown("## ✨ BlogChecker AI\nSmart AI reviewer for blog content, with interactive approval and OCR image support.")
53
 
@@ -62,20 +64,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as app:
62
  review_btn = gr.Button("🧠 Review Content")
63
  finalize_btn = gr.Button("βœ… Finalize Clean Blog")
64
 
65
- line_outputs = []
66
- decisions = []
67
- originals = []
68
- suggestions = []
69
-
70
- with gr.Column() as dynamic_review_section:
71
- for i in range(5): # support 5 lines for demo; can be dynamic later
72
- orig = gr.Textbox(label=f"Original Line {i+1}", interactive=False)
73
- sugg = gr.Textbox(label=f"Suggested Line {i+1}", interactive=False)
74
- accept = gr.Checkbox(label="βœ… Accept Suggestion")
75
- originals.append(orig)
76
- suggestions.append(sugg)
77
- decisions.append(accept)
78
- line_outputs.append((orig, sugg, accept))
79
 
80
  final_output = gr.Textbox(label="πŸ“¦ Final Clean Blog", lines=10)
81
 
@@ -86,25 +76,26 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as app:
86
 
87
  def process_review(text):
88
  results = review_lines(text)
89
- outputs = []
90
- for i in range(len(line_outputs)):
91
- if i < len(results):
92
- outputs.extend([results[i][0], results[i][1], False]) # orig, suggestion, unselected
93
- else:
94
- outputs.extend(["", "", False]) # clear unused slots
95
- return outputs
96
-
97
- def collect_decisions(*args):
98
- count = len(args) // 3
99
- origs = args[:count]
100
- suggs = args[count:2*count]
101
- accepts = args[2*count:]
102
- return finalize_text(origs, suggs, accepts)
103
-
 
104
 
105
  # Wire actions
106
  extract_btn.click(fn=extract_both, inputs=[blog_url, image_url], outputs=combined_text)
107
- review_btn.click(fn=process_review, inputs=combined_text, outputs=[el for group in line_outputs for el in group])
108
- finalize_btn.click(fn=collect_decisions, inputs=originals + suggestions + decisions, outputs=final_output)
109
 
110
  app.launch()
 
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')
 
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):
49
  final.append(sugg if keep else orig)
50
  return "\n".join(final)
51
 
52
+ # Gradio app
53
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as app:
54
  gr.Markdown("## ✨ BlogChecker AI\nSmart AI reviewer for blog content, with interactive approval and OCR image support.")
55
 
 
64
  review_btn = gr.Button("🧠 Review Content")
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
 
 
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)
98
+ review_btn.click(fn=process_review, inputs=combined_text, outputs=review_section)
99
+ finalize_btn.click(fn=collect_dynamic_decisions, outputs=final_output)
100
 
101
  app.launch()