Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import transformers
|
| 4 |
+
from synthid_text import detector_bayesian, synthid_mixin, logits_processing
|
| 5 |
+
|
| 6 |
+
# Configurations and model selection
|
| 7 |
+
MODEL_NAME = "google/gemma-7b-it" # Choose the model (Gemma models used in SynthID)
|
| 8 |
+
DEVICE = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
|
| 9 |
+
|
| 10 |
+
# Initialize tokenizer and model configuration
|
| 11 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 12 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 13 |
+
tokenizer.padding_side = "left"
|
| 14 |
+
|
| 15 |
+
# Watermarking configuration
|
| 16 |
+
CONFIG = synthid_mixin.DEFAULT_WATERMARKING_CONFIG
|
| 17 |
+
|
| 18 |
+
# Logits processor for SynthID
|
| 19 |
+
logits_processor = logits_processing.SynthIDLogitsProcessor(
|
| 20 |
+
**CONFIG, top_k=40, temperature=0.5
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Function to check for AI-generated content using SynthID
|
| 24 |
+
def check_plagiarism(text):
|
| 25 |
+
# Tokenize and process the input text
|
| 26 |
+
tokens = tokenizer.encode_plus(text, return_tensors="pt", truncation=True, padding=True)
|
| 27 |
+
tokens = tokens.to(DEVICE)
|
| 28 |
+
|
| 29 |
+
# Use SynthID's bayesian detector to check for AI generation likelihood
|
| 30 |
+
try:
|
| 31 |
+
is_ai_generated = detector_bayesian.is_generated(tokens['input_ids'], logits_processor, threshold=0.5)
|
| 32 |
+
if is_ai_generated:
|
| 33 |
+
return "Flagged as AI-generated content (Academic Integrity Warning)."
|
| 34 |
+
else:
|
| 35 |
+
return "Content appears to be human-generated."
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error in detection process: {e}"
|
| 38 |
+
|
| 39 |
+
# Define the Gradio interface
|
| 40 |
+
def create_plagiarism_checker():
|
| 41 |
+
with gr.Blocks() as app:
|
| 42 |
+
gr.Markdown("## Plagiarism and Academic Integrity Checker")
|
| 43 |
+
gr.Markdown("Paste your text below to check if it is AI-generated using SynthID detection.")
|
| 44 |
+
|
| 45 |
+
# Input textbox for users to paste text
|
| 46 |
+
text_input = gr.Textbox(placeholder="Paste your text here...", label="Input Text", lines=10)
|
| 47 |
+
|
| 48 |
+
# Output box to display the result
|
| 49 |
+
output = gr.Textbox(label="Integrity Check Result", interactive=False)
|
| 50 |
+
|
| 51 |
+
# Button to initiate the check
|
| 52 |
+
check_button = gr.Button("Check Text")
|
| 53 |
+
|
| 54 |
+
# Define the click event for the button
|
| 55 |
+
check_button.click(fn=check_plagiarism, inputs=text_input, outputs=output)
|
| 56 |
+
|
| 57 |
+
return app
|
| 58 |
+
|
| 59 |
+
# Launch the app
|
| 60 |
+
plagiarism_checker_app = create_plagiarism_checker()
|
| 61 |
+
plagiarism_checker_app.launch()
|