sashtech commited on
Commit
aec8023
·
verified ·
1 Parent(s): 686234d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -34
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import os
2
  import gradio as gr
3
- from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
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
- # Check for GPU and set the device accordingly
26
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
27
 
28
- # Load AI Detector model and tokenizer from Hugging Face (roberta-base-openai-detector)
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
- inputs = tokenizer_ai(text, return_tensors="pt", truncation=True, max_length=512).to(device)
35
- with torch.no_grad():
36
- outputs = model_ai(**inputs)
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
- # Determine the label based on the higher probability
42
- if ai_probability > human_probability:
43
- label = "AI"
44
- probability = ai_probability
45
- else:
46
- label = "Human"
47
- probability = human_probability
48
 
49
- return f"The content is {probability:.2f}% {label} Written", probability
 
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 demo:
122
  with gr.Row():
123
  with gr.Column():
124
- t1 = gr.Textbox(
125
- lines=5,
126
- label='Text',
127
- value="There are a few things that can help protect your credit card information from being misused when you give it to a restaurant or any other business:\n\nEncryption: Many businesses use encryption to protect your credit card information when it is being transmitted or stored. This means that the information is transformed into a code that is difficult for anyone to read without the right key."
128
- )
129
- button1 = gr.Button("🤖 Predict!")
130
- label1 = gr.Textbox(lines=1, label='Predicted Label 🎃')
131
- score1 = gr.Textbox(lines=1, label='Probability (%)')
132
 
133
- button1.click(detect_ai_generated, inputs=[t1], outputs=[label1, score1])
 
134
 
135
- demo.launch()
 
 
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)