File size: 2,158 Bytes
04919b2
 
 
c5d2e49
04919b2
 
 
 
aed78e3
04919b2
aed78e3
 
 
 
 
c5d2e49
aed78e3
 
 
 
 
c5d2e49
aed78e3
 
 
 
 
 
 
 
 
 
 
 
c5d2e49
 
 
 
 
 
 
 
 
 
 
 
 
 
aed78e3
c5d2e49
 
 
aed78e3
a2b6ad0
c5d2e49
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
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)