-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
80 lines (55 loc) · 2.09 KB
/
Copy pathserver.py
File metadata and controls
80 lines (55 loc) · 2.09 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# ============================================
# PART 1: IMPORTS
# ============================================
from flask import Flask, request, jsonify
from flask_cors import CORS
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from peft import PeftModel # NEW: This loads LoRA adapters
import torch
# ============================================
# PART 2: SETUP
# ============================================
app = Flask(__name__)
CORS(app)
# ============================================
# PART 3: LOAD MODEL (Base + LoRA Adapter)
# ============================================
print("Loading model...")
# Step 1: Load the original base model from Hugging Face
base_model = AutoModelForSequenceClassification.from_pretrained(
"distilbert-base-uncased",
num_labels=2
)
# Step 2: Load YOUR LoRA adapter on top of it
model = PeftModel.from_pretrained(base_model, "./my-sentiment-model")
# Step 3: Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
# Set to evaluation mode
model.eval()
print("Model loaded!")
# ============================================
# PART 4: API ENDPOINT - Analyze Sentiment
# ============================================
@app.route("/api/analyze", methods=["POST"])
def analyze():
text = request.json.get("text", "")
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
with torch.no_grad():
outputs = model(**inputs)
probs = torch.softmax(outputs.logits, dim=-1)
pred = outputs.logits.argmax(dim=-1).item()
return jsonify({
"sentiment": "Positive" if pred == 1 else "Negative",
"confidence": round(probs.max().item() * 100, 2)
})
# ============================================
# PART 5: HEALTH CHECK
# ============================================
@app.route("/api/health", methods=["GET"])
def health():
return jsonify({"status": "ok"})
# ============================================
# PART 6: START SERVER
# ============================================
if __name__ == "__main__":
app.run(debug=True, port=5000)