Spaces:
Running
Running
File size: 4,425 Bytes
423a42f 822dfd5 dd5f028 99d7e90 6257584 dd5f028 1af9f6b 509ca73 6d8186b 6257584 ec2738b 7497699 a2be7db 10f9ea6 333978a 0204bd8 5aecc17 10f9ea6 1af9f6b 8b77a26 5aecc17 e60d9fc 10f9ea6 e60d9fc 09a4a4b c4de2a2 e60d9fc 09a4a4b 5abdf22 09a4a4b 10f9ea6 e60d9fc ccb7aa0 370c257 24c6700 dd5f028 24c6700 5aecc17 1af9f6b 24c6700 c4de2a2 25f7cba 0c5f3f6 1af9f6b 423a42f e947bcb c34d039 24c6700 25f7cba 24c6700 5abdf22 24c6700 10f9ea6 423a42f ccb7aa0 370c257 24c6700 c672f84 509ca73 1af9f6b |
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
from huggingface_hub import InferenceClient
import random
from flask import Flask, request, jsonify, redirect, url_for
from flask_cors import CORS
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
app = Flask(__name__)
CORS(app)
@app.route('/')
def home():
return jsonify({"message": "Welcome to the Recommendation API!"})
def format_prompt(message):
# Generate a random user prompt and bot response pair
user_prompt = "UserPrompt"
bot_response = "BotResponse"
return f"<s>[INST] {user_prompt} [/INST] {bot_response}</s> [INST] {message} [/INST]"
@app.route('/ai_mentor', methods=['POST'])
def ai_mentor():
data = request.get_json()
message = data.get('message')
if not message:
return jsonify({"message": "Missing message"}), 400
temperature = 0.9
max_new_tokens = 256
top_p = 0.95
repetition_penalty = 1.0
generate_kwargs = dict(
temperature=temperature,
max_new_tokens=max_new_tokens,
top_p=top_p,
repetition_penalty=repetition_penalty,
do_sample=True,
seed=42,
)
# Define prompt for the conversation
prompt = f""" prompt:
Act as an mentor
User: {message}"""
formatted_prompt = format_prompt(prompt)
try:
# Generate response from the Language Model
response = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False)
return jsonify({"response": response}), 200
except Exception as e:
return jsonify({"message": f"Failed to process request: {str(e)}"}), 500
@app.route('/get_course', methods=['POST'])
def get_course():
temperature = 0.9
max_new_tokens = 256
top_p = 0.95
repetition_penalty = 1.0
content = request.json
user_degree = content.get('degree')
user_stream = content.get('stream')
#user_semester = content.get('semester')
generate_kwargs = dict(
temperature=temperature,
max_new_tokens=max_new_tokens,
top_p=top_p,
repetition_penalty=repetition_penalty,
do_sample=True,
seed=42,
)
prompt = f""" prompt:
You need to act like as recommendation engine for course recommendation for a student based on below details.
Degree: {user_degree}
Stream: {user_stream}
Based on above details recommend the courses that relate to the above details
Note: Output should be list in below format:
[course1, course2, course3,...]
Return only answer not prompt and unnecessary stuff, also dont add any special characters or punctuation marks
"""
formatted_prompt = format_prompt(prompt)
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False)
return jsonify({"ans": stream})
@app.route('/get_mentor', methods=['POST'])
def get_mentor():
temperature = 0.9
max_new_tokens = 256
top_p = 0.95
repetition_penalty = 1.0
content = request.json
user_degree = content.get('degree')
user_stream = content.get('stream')
#user_semester = content.get('semester')
courses = content.get('courses')
mentors_data= content.get('mentors_data')
temperature = float(temperature)
if temperature < 1e-2:
temperature = 1e-2
top_p = float(top_p)
generate_kwargs = dict(
temperature=temperature,
max_new_tokens=max_new_tokens,
top_p=top_p,
repetition_penalty=repetition_penalty,
do_sample=True,
seed=42,
)
prompt = f""" prompt:
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.
Degree: {user_degree}
Stream: {user_stream}
courses opted:{courses}
Mentor list= {mentors_data}
Based on above details recommend the mentor that realtes to above details
Note: Output should be list in below format:
[mentor1,mentor2,mentor3,...]
Return only answer not prompt and unnecessary stuff, also dont add any special characters or punctuation marks
"""
formatted_prompt = format_prompt(prompt)
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False)
return jsonify({"ans": stream})
if __name__ == '__main__':
app.run(debug=True)
|