|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from flask import Flask, request, jsonify |
|
from flask_cors import CORS |
|
import pandas |
|
from human_text_detect import detect_human_text |
|
|
|
app = Flask(__name__) |
|
CORS(app) |
|
|
|
@app.route('/') |
|
def index(): |
|
return 'Hello' |
|
|
|
@app.route('/detectHumanInAIText/checkText', methods=['POST']) |
|
def check_text(): |
|
|
|
|
|
print('Get data') |
|
data = request.get_json() |
|
text = data.get('text') |
|
model_name = data.get('model') |
|
topic = data.get('topic') |
|
|
|
|
|
print('Validate data') |
|
answer = validate_data(text, model_name, topic) |
|
if answer != '': |
|
return jsonify({'error': answer}), 400 |
|
|
|
topic = check_topic(topic) |
|
hcRelativeToThreshold, df_sentences = detect_human_text(model_name, topic, text) |
|
message = 'Edits found in the text' if hcRelativeToThreshold >= 0 else 'We couldn\'t find edits in the text' |
|
|
|
sentences = [ |
|
{ |
|
"sentence": row["sentence"], |
|
"lppt": row["response"], |
|
"pvalue": row["pvalue"], |
|
"color": "#f5aca4" if row["pvalue"] < 0.05 else "" |
|
} |
|
for _, row in df_sentences.iterrows() |
|
] |
|
return jsonify({'message': message, 'hcRelativeToThreshold': hcRelativeToThreshold, 'sentences': sentences}) |
|
|
|
def validate_data(text, model_name, topic): |
|
if text is None or text == '': |
|
return 'Text is missing' |
|
|
|
if model_name is None or model_name == '': |
|
return 'Model name is missing' |
|
|
|
if topic is None or topic == '': |
|
return 'Topic is missing' |
|
|
|
if model_name not in ['GPT2XL', 'PHI2']: |
|
return f'Model {model_name} not supported' |
|
|
|
if topic not in ['Characters', 'Locations', 'Nature', 'Video games', 'Series', 'Movies', 'War']: |
|
return f'Topic {topic} not supported' |
|
|
|
return '' |
|
|
|
def check_topic(topic): |
|
topic_dict = { |
|
'empirical': 'empirical', |
|
'figures': 'characters', |
|
'landmarks': 'locations', |
|
'nature': 'nature', |
|
'games': 'video_games_series_movies', |
|
'wars': 'war' |
|
} |
|
|
|
return topic_dict[topic] |