|
from typing import List, Tuple |
|
import logging |
|
|
|
import gradio as gr |
|
|
|
try: |
|
from .services import identify_vague_statements |
|
except ImportError: |
|
from services import identify_vague_statements |
|
|
|
|
|
def create_editable_section(label, js_script, submit_fn, textbox): |
|
with gr.Accordion(f"Edit {label}", open=False) as acc: |
|
output_text = gr.HTML(label="Highlighted Text", elem_id=f"highlighted_textarea_{label}", value=textbox) |
|
output_text_suggestion = gr.Textbox(label="Suggestions", lines=5, interactive=True) |
|
with gr.Row(): |
|
submit_button = gr.Button("Run Editorial AI", size="small") |
|
accept_button = gr.Button("Apply Changes", size="small") |
|
|
|
edit_pairs = gr.State([]) |
|
|
|
def submit_callback(default_text): |
|
result = submit_fn(default_text) |
|
pairs = result["pairs"] |
|
edit_pairs.value = pairs |
|
return result["html"], result["suggestions"] |
|
|
|
def update_change(changes, original): |
|
for old_text, new_text in changes: |
|
modified = original.replace(old_text, new_text) |
|
return modified |
|
|
|
|
|
submit_button.click( |
|
|
|
fn=submit_callback, |
|
inputs=textbox, |
|
outputs=[output_text, output_text_suggestion] |
|
) |
|
|
|
|
|
accept_button.click( |
|
fn=update_change, |
|
inputs=[edit_pairs, textbox], |
|
outputs=textbox |
|
) |
|
|
|
|
|
gr.HTML(f"<script>{js_script}</script>") |
|
return acc |
|
|
|
|
|
def highlight_text(input_section: str) -> Tuple[str, List[str], List[str], List[str]]: |
|
try: |
|
|
|
input_section = input_section.replace("&", "&").replace("<", "<").replace(">", ">") |
|
vague_statements, alternative_texts, reasons = identify_vague_statements(input_section) |
|
|
|
highlighted_text = input_section |
|
for vague_statement in vague_statements: |
|
vague_statement = vague_statement.replace("&", "&").replace("<", "<").replace(">", ">") |
|
highlighted_text = highlighted_text.replace(vague_statement, f'<mark>{vague_statement}</mark>') |
|
|
|
logging.info("Success: Highlight text") |
|
return highlighted_text, vague_statements, alternative_texts, reasons |
|
|
|
except Exception as e: |
|
logging.error("Error: %s", e) |
|
|
|
raise gr.Error("Error in Extracting vague statements..") |
|
|
|
|
|
def update_highlighted_text(default_text: str): |
|
gr.Info("Running Editorial AI..") |
|
|
|
|
|
highlighted_text, vague_statements, alternative_texts, reasons = highlight_text(default_text) |
|
suggestions = "" |
|
pairs = [] |
|
|
|
for vague, alternative, reason in zip(vague_statements, alternative_texts, reasons): |
|
pairs.append((vague, alternative)) |
|
suggestions += f"Vague Statement: {vague}\nReason: {reason}\nAlternative Text: {alternative}\n\n" |
|
|
|
html = f""" |
|
<div id="highlighted_textarea" class="custom-textarea" contenteditable="true"> |
|
{highlighted_text} |
|
</div> |
|
<script> |
|
window.highlightTexts = [{', '.join([f'`{sentence.replace("&", "&").replace("<", "<").replace(">", ">")}`' for sentence in vague_statements])}]; |
|
</script> |
|
""" |
|
|
|
result = { |
|
"html": html, |
|
"suggestions": suggestions, |
|
"pairs": pairs |
|
} |
|
|
|
logging.info("Highlighted the text successfully..") |
|
return result |
|
|