Spaces:
Sleeping
Sleeping
File size: 6,012 Bytes
6af176b c873d13 b2d2b3c 5c898fe b2d2b3c 6af176b b2d2b3c 6af176b b2d2b3c 5ef657b b2d2b3c c873d13 b2d2b3c c873d13 b2d2b3c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import gradio as gr
import requests
from PIL import Image
import pytesseract
import difflib
from io import BytesIO
from transformers import pipeline
import trafilatura
from nltk.tokenize import sent_tokenize
import nltk
nltk.download("punkt")
# === Load AI model ===
reviewer = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-beta", max_new_tokens=200)
device = "cpu"
print(f"Device set to use {device}")
# === Utility: Highlight diffs ===
def highlight_diff(original, suggestion):
diff = difflib.ndiff(original.split(), suggestion.split())
result = ""
for word in diff:
if word.startswith("- "):
result += f"<span style='color:red;text-decoration:line-through'>{word[2:]}</span> "
elif word.startswith("+ "):
result += f"<span style='color:green;font-weight:bold'>{word[2:]}</span> "
elif word.startswith(" "):
result += word[2:] + " "
return result.strip()
# === Extract blog content from URL ===
def extract_text_from_url(url):
try:
headers = {"User-Agent": "Mozilla/5.0"}
response = requests.get(url, headers=headers, timeout=10)
if response.status_code == 200:
return trafilatura.extract(response.text)
else:
return f"β Blog Error: HTTP {response.status_code} on URL {url}"
except Exception as e:
return f"β Blog Error: {e}"
# === Extract text from image URL (OCR) ===
def extract_text_from_image(image_url):
try:
img_data = requests.get(image_url).content
image = Image.open(BytesIO(img_data)).convert("L")
text = pytesseract.image_to_string(image)
return text if text.strip() else "β OCR Error: No readable text found."
except Exception as e:
return f"β OCR Error: {e}"
# === Suggestion generator ===
def generate_suggestions(text):
sentences = sent_tokenize(text)
suggestions = []
for sent in sentences:
prompt = f"Improve the tone, grammar, clarity and flag any sensitive content:\n\n{sent}"
output = reviewer(prompt, max_new_tokens=200)[0]["generated_text"]
cleaned = output.replace(prompt, "").strip()
suggestions.append(cleaned if cleaned else sent)
return sentences, suggestions
# === Final approval handler ===
def collect_decisions(originals, suggestions, *choices):
results = []
for orig, sugg, choice in zip(originals, suggestions, choices):
results.append(sugg if choice == "Accept" else orig)
return "\n".join(results)
# === Gradio UI ===
with gr.Blocks() as demo:
gr.Markdown("# β¨ Blog Reviewer AI")
gr.Markdown("Detect tone issues, errors, and sensitive content β and clean them interactively!")
with gr.Tab("π From Blog URL"):
blog_url = gr.Textbox(label="Enter blog URL")
fetch_btn = gr.Button("Fetch & Review")
with gr.Tab("πΌοΈ From Image URL (OCR)"):
image_url = gr.Textbox(label="Enter Image URL")
image_btn = gr.Button("Extract & Review")
with gr.Tab("π Paste Text"):
pasted_text = gr.Textbox(label="Paste blog content here", lines=10)
paste_btn = gr.Button("Review Text")
output_section = gr.Column(visible=False)
originals = gr.State([])
suggestions = gr.State([])
decision_radios = []
view_mode = gr.Radio(["Original", "Suggestion", "Side-by-Side"], value="Side-by-Side", label="Choose View")
final_output = gr.Textbox(label="β
Final Output", lines=12)
finalize_btn = gr.Button("Generate Clean Version")
sentence_blocks = []
# === Show suggestions UI ===
def show_review(text):
origs, suggs = generate_suggestions(text)
originals.value = origs
suggestions.value = suggs
return origs, suggs, True
# === Populate sentence review rows dynamically ===
def populate_review_ui(origs, suggs):
global decision_radios, sentence_blocks
decision_radios = []
sentence_blocks = []
ui_blocks = []
for i, (orig, sugg) in enumerate(zip(origs, suggs)):
orig_md = gr.Markdown(f"<b>{orig}</b>", visible=False)
sugg_md = gr.Markdown(f"<b>{sugg}</b>", visible=False)
diff_md = gr.Markdown(highlight_diff(orig, sugg), visible=True)
radio = gr.Radio(["Accept", "Reject"], value="Accept", label=f"Suggestion {i+1}")
decision_radios.append(radio)
sentence_blocks.append((orig_md, sugg_md, diff_md))
ui_blocks.extend([orig_md, sugg_md, diff_md, radio])
return ui_blocks
# === Toggle view mode ===
def toggle_view(view):
updates = []
for orig_md, sugg_md, diff_md in sentence_blocks:
if view == "Original":
updates.extend([gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)])
elif view == "Suggestion":
updates.extend([gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)])
else: # Side-by-side
updates.extend([gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)])
return updates
# === Final output handler ===
def finalize_output(origs, suggs, *choices):
return collect_decisions(origs, suggs, *choices)
# Button click handlers
fetch_btn.click(fn=extract_text_from_url, inputs=blog_url, outputs=pasted_text)
image_btn.click(fn=extract_text_from_image, inputs=image_url, outputs=pasted_text)
paste_btn.click(fn=show_review, inputs=pasted_text, outputs=[originals, suggestions, output_section])
# Dynamic render trigger
originals.change(fn=populate_review_ui, inputs=[originals, suggestions], outputs=[])
view_mode.change(fn=toggle_view, inputs=view_mode,
outputs=[item for block in sentence_blocks for item in block])
finalize_btn.click(fn=finalize_output, inputs=[originals, suggestions] + decision_radios, outputs=final_output)
demo.launch()
|