Spaces:
Running
Running
File size: 13,656 Bytes
423a42f 676abcb dd5f028 5e589ed db71d9f 99d7e90 6257584 3694ac7 e702ced 3694ac7 cead8dd 529badd dd5f028 686a583 01793a8 509ca73 74de502 adb775b e702ced 74de502 272850f b28bd83 74de502 f92b9c9 74de502 f92b9c9 272850f 74de502 272850f b28bd83 adb775b 6d8186b 6257584 ec2738b 7497699 a31113d a2be7db 10f9ea6 333978a 0204bd8 646c385 5aecc17 e6d9b34 5aecc17 10f9ea6 1af9f6b 64fe3e4 5aecc17 e60d9fc 10f9ea6 09a4a4b 0a134b5 09a4a4b e60d9fc 09a4a4b a6b3f44 09a4a4b a6b3f44 5abdf22 09a4a4b 10f9ea6 e60d9fc ccb7aa0 370c257 24c6700 dd5f028 238c426 5aecc17 1af9f6b 24c6700 adb775b f92b9c9 adb775b 1af9f6b 423a42f e947bcb c34d039 84ceaf6 387a3f9 84ceaf6 58fdf31 84ceaf6 24c6700 10f9ea6 423a42f ccb7aa0 370c257 24c6700 0a134b5 c26acbb ebcdc0f 529badd 7c0f013 529badd c26acbb c672f84 c26acbb ebcdc0f 529badd ebcdc0f 529badd ebcdc0f 529badd ebcdc0f 7c0f013 7226a4c 2cc7269 c26acbb fdce613 3e52308 f7f2c28 f92b9c9 f7f2c28 d38ae24 3028812 d38ae24 3028812 d38ae24 c26acbb 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
from huggingface_hub import InferenceClient
import os
import random
import json
from flask import Flask, jsonify
from flask import Flask, request, jsonify, redirect, url_for
from flask_cors import CORS
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine, Column, Integer, String, Boolean, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import LargeBinary
from sqlalchemy.orm import sessionmaker, relationship
import json
HF_TOKEN = os.getenv("HF_TOKEN")
client = InferenceClient(model="mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN)
connection_string = "postgresql://neondb_owner:[email protected]/neondb?sslmode=require"
Base = declarative_base()
class Stream(Base):
__tablename__ = 'streams'
name = Column(String, primary_key=True, nullable=False)
class Mentor(Base):
__tablename__ = 'mentors'
id = Column(Integer, primary_key=True)
mentor_name = Column(String)
username = Column(String, unique=True)
profile_photo = Column(LargeBinary)
description = Column(String)
highest_degree = Column(String)
expertise = Column(String)
recent_project = Column(String)
meeting_time = Column(String)
fees = Column(String)
stream_name = Column(String, ForeignKey('streams.name'))
country = Column(String)
verified = Column(Boolean, default=False)
stream = relationship("Stream", backref="mentors")
stream = relationship("Stream", backref="mentors")
engine = create_engine(connection_string)
Session = sessionmaker(bind=engine)
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:
You are an AI mentor providing concise and complete responses. Answer the user's question clearly and in a few sentences.
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') # Uncomment this line
user_stream = content.get('stream')
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 degree recommendation for a student. Below are current details.
Stream: {user_stream}
Based on current details recommend the degree for higher education.
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_stream = content.get('stream')
session = Session()
# Query verified mentors
verified_mentors = session.query(Mentor).filter_by(verified=True).all()
mentor_list = []
for mentor in verified_mentors:
mentor_info = {
"id": mentor.id,
"mentor_name": mentor.mentor_name,
"profile_photo": mentor.profile_photo.decode('utf-8'), # Decode binary photo to string
"description": mentor.description,
"highest_degree": mentor.highest_degree,
"expertise": mentor.expertise,
"recent_project": mentor.recent_project,
"meeting_time": mentor.meeting_time,
"fees": mentor.fees,
"stream": mentor.stream,
"country": mentor.country,
"verified": mentor.verified
}
mentor_list.append(mentor_info)
session.close()
mentors_data= mentor_list
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 as a recommendation engine for mentor recommendations based on the student's stream and a list of available mentors.
Stream: {user_stream}
Mentor list: {mentors_data}
Based on the provided details, recommend the mentors that relate to the student's stream. Dont choose mentor outside mentors list
Note: The output should be a valid list, containing only the mentor's name from attached mentor list. Dont give unnecessary explanations or additional details
"""
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_streams', methods=['GET'])
def get_streams():
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,
)
prompt = """
You are a recommendation engine.
List at least 40 branches of study (e.g., Computer Science, Chemical Engineering, Aerospace).
**Output should be a valid JSON array with double quotes, like this:**
["Computer Science", "Chemical Engineering", "Aerospace", ...]
Do not add extra text, explanations, or newlines.
"""
formatted_prompt = format_prompt(prompt)
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False)
try:
# Ensure the model's response is a valid JSON array
cleaned_data = stream.strip()
# Fix incomplete or malformed JSON
if not cleaned_data.startswith("[") or not cleaned_data.endswith("]"):
cleaned_data = cleaned_data.split("[", 1)[-1] # Keep text after first [
cleaned_data = "[" + cleaned_data # Add missing opening bracket
cleaned_data = cleaned_data.rsplit("]", 1)[0] # Keep text before last ]
cleaned_data = cleaned_data + "]" # Add missing closing bracket
# Parse JSON safely
parsed_data = json.loads(cleaned_data)
if not isinstance(parsed_data, list): # Ensure it's a list
raise ValueError("Response is not a valid list")
return jsonify({"ans": parsed_data}) # Return clean JSON list
except Exception as e:
return jsonify({"error": "Invalid response format", "details": str(e)})
@app.route('/get_education_profiles', methods=['GET'])
def get_education_profiles():
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,
)
sectors = ["engineering", "medical", "arts", "commerce", "science", "management"] # Example sectors
prompt = f"""prompt:
You need to act like a recommendation engine.
List all education-related profiles in sectors like {', '.join(sectors)}.
Note: Output should be a list in the below format:
[profile1, profile2, profile3,...]
Return only the answer, not the prompt or unnecessary stuff, and don't add any special characters or punctuation marks.
"""
formatted_prompt = format_prompt(prompt)
education_profiles = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False)
return jsonify({"ans": education_profiles})
@app.route('/get_certificate', methods=['POST'])
def get_certificate():
temperature = 0.9
max_new_tokens = 256
top_p = 0.95
repetition_penalty = 1.0
content = request.json
# user_degree = content.get('degree') # Uncomment this line
user_stream = content.get('stream')
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 certification recommendation for a student. Below are current details.
Stream: {user_stream}
Based on current details recommend the certification
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_three_streams', methods=['POST'])
def get_three_streams():
temperature = 0.9
max_new_tokens = 256
top_p = 0.95
repetition_penalty = 1.0
content = request.json
user_degree = content.get('degree') # Uncomment this line
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 stream recommendation for a student based on user degree. Below are details.
Degree: {user_degree}
Based on above degree details recommend only 3 the streams
Note: Output should be list in below format:
[stream1, stream2, stream2]
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_competition', methods=['POST'])
def get_competition():
temperature = 0.9
max_new_tokens = 256
top_p = 0.95
repetition_penalty = 1.0
content = request.json
# user_degree = content.get('degree') # Uncomment this line
user_stream = content.get('stream')
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 competition recommendation for a student. Below are current details.
Stream: {user_stream}
Based on current details recommend the competition
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})
def validate_field(field_name, field_value):
prompt = f"Check if the following {field_name} is valid: {field_value}. Return 'true' if valid, else 'false'."
response = client.text_generation(prompt)
return "true" in response.lower()
@app.route("/validate", methods=["POST"])
def validate():
data = request.json
if not data:
return jsonify({"error": "No data provided"}), 400
validation_results = {}
for field, value in data.items():
validation_results[field] = validate_field(field, value)
return jsonify(validation_results)
if __name__ == '__main__':
app.run(debug=True)
|