idkash1 commited on
Commit
7798d42
·
verified ·
1 Parent(s): d9110e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -12
app.py CHANGED
@@ -19,26 +19,22 @@ from human_text_detect import detect_human_text
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({'error': answer}), 400
41
-
 
42
  topic = check_topic(topic)
43
  hcRelativeToThreshold, df_sentences = detect_human_text(model_name, topic, text)
44
  message = 'Edits found in the text' if hcRelativeToThreshold >= 0 else 'We couldn\'t find edits in the text'
@@ -52,7 +48,40 @@ def check_text():
52
  }
53
  for _, row in df_sentences.iterrows()
54
  ]
55
- return jsonify({'message': message, 'hcRelativeToThreshold': hcRelativeToThreshold, 'sentences': sentences})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  def validate_data(text, model_name, topic):
58
  if text is None or text == '':
 
19
  app = Flask(__name__)
20
  CORS(app)
21
 
22
+ task_results = {}
23
+
24
  @app.route('/')
25
  def index():
26
  return 'Hello'
27
 
28
+ def process_analysis(task_id, text, model_name, topic):
29
+ print(f"Processing task: {task_id}")
30
 
 
 
 
 
 
 
 
31
  # Validate data
32
  print('Validate data')
33
  answer = validate_data(text, model_name, topic)
34
  if answer != '':
35
+ task_results[task_id] = {'error': answer}
36
+ return
37
+
38
  topic = check_topic(topic)
39
  hcRelativeToThreshold, df_sentences = detect_human_text(model_name, topic, text)
40
  message = 'Edits found in the text' if hcRelativeToThreshold >= 0 else 'We couldn\'t find edits in the text'
 
48
  }
49
  for _, row in df_sentences.iterrows()
50
  ]
51
+
52
+ # Store the result
53
+ task_results[task_id] = {'message': message, 'hcRelativeToThreshold': hcRelativeToThreshold, 'sentences': sentences}
54
+
55
+ @app.route('/detectHumanInAIText/checkText', methods=['POST'])
56
+ def check_text():
57
+ # Get data
58
+ print('Get data')
59
+ data = request.get_json()
60
+ text = data.get('text')
61
+ model_name = data.get('model')
62
+ topic = data.get('topic')
63
+
64
+ # Generate a unique taskId
65
+ task_id = str(uuid.uuid4())
66
+
67
+ # Start processing in a separate thread
68
+ thread = threading.Thread(target=process_analysis, args=(task_id, text, model_name, topic))
69
+ thread.start()
70
+
71
+ # Return taskId immediately
72
+ return jsonify({'taskId': task_id}), 202
73
+
74
+ @app.route('/detectHumanInAIText/getAnalyzeResults', methods=['GET'])
75
+ def get_results():
76
+ task_id = request.args.get('taskId')
77
+
78
+ if not task_id:
79
+ return jsonify({'error': 'Missing taskId parameter'}), 400
80
+
81
+ if task_id not in task_results:
82
+ return jsonify({'status': 'pending'}), 202
83
+
84
+ return jsonify(task_results.pop(task_id)), 200
85
 
86
  def validate_data(text, model_name, topic):
87
  if text is None or text == '':