Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
from transformers import
|
4 |
-
import torch
|
5 |
import spacy
|
6 |
import subprocess
|
7 |
import nltk
|
@@ -22,31 +21,20 @@ except OSError:
|
|
22 |
# Load a smaller Word2Vec model from Gensim's pre-trained models
|
23 |
word_vectors = api.load("glove-wiki-gigaword-50")
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
|
28 |
-
#
|
29 |
-
tokenizer_ai = AutoTokenizer.from_pretrained("roberta-base-openai-detector")
|
30 |
-
model_ai = AutoModelForSequenceClassification.from_pretrained("roberta-base-openai-detector").to(device)
|
31 |
-
|
32 |
-
# AI detection function using the RoBERTa-based model
|
33 |
def detect_ai_generated(text):
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
probabilities = torch.softmax(outputs.logits, dim=1)
|
38 |
-
ai_probability = probabilities[0][1].item() * 100 # Probability of being AI-generated
|
39 |
-
human_probability = 100 - ai_probability # Probability of being Human-written
|
40 |
|
41 |
-
#
|
42 |
-
if
|
43 |
-
label = "AI"
|
44 |
-
probability = ai_probability
|
45 |
-
else:
|
46 |
-
label = "Human"
|
47 |
-
probability = human_probability
|
48 |
|
49 |
-
|
|
|
50 |
|
51 |
# Function to get synonyms using NLTK WordNet
|
52 |
def get_synonyms_nltk(word, pos):
|
@@ -118,18 +106,18 @@ def paraphrase_and_correct(text):
|
|
118 |
return final_text
|
119 |
|
120 |
# Gradio interface definition
|
121 |
-
with gr.Blocks() as
|
122 |
with gr.Row():
|
123 |
with gr.Column():
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
)
|
129 |
-
|
130 |
-
label1 = gr.Textbox(lines=1, label='Predicted Label 🎃')
|
131 |
-
score1 = gr.Textbox(lines=1, label='Probability (%)')
|
132 |
|
133 |
-
|
|
|
134 |
|
135 |
-
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
from transformers import pipeline
|
|
|
4 |
import spacy
|
5 |
import subprocess
|
6 |
import nltk
|
|
|
21 |
# Load a smaller Word2Vec model from Gensim's pre-trained models
|
22 |
word_vectors = api.load("glove-wiki-gigaword-50")
|
23 |
|
24 |
+
# Load the English AI detection pipeline using the Hello-SimpleAI model
|
25 |
+
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
26 |
|
27 |
+
# AI detection function using the Hello-SimpleAI/chatgpt-detector-roberta model
|
|
|
|
|
|
|
|
|
28 |
def detect_ai_generated(text):
|
29 |
+
res = pipeline_en(text)[0]
|
30 |
+
label = res['label'] # "LABEL_0" or "LABEL_1"
|
31 |
+
score = res['score'] * 100 # Convert probability to percentage
|
|
|
|
|
|
|
32 |
|
33 |
+
# Map the model's label to human-readable label
|
34 |
+
human_readable_label = "AI" if label == "LABEL_1" else "Human"
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# Return formatted string with label and percentage score
|
37 |
+
return f"The content is {score:.2f}% {human_readable_label} Written", score
|
38 |
|
39 |
# Function to get synonyms using NLTK WordNet
|
40 |
def get_synonyms_nltk(word, pos):
|
|
|
106 |
return final_text
|
107 |
|
108 |
# Gradio interface definition
|
109 |
+
with gr.Blocks() as interface:
|
110 |
with gr.Row():
|
111 |
with gr.Column():
|
112 |
+
text_input = gr.Textbox(lines=5, label="Input Text")
|
113 |
+
detect_button = gr.Button("AI Detection")
|
114 |
+
paraphrase_button = gr.Button("Paraphrase & Correct")
|
115 |
+
with gr.Column():
|
116 |
+
output_label = gr.Textbox(label="Predicted Label 🎃")
|
117 |
+
output_prob = gr.Textbox(label="Probability (%)")
|
|
|
|
|
118 |
|
119 |
+
detect_button.click(detect_ai_generated, inputs=text_input, outputs=[output_label, output_prob])
|
120 |
+
paraphrase_button.click(paraphrase_and_correct, inputs=text_input, outputs=output_label)
|
121 |
|
122 |
+
# Launch the Gradio app
|
123 |
+
interface.launch(debug=False)
|