Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,11 +1,7 @@
|
|
| 1 |
-
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
|
| 6 |
-
client = InferenceClient(
|
| 7 |
-
"mistralai/Mistral-7B-Instruct-v0.1"
|
| 8 |
-
)
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
|
@@ -17,13 +13,18 @@ with open(file_path, "r") as file:
|
|
| 17 |
def home():
|
| 18 |
return jsonify({"message": "Welcome to the Recommendation API!"})
|
| 19 |
|
| 20 |
-
|
| 21 |
def format_prompt(message):
|
| 22 |
prompt = "<s>"
|
| 23 |
prompt += f"[INST] {message} [/INST]"
|
| 24 |
prompt += "</s>"
|
| 25 |
return prompt
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
@app.route('/get_course', methods=['POST'])
|
| 28 |
def recommend():
|
| 29 |
temperature = 0.9
|
|
@@ -54,26 +55,23 @@ def recommend():
|
|
| 54 |
{{"course1:course_name, course2:course_name, course3:course_name,...}}
|
| 55 |
"""
|
| 56 |
formatted_prompt = format_prompt(prompt)
|
| 57 |
-
print(formatted_prompt)
|
| 58 |
-
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 59 |
-
output = ""
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
yield output
|
| 64 |
-
return output
|
| 65 |
|
| 66 |
@app.route('/get_mentor', methods=['POST'])
|
| 67 |
def mentor():
|
| 68 |
-
temperature=0.9
|
| 69 |
-
max_new_tokens=256
|
| 70 |
-
top_p=0.95
|
| 71 |
-
repetition_penalty=1.0
|
|
|
|
| 72 |
content = request.json
|
| 73 |
user_degree = content.get('degree')
|
| 74 |
user_stream = content.get('stream')
|
| 75 |
user_semester = content.get('semester')
|
| 76 |
courses = content.get('courses')
|
|
|
|
| 77 |
temperature = float(temperature)
|
| 78 |
if temperature < 1e-2:
|
| 79 |
temperature = 1e-2
|
|
@@ -89,12 +87,10 @@ def mentor():
|
|
| 89 |
)
|
| 90 |
prompt = f""" prompt:
|
| 91 |
You need to act like as recommendataion engine for mentor recommendation for student based on below details also the list of mentors with their experience is attached.
|
| 92 |
-
|
| 93 |
Degree: {user_degree}
|
| 94 |
Stream: {user_stream}
|
| 95 |
Current Semester: {user_semester}
|
| 96 |
courses opted:{courses}
|
| 97 |
-
|
| 98 |
Mentor list= {mentors_data}
|
| 99 |
Based on above details recommend the mentor that realtes to above details
|
| 100 |
Note: Output should be valid json format in below format:
|
|
@@ -103,13 +99,7 @@ def mentor():
|
|
| 103 |
formatted_prompt = format_prompt(prompt)
|
| 104 |
|
| 105 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
for response in stream:
|
| 109 |
-
output += response.token.text
|
| 110 |
-
yield output
|
| 111 |
-
return jsonify({"ans":output})
|
| 112 |
-
|
| 113 |
|
| 114 |
if __name__ == '__main__':
|
| 115 |
-
app.run(debug=True)
|
|
|
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
|
|
|
| 13 |
def home():
|
| 14 |
return jsonify({"message": "Welcome to the Recommendation API!"})
|
| 15 |
|
|
|
|
| 16 |
def format_prompt(message):
|
| 17 |
prompt = "<s>"
|
| 18 |
prompt += f"[INST] {message} [/INST]"
|
| 19 |
prompt += "</s>"
|
| 20 |
return prompt
|
| 21 |
|
| 22 |
+
def generate_output(stream):
|
| 23 |
+
output = ""
|
| 24 |
+
for response in stream:
|
| 25 |
+
output += response.token.text
|
| 26 |
+
yield output
|
| 27 |
+
|
| 28 |
@app.route('/get_course', methods=['POST'])
|
| 29 |
def recommend():
|
| 30 |
temperature = 0.9
|
|
|
|
| 55 |
{{"course1:course_name, course2:course_name, course3:course_name,...}}
|
| 56 |
"""
|
| 57 |
formatted_prompt = format_prompt(prompt)
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 60 |
+
return jsonify({"ans": list(generate_output(stream))})
|
|
|
|
|
|
|
| 61 |
|
| 62 |
@app.route('/get_mentor', methods=['POST'])
|
| 63 |
def mentor():
|
| 64 |
+
temperature = 0.9
|
| 65 |
+
max_new_tokens = 256
|
| 66 |
+
top_p = 0.95
|
| 67 |
+
repetition_penalty = 1.0
|
| 68 |
+
|
| 69 |
content = request.json
|
| 70 |
user_degree = content.get('degree')
|
| 71 |
user_stream = content.get('stream')
|
| 72 |
user_semester = content.get('semester')
|
| 73 |
courses = content.get('courses')
|
| 74 |
+
|
| 75 |
temperature = float(temperature)
|
| 76 |
if temperature < 1e-2:
|
| 77 |
temperature = 1e-2
|
|
|
|
| 87 |
)
|
| 88 |
prompt = f""" prompt:
|
| 89 |
You need to act like as recommendataion engine for mentor recommendation for student based on below details also the list of mentors with their experience is attached.
|
|
|
|
| 90 |
Degree: {user_degree}
|
| 91 |
Stream: {user_stream}
|
| 92 |
Current Semester: {user_semester}
|
| 93 |
courses opted:{courses}
|
|
|
|
| 94 |
Mentor list= {mentors_data}
|
| 95 |
Based on above details recommend the mentor that realtes to above details
|
| 96 |
Note: Output should be valid json format in below format:
|
|
|
|
| 99 |
formatted_prompt = format_prompt(prompt)
|
| 100 |
|
| 101 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 102 |
+
return jsonify({"ans": list(generate_output(stream))})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
if __name__ == '__main__':
|
| 105 |
+
app.run(debug=True)
|