Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,79 @@
|
|
| 1 |
-
#https://www.freecodecamp.org/news/how-to-setup-virtual-environments-in-python/
|
| 2 |
-
#https://www.youtube.com/watch?v=qbLc5a9jdXo&ab_channel=CalebCurry
|
| 3 |
-
#https://stackoverflow.com/questions/26368306/export-is-not-recognized-as-an-internal-or-external-command
|
| 4 |
-
#python3 -m venv .venv
|
| 5 |
-
#source .venv/bin/activate
|
| 6 |
-
#
|
| 7 |
-
#pip freeze > requirements.txt
|
| 8 |
-
#$env:FLASK_APP="application.py" #set FLASK_APP=application.py # export FLASK_APP=application.py
|
| 9 |
-
#set FLASK_ENV=development #export FLASK_ENV=production
|
| 10 |
-
#flask run #flask run --host=0.0.0.0
|
| 11 |
-
|
| 12 |
-
#pip install torchvision
|
| 13 |
-
|
| 14 |
-
from flask import Flask, request, jsonify
|
| 15 |
-
from flask_cors import CORS
|
| 16 |
-
import pandas
|
| 17 |
-
from human_text_detect import detect_human_text
|
| 18 |
-
|
| 19 |
-
app = Flask(__name__)
|
| 20 |
-
CORS(app)
|
| 21 |
-
|
| 22 |
-
@app.route('/')
|
| 23 |
-
def index():
|
| 24 |
-
return 'Hello'
|
| 25 |
-
|
| 26 |
-
@app.route('/detectHumanInAIText/checkText', methods=['POST'])
|
| 27 |
-
def check_text():
|
| 28 |
-
|
| 29 |
-
# Get data
|
| 30 |
-
print('Get data')
|
| 31 |
-
data = request.get_json()
|
| 32 |
-
text = data.get('text')
|
| 33 |
-
model_name = data.get('model')
|
| 34 |
-
topic = data.get('topic')
|
| 35 |
-
|
| 36 |
-
# Validate data
|
| 37 |
-
print('Validate data')
|
| 38 |
-
answer = validate_data(text, model_name, topic)
|
| 39 |
-
if answer != '':
|
| 40 |
-
return jsonify({'answer': answer}), 400
|
| 41 |
-
|
| 42 |
-
topic = check_topic(topic)
|
| 43 |
-
answer = detect_human_text(model_name, topic, text)
|
| 44 |
-
|
| 45 |
-
return jsonify({'answer': answer})
|
| 46 |
-
|
| 47 |
-
def validate_data(text, model_name, topic):
|
| 48 |
-
if text is None or text == '':
|
| 49 |
-
return 'Text is missing'
|
| 50 |
-
|
| 51 |
-
if model_name is None or model_name == '':
|
| 52 |
-
return 'Model name is missing'
|
| 53 |
-
|
| 54 |
-
if topic is None or topic == '':
|
| 55 |
-
return 'Topic is missing'
|
| 56 |
-
|
| 57 |
-
if model_name not in ['GPT2XL', 'PHI2']:
|
| 58 |
-
return f'Model {model_name} not supported'
|
| 59 |
-
|
| 60 |
-
if topic not in ['Characters', 'Locations', 'Nature', 'Video games', 'Series', 'Movies', 'War']:
|
| 61 |
-
return f'Topic {topic} not supported'
|
| 62 |
-
|
| 63 |
-
return ''
|
| 64 |
-
|
| 65 |
-
def check_topic(topic):
|
| 66 |
-
topic_dict = {
|
| 67 |
-
'Characters': 'characters',
|
| 68 |
-
'Locations': 'locations',
|
| 69 |
-
'Nature': 'nature',
|
| 70 |
-
'Video games': 'video_games_series_movies',
|
| 71 |
-
'Series': 'video_games_series_movies',
|
| 72 |
-
'Movies': 'video_games_series_movies',
|
| 73 |
-
'War': 'war'
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
return topic_dict[topic]
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#https://www.freecodecamp.org/news/how-to-setup-virtual-environments-in-python/
|
| 2 |
+
#https://www.youtube.com/watch?v=qbLc5a9jdXo&ab_channel=CalebCurry
|
| 3 |
+
#https://stackoverflow.com/questions/26368306/export-is-not-recognized-as-an-internal-or-external-command
|
| 4 |
+
#python3 -m venv .venv
|
| 5 |
+
#source .venv/bin/activate
|
| 6 |
+
#
|
| 7 |
+
#pip freeze > requirements.txt
|
| 8 |
+
#$env:FLASK_APP="application.py" #set FLASK_APP=application.py # export FLASK_APP=application.py
|
| 9 |
+
#set FLASK_ENV=development #export FLASK_ENV=production
|
| 10 |
+
#flask run #flask run --host=0.0.0.0
|
| 11 |
+
|
| 12 |
+
#pip install torchvision
|
| 13 |
+
|
| 14 |
+
from flask import Flask, request, jsonify
|
| 15 |
+
from flask_cors import CORS
|
| 16 |
+
import pandas
|
| 17 |
+
from human_text_detect import detect_human_text
|
| 18 |
+
|
| 19 |
+
app = Flask(__name__)
|
| 20 |
+
CORS(app)
|
| 21 |
+
|
| 22 |
+
@app.route('/')
|
| 23 |
+
def index():
|
| 24 |
+
return 'Hello'
|
| 25 |
+
|
| 26 |
+
@app.route('/detectHumanInAIText/checkText', methods=['POST'])
|
| 27 |
+
def check_text():
|
| 28 |
+
|
| 29 |
+
# Get data
|
| 30 |
+
print('Get data')
|
| 31 |
+
data = request.get_json()
|
| 32 |
+
text = data.get('text')
|
| 33 |
+
model_name = data.get('model')
|
| 34 |
+
topic = data.get('topic')
|
| 35 |
+
|
| 36 |
+
# Validate data
|
| 37 |
+
print('Validate data')
|
| 38 |
+
answer = validate_data(text, model_name, topic)
|
| 39 |
+
if answer != '':
|
| 40 |
+
return jsonify({'answer': answer}), 400
|
| 41 |
+
|
| 42 |
+
topic = check_topic(topic)
|
| 43 |
+
answer = detect_human_text(model_name, topic, text)
|
| 44 |
+
|
| 45 |
+
return jsonify({'answer': answer})
|
| 46 |
+
|
| 47 |
+
def validate_data(text, model_name, topic):
|
| 48 |
+
if text is None or text == '':
|
| 49 |
+
return 'Text is missing'
|
| 50 |
+
|
| 51 |
+
if model_name is None or model_name == '':
|
| 52 |
+
return 'Model name is missing'
|
| 53 |
+
|
| 54 |
+
if topic is None or topic == '':
|
| 55 |
+
return 'Topic is missing'
|
| 56 |
+
|
| 57 |
+
if model_name not in ['GPT2XL', 'PHI2']:
|
| 58 |
+
return f'Model {model_name} not supported'
|
| 59 |
+
|
| 60 |
+
if topic not in ['Characters', 'Locations', 'Nature', 'Video games', 'Series', 'Movies', 'War']:
|
| 61 |
+
return f'Topic {topic} not supported'
|
| 62 |
+
|
| 63 |
+
return ''
|
| 64 |
+
|
| 65 |
+
def check_topic(topic):
|
| 66 |
+
topic_dict = {
|
| 67 |
+
'Characters': 'characters',
|
| 68 |
+
'Locations': 'locations',
|
| 69 |
+
'Nature': 'nature',
|
| 70 |
+
'Video games': 'video_games_series_movies',
|
| 71 |
+
'Series': 'video_games_series_movies',
|
| 72 |
+
'Movies': 'video_games_series_movies',
|
| 73 |
+
'War': 'war'
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
return topic_dict[topic]
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
app.run(host="0.0.0.0", port=5000)
|