Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
|
| 4 |
+
device = "cuda" # the device to load the model onto
|
| 5 |
+
|
| 6 |
+
model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@app.route('/recommend', methods=['POST'])
|
| 12 |
+
def recommendation():
|
| 13 |
+
content = request.json
|
| 14 |
+
user_degree = content.get('degree')
|
| 15 |
+
user_stream = content.get('stream')
|
| 16 |
+
user_semester = content.get('semester')
|
| 17 |
+
messages = [
|
| 18 |
+
{"role": "user", "content": f"""
|
| 19 |
+
You need to act like as recommendataion engine for course recommendation based on below details.
|
| 20 |
+
|
| 21 |
+
Degree: {user_degree}
|
| 22 |
+
Stream: {user_stream}
|
| 23 |
+
Current Semester: {user_semester}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
Based on above details recommend the courses that realtes to above details
|
| 27 |
+
Note: Output should bevalid json format in below format:
|
| 28 |
+
{{"course1:ABC,course2:DEF,course3:XYZ,...}}
|
| 29 |
+
|
| 30 |
+
"""},
|
| 31 |
+
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt")
|
| 35 |
+
|
| 36 |
+
model_inputs = encodeds.to(device)
|
| 37 |
+
model.to(device)
|
| 38 |
+
|
| 39 |
+
generated_ids = model.generate(model_inputs, max_new_tokens=1000, do_sample=True)
|
| 40 |
+
decoded = tokenizer.batch_decode(generated_ids)
|
| 41 |
+
return jsonify({"res":decoded[0]})
|
| 42 |
+
|
| 43 |
+
if __name__ == '__main__':
|
| 44 |
+
app.run(debug=True)
|