darshan8950 commited on
Commit
423a42f
·
verified ·
1 Parent(s): c34d039

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +61 -18
main.py CHANGED
@@ -1,11 +1,11 @@
1
 
2
  from flask import Flask, request, jsonify
 
 
3
 
4
- device = "cuda" # the device to load the model onto
5
-
6
- from ctransformers import AutoModelForCausalLM
7
-
8
- llm = AutoModelForCausalLM.from_pretrained("TheBloke/Mistral-7B-v0.1-GGUF", model_file="mistral-7b-v0.1.Q4_K_M.gguf", model_type="mistral", gpu_layers=00)
9
 
10
  app = Flask(__name__)
11
 
@@ -17,13 +17,33 @@ with open(file_path, "r") as file:
17
  def home():
18
  return jsonify({"message": "Welcome to the Recommendation API!"})
19
 
 
 
 
 
 
 
 
20
  @app.route('/recommend', methods=['POST'])
21
- def recommendation():
22
  content = request.json
23
  user_degree = content.get('degree')
24
  user_stream = content.get('stream')
25
  user_semester = content.get('semester')
26
- prompt = """ prompt:
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  You need to act like as recommendataion engine for course recommendation for student based on below details.
28
 
29
  Degree: {user_degree}
@@ -33,22 +53,39 @@ def recommendation():
33
 
34
  Based on above details recommend the courses that realtes to above details
35
  Note: Output should bevalid json format in below format:
36
- {{"course1:ABC,course2:DEF,course3:XYZ,...}}
37
 
38
  """
39
- suffix="[/INST]"
40
- prefix="[INST] <<SYS>> You are a helpful assistant <</SYS>>"
41
- prompt = f"{prefix}{prompt}{suffix}"
42
- return jsonify({"ans":llm(prompt)})
43
 
 
 
 
 
 
 
 
44
 
45
  @app.route('/get_mentor', methods=['POST'])
46
- def mentor():
47
  content = request.json
48
  user_degree = content.get('degree')
49
  user_stream = content.get('stream')
50
  user_semester = content.get('semester')
51
  courses = content.get('courses')
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  prompt = f""" prompt:
53
  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.
54
 
@@ -60,12 +97,18 @@ def mentor():
60
  Mentor list= {mentors_data}
61
  Based on above details recommend the mentor that realtes to above details
62
  Note: Output should be valid json format in below format:
63
- {{"mentor1:ABC,mentor2:DEF,mentor3:XYZ,...}}
64
  """
65
- suffix="[/INST]"
66
- prefix="[INST] <<SYS>> You are a helpful assistant <</SYS>>"
67
- prompt = f"{prefix}{prompt}{suffix}"
68
- return jsonify({"ans":llm(prompt)})
 
 
 
 
 
 
69
 
70
  if __name__ == '__main__':
71
  app.run(debug=True)
 
1
 
2
  from flask import Flask, request, jsonify
3
+ from huggingface_hub import InferenceClient
4
+ import gradio as gr
5
 
6
+ client = InferenceClient(
7
+ "mistralai/Mistral-7B-Instruct-v0.1"
8
+ )
 
 
9
 
10
  app = Flask(__name__)
11
 
 
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('/recommend', methods=['POST'])
28
+ def recommend(temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,):
29
  content = request.json
30
  user_degree = content.get('degree')
31
  user_stream = content.get('stream')
32
  user_semester = content.get('semester')
33
+ temperature = float(temperature)
34
+ if temperature < 1e-2:
35
+ temperature = 1e-2
36
+ top_p = float(top_p)
37
+
38
+ generate_kwargs = dict(
39
+ temperature=temperature,
40
+ max_new_tokens=max_new_tokens,
41
+ top_p=top_p,
42
+ repetition_penalty=repetition_penalty,
43
+ do_sample=True,
44
+ seed=42,
45
+ )
46
+ prompt = f""" prompt:
47
  You need to act like as recommendataion engine for course recommendation for student based on below details.
48
 
49
  Degree: {user_degree}
 
53
 
54
  Based on above details recommend the courses that realtes to above details
55
  Note: Output should bevalid json format in below format:
56
+ {{"course1:course_name,course2:course_name,course3:course_name,...}}
57
 
58
  """
59
+ formatted_prompt = format_prompt(prompt)
 
 
 
60
 
61
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
62
+ output = ""
63
+
64
+ for response in stream:
65
+ output += response.token.text
66
+ yield output
67
+ return jsonify({"ans":output})
68
 
69
  @app.route('/get_mentor', methods=['POST'])
70
+ def mentor(temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,):
71
  content = request.json
72
  user_degree = content.get('degree')
73
  user_stream = content.get('stream')
74
  user_semester = content.get('semester')
75
  courses = content.get('courses')
76
+ temperature = float(temperature)
77
+ if temperature < 1e-2:
78
+ temperature = 1e-2
79
+ top_p = float(top_p)
80
+
81
+ generate_kwargs = dict(
82
+ temperature=temperature,
83
+ max_new_tokens=max_new_tokens,
84
+ top_p=top_p,
85
+ repetition_penalty=repetition_penalty,
86
+ do_sample=True,
87
+ seed=42,
88
+ )
89
  prompt = f""" prompt:
90
  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.
91
 
 
97
  Mentor list= {mentors_data}
98
  Based on above details recommend the mentor that realtes to above details
99
  Note: Output should be valid json format in below format:
100
+ {{"mentor1:mentor_name,mentor2:mentor_name,mentor3:mentor_name,...}}
101
  """
102
+ formatted_prompt = format_prompt(prompt)
103
+
104
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
105
+ output = ""
106
+
107
+ for response in stream:
108
+ output += response.token.text
109
+ yield output
110
+ return jsonify({"ans":output})
111
+
112
 
113
  if __name__ == '__main__':
114
  app.run(debug=True)