-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
65 lines (51 loc) · 2.03 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, request, jsonify
from flask_cors import CORS
import pickle
import re
# Load the model and vectorizer
with open("log_model.pkl", "rb") as model_file:
model = pickle.load(model_file)
with open("tfidf_vectorizer.pkl", "rb") as vectorizer_file:
vectorizer = pickle.load(vectorizer_file)
# Initialize Flask app
app = Flask(__name__)
CORS(app) # Enable CORS for cross-origin requests
# Test route to check server health
@app.route('/')
def home():
return "Flask server is running!"
# Prediction endpoint (handles both POST and non-POST methods)
@app.route('/predict', methods=['POST', 'GET'])
def predict():
if request.method == 'GET':
# Handle GET request with a user-friendly message
return jsonify({"error": "This endpoint only supports POST requests. Please send a POST request."}), 405
try:
data = request.get_json()
input_text = data.get('text', '')
if not input_text:
return jsonify({"error": "No text provided"}), 400
# Preprocess and vectorize input text
cleaned_text = clean_text(input_text)
vectorized_text = vectorizer.transform([cleaned_text])
# Predict and get probabilities
probabilities = model.predict_proba(vectorized_text)[0]
prediction = model.predict(vectorized_text)[0]
# Get the confidence score for the predicted class
confidence = max(probabilities) * 100
# Return prediction and confidence score
return jsonify({
"prediction": "Human-written" if prediction == 0 else "AI-generated",
"confidence": confidence
})
except Exception as e:
app.logger.error(f"Error in prediction endpoint: {str(e)}")
return jsonify({"error": str(e)}), 500
# Preprocessing function
def clean_text(text):
text = text.lower()
text = re.sub(r'[^a-z\s]', '', text)
return text
# Run the app
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)