Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
import spacy | |
import subprocess | |
import nltk | |
from nltk.corpus import wordnet | |
from spellchecker import SpellChecker | |
# Initialize other components (AI detection, NLP, etc.) as before... | |
# Function to paraphrase and correct grammar using Ginger | |
def correct_with_ginger(text): | |
ginger_result = get_ginger_result(text) | |
if "error" in ginger_result: | |
return ginger_result["error"] | |
original_text = text | |
fixed_text = original_text | |
color_gap, fixed_gap = 0, 0 | |
if not ginger_result["LightGingerTheTextResult"]: | |
return "No grammatical issues found!" | |
for result in ginger_result["LightGingerTheTextResult"]: | |
if result["Suggestions"]: | |
from_index = result["From"] + color_gap | |
to_index = result["To"] + 1 + color_gap | |
suggest = result["Suggestions"][0]["Text"] | |
original_text = original_text[:from_index] + original_text[from_index:to_index] + original_text[to_index:] | |
fixed_text = fixed_text[:from_index-fixed_gap] + suggest + fixed_text[to_index-fixed_gap:] | |
color_gap += len(suggest) - (to_index - from_index) | |
fixed_gap += to_index - from_index - len(suggest) | |
return fixed_text | |
# Gradio app setup with two tabs | |
with gr.Blocks() as demo: | |
with gr.Tab("AI Detection"): | |
t1 = gr.Textbox(lines=5, label='Text') | |
button1 = gr.Button("π€ Predict!") | |
label1 = gr.Textbox(lines=1, label='Predicted Label π') | |
score1 = gr.Textbox(lines=1, label='Prob') | |
button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1]) | |
with gr.Tab("Paraphrasing & Grammar Correction"): | |
t2 = gr.Textbox(lines=5, label='Enter text for paraphrasing and grammar correction') | |
button2 = gr.Button("π Paraphrase and Correct") | |
ginger_button = gr.Button("π§ Correct with Ginger") | |
result2 = gr.Textbox(lines=5, label='Corrected Text') | |
button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2) | |
ginger_button.click(fn=correct_with_ginger, inputs=t2, outputs=result2) | |
demo.launch(share=True) | |