Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,21 @@
|
|
1 |
-
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
|
2 |
from flask import Flask, request, jsonify
|
3 |
-
import
|
|
|
4 |
|
5 |
-
# Initialize Flask app
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
-
# Load
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
# Run model inference
|
25 |
-
with torch.no_grad():
|
26 |
-
outputs = model(**tokens)
|
27 |
-
|
28 |
-
# Get prediction logits
|
29 |
-
logits = outputs.logits
|
30 |
-
|
31 |
-
# Convert logits to probability (0-1 range)
|
32 |
-
score = torch.sigmoid(logits).item() # Calculate the probability
|
33 |
-
score = round(score * 100, 2) # Scale the score to 0-100
|
34 |
-
|
35 |
-
# Map logits to risk classification (Low, Medium, High)
|
36 |
-
risk_classes = ["Low", "Medium", "High"]
|
37 |
-
risk = risk_classes[torch.argmax(logits).item()]
|
38 |
-
|
39 |
-
# Generate a dummy recommendation (you can customize this based on business rules)
|
40 |
-
recommendation = "Schedule another meeting before sending proposal." # Replace with your custom recommendation logic
|
41 |
-
|
42 |
-
return {
|
43 |
-
"score": score,
|
44 |
-
"confidence": round(torch.max(torch.softmax(logits, dim=-1)).item(), 2),
|
45 |
-
"risk": risk,
|
46 |
-
"recommendation": recommendation
|
47 |
-
}
|
48 |
-
|
49 |
-
# Define an endpoint for deal qualification prediction
|
50 |
-
@app.route('/predict', methods=['POST'])
|
51 |
-
def predict():
|
52 |
-
# Get input JSON data from the POST request
|
53 |
-
data = request.get_json()
|
54 |
-
|
55 |
-
# Validate input structure (ensure required fields are present)
|
56 |
-
if not all(key in data for key in ['industry', 'stage', 'amount', 'lead_score', 'emails_last_7_days', 'meetings_last_30_days']):
|
57 |
-
return jsonify({"error": "Missing required input data"}), 400
|
58 |
-
|
59 |
-
# Predict using the pre-trained model
|
60 |
-
result = predict_deal_qualification(data)
|
61 |
-
|
62 |
-
# Return the prediction result as a JSON response
|
63 |
-
return jsonify(result)
|
64 |
-
|
65 |
-
# Run the Flask app
|
66 |
-
if __name__ == '__main__':
|
67 |
-
app.run(debug=True, host="0.0.0.0", port=5000)
|
|
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
+
from model import load_model, predict_deal_score
|
3 |
+
import logging
|
4 |
|
|
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
# Load model once at startup
|
8 |
+
model, tokenizer, summarizer = load_model()
|
9 |
+
|
10 |
+
@app.route("/score", methods=["POST"])
|
11 |
+
def score():
|
12 |
+
try:
|
13 |
+
data = request.get_json()
|
14 |
+
result = predict_deal_score(data, model, tokenizer, summarizer)
|
15 |
+
return jsonify(result), 200
|
16 |
+
except Exception as e:
|
17 |
+
logging.exception("Prediction error")
|
18 |
+
return jsonify({"error": str(e)}), 500
|
19 |
+
|
20 |
+
if __name__ == "__main__":
|
21 |
+
app.run(host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|