karthikmn commited on
Commit
3cc00a5
·
verified ·
1 Parent(s): 2240d27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -63
app.py CHANGED
@@ -1,67 +1,21 @@
1
- from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
2
  from flask import Flask, request, jsonify
3
- import torch
 
4
 
5
- # Initialize Flask app
6
  app = Flask(__name__)
7
 
8
- # Load pre-trained DistilBERT model and tokenizer
9
- model_name = "distilbert-base-uncased"
10
- tokenizer = DistilBertTokenizer.from_pretrained(model_name)
11
- model = DistilBertForSequenceClassification.from_pretrained(model_name)
12
-
13
- # Ensure the model is in evaluation mode
14
- model.eval()
15
-
16
- # Define a function to predict score and risk based on the input
17
- def predict_deal_qualification(inputs):
18
- # Prepare the input text (combining fields for the model)
19
- input_text = f"Industry: {inputs['industry']}, Stage: {inputs['stage']}, Amount: {inputs['amount']}, Lead Score: {inputs['lead_score']}, Email count: {inputs['emails_last_7_days']}, Meeting count: {inputs['meetings_last_30_days']}"
20
-
21
- # Tokenize the input text
22
- tokens = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True, max_length=128)
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)