Swapnil-101 commited on
Commit
dd5f028
·
verified ·
1 Parent(s): 5abdf22

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +55 -2
main.py CHANGED
@@ -1,6 +1,12 @@
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__)
@@ -13,7 +19,49 @@ with open(file_path, "r") as file:
13
  def home():
14
  return jsonify({"message": "Welcome to the Recommendation API!"})
15
 
16
- import random
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  def format_prompt(message):
19
  # Generate a random user prompt and bot response pair
@@ -25,7 +73,9 @@ def format_prompt(message):
25
 
26
 
27
  @app.route('/get_course', methods=['POST'])
 
28
  def recommend():
 
29
  temperature = 0.9
30
  max_new_tokens = 256
31
  top_p = 0.95
@@ -60,8 +110,11 @@ def recommend():
60
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False)
61
  return jsonify({"ans": stream})
62
 
 
63
  @app.route('/get_mentor', methods=['POST'])
 
64
  def mentor():
 
65
  temperature = 0.9
66
  max_new_tokens = 256
67
  top_p = 0.95
 
 
1
  from huggingface_hub import InferenceClient
2
 
3
+ import random
4
+
5
+ from flask import Flask, request, jsonify, make_response
6
+ from flask_jwt_extended import JWTManager, create_access_token, jwt_required, get_jwt_identity
7
+ from werkzeug.security import generate_password_hash, check_password_hash
8
+ import sqlite3
9
+
10
  client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
11
 
12
  app = Flask(__name__)
 
19
  def home():
20
  return jsonify({"message": "Welcome to the Recommendation API!"})
21
 
22
+
23
+
24
+ app.config['JWT_SECRET_KEY'] = 123456
25
+ jwt = JWTManager(app)
26
+
27
+ # Create SQLite database
28
+ conn = sqlite3.connect('users.db', check_same_thread=False)
29
+ c = conn.cursor()
30
+ c.execute('''CREATE TABLE IF NOT EXISTS users
31
+ (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, password TEXT)''')
32
+ conn.commit()
33
+
34
+ # Endpoint for user registration
35
+ @app.route('/register', methods=['POST'])
36
+ def register():
37
+ data = request.get_json()
38
+ username = data.get('username')
39
+ password = data.get('password')
40
+ if not username or not password:
41
+ return jsonify({"message": "Missing username or password"}), 400
42
+ hashed_password = generate_password_hash(password)
43
+ try:
44
+ c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, hashed_password))
45
+ conn.commit()
46
+ return jsonify({"message": "User created successfully"}), 201
47
+ except sqlite3.IntegrityError:
48
+ return jsonify({"message": "Username already exists"}), 400
49
+
50
+ # Endpoint for user login
51
+ @app.route('/login', methods=['POST'])
52
+ def login():
53
+ data = request.get_json()
54
+ username = data.get('username')
55
+ password = data.get('password')
56
+ if not username or not password:
57
+ return jsonify({"message": "Missing username or password"}), 400
58
+ user = c.execute("SELECT * FROM users WHERE username=?", (username,)).fetchone()
59
+ if user and check_password_hash(user[2], password):
60
+ access_token = create_access_token(identity=username, expires_delta=False)
61
+ return jsonify({"access_token": access_token}), 200
62
+ else:
63
+ return jsonify({"message": "Invalid username or password"}), 401
64
+
65
 
66
  def format_prompt(message):
67
  # Generate a random user prompt and bot response pair
 
73
 
74
 
75
  @app.route('/get_course', methods=['POST'])
76
+ @jwt_required()
77
  def recommend():
78
+ current_user = get_jwt_identity()
79
  temperature = 0.9
80
  max_new_tokens = 256
81
  top_p = 0.95
 
110
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False)
111
  return jsonify({"ans": stream})
112
 
113
+
114
  @app.route('/get_mentor', methods=['POST'])
115
+ @jwt_required()
116
  def mentor():
117
+ current_user = get_jwt_identity()
118
  temperature = 0.9
119
  max_new_tokens = 256
120
  top_p = 0.95