Spaces:
Running
Running
added some stuff
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from io import BytesIO
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import os
|
| 4 |
-
from utils import google_search,split_text_into_chunks,insert_embeddings_into_pinecone_database,query_vector_database,generate_embedding_for_user_resume,delete_vector_namespace
|
| 5 |
from fastapi import FastAPI, File, UploadFile
|
| 6 |
from fastapi.responses import JSONResponse
|
| 7 |
import docx
|
|
@@ -16,15 +16,34 @@ CX = os.getenv("SEARCH_ENGINE_ID")
|
|
| 16 |
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 17 |
PINECONE_API_KEY=os.getenv("PINECONE_API_KEY")
|
| 18 |
GEMINI_API_KEY=os.getenv("GEMINI_API_KEY")
|
|
|
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
import re
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
class CourseRecommendation(BaseModel):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
def extract_course_info(text: str) -> CourseRecommendation:
|
| 30 |
# Example regex patterns – adjust these as needed based on the response format.
|
|
@@ -37,7 +56,7 @@ def extract_course_info(text: str) -> CourseRecommendation:
|
|
| 37 |
coursename = course_match.group(1).strip() if course_match else "Unknown"
|
| 38 |
completiontime = time_match.group(0).strip() if time_match else "Unknown"
|
| 39 |
|
| 40 |
-
return CourseRecommendation(
|
| 41 |
|
| 42 |
|
| 43 |
|
|
@@ -78,12 +97,10 @@ def get_course_func(query):
|
|
| 78 |
link = item.get('link')
|
| 79 |
snippet = item.get('snippet')
|
| 80 |
content_structure={}
|
| 81 |
-
|
| 82 |
content_structure["Course_Title"]=title
|
| 83 |
content_structure["Course_Link"]=link
|
| 84 |
content_structure["Course_Snippet"]= snippet
|
| 85 |
content_structure["Scraped_Course_Details"]= scrapeCourse(url=link)
|
| 86 |
-
|
| 87 |
content.append(content_structure)
|
| 88 |
|
| 89 |
|
|
@@ -100,7 +117,6 @@ async def upload_file(user_id,file: UploadFile = File(...)):
|
|
| 100 |
content = await file.read() # Read the file content (this will return bytes)
|
| 101 |
sentences=[]
|
| 102 |
|
| 103 |
-
# Print file details for debugging
|
| 104 |
print(f"File name: {file.filename}")
|
| 105 |
print(f"File content type: {file.content_type}")
|
| 106 |
print(f"File size: {file.size} bytes")
|
|
@@ -108,7 +124,6 @@ async def upload_file(user_id,file: UploadFile = File(...)):
|
|
| 108 |
|
| 109 |
if "pdf" == file.filename.split('.')[1]:
|
| 110 |
pdf_document = fitz.open(stream=BytesIO(content), filetype="pdf")
|
| 111 |
-
# Print the content of the file (if it's text, you can decode it)
|
| 112 |
extracted_text = ""
|
| 113 |
for page_num in range(pdf_document.page_count):
|
| 114 |
page = pdf_document.load_page(page_num)
|
|
@@ -129,10 +144,10 @@ async def upload_file(user_id,file: UploadFile = File(...)):
|
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
-
@app.
|
| 133 |
-
def ask_ai_about_resume(
|
| 134 |
# Retrieve context from your vector database
|
| 135 |
-
context = query_vector_database(query=
|
| 136 |
|
| 137 |
# Ensure that an event loop is present in this thread.
|
| 138 |
try:
|
|
@@ -148,15 +163,15 @@ def ask_ai_about_resume(query, user_id):
|
|
| 148 |
model="gemini-2.0-flash",
|
| 149 |
contents=f"""
|
| 150 |
Answer this question using the context provided:
|
| 151 |
-
question: {
|
| 152 |
context: {context}
|
| 153 |
"""
|
| 154 |
)
|
| 155 |
|
| 156 |
return {"Ai_Response":response.text}
|
| 157 |
|
| 158 |
-
@app.
|
| 159 |
-
def ask_ai_about_resume(
|
| 160 |
"""
|
| 161 |
User Profile Information for Career Development
|
| 162 |
|
|
@@ -214,18 +229,33 @@ Parameters:
|
|
| 214 |
- "completiontime": an estimate of how long it would take to complete the course.
|
| 215 |
Do not include any extra text.
|
| 216 |
Recommend a course using this information below :
|
| 217 |
-
Which of the following best describes you?: {
|
| 218 |
-
Would you like to prepare for an interim role to gain experience and income while pursuing your dream job?: {
|
| 219 |
-
What is your desired role?: {
|
| 220 |
-
Why do you want to achieve this desired role?: {
|
| 221 |
-
How do you prefer to learn new skills?: {
|
| 222 |
-
How many hours per day can you dedicate to learning?: {
|
| 223 |
-
What are the biggest challenges or obstacles you face in reaching your dream role?: {
|
| 224 |
-
What is your ideal timeframe for achieving your dream role?: {
|
| 225 |
|
| 226 |
|
| 227 |
"""
|
| 228 |
)
|
| 229 |
course_info = extract_course_info(response.text)
|
| 230 |
-
courses = get_course_func(query=course_info.
|
| 231 |
-
return courses
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from io import BytesIO
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import os
|
| 4 |
+
from utils import google_search,split_text_into_chunks,insert_embeddings_into_pinecone_database,query_vector_database,generate_embedding_for_user_resume,delete_vector_namespace,create_user,login_user
|
| 5 |
from fastapi import FastAPI, File, UploadFile
|
| 6 |
from fastapi.responses import JSONResponse
|
| 7 |
import docx
|
|
|
|
| 16 |
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 17 |
PINECONE_API_KEY=os.getenv("PINECONE_API_KEY")
|
| 18 |
GEMINI_API_KEY=os.getenv("GEMINI_API_KEY")
|
| 19 |
+
MONGO_URI=os.getenv("MONGO_URI")
|
| 20 |
app = FastAPI()
|
| 21 |
|
| 22 |
import re
|
| 23 |
|
| 24 |
+
class UserBody(BaseModel):
|
| 25 |
+
Email:str
|
| 26 |
+
Password:str
|
| 27 |
+
|
| 28 |
+
class AiAnalysis(BaseModel):
|
| 29 |
+
UserId:str
|
| 30 |
+
Query:str
|
| 31 |
+
|
| 32 |
+
class UserCourse(BaseModel):
|
| 33 |
+
EmploymentStatus:str
|
| 34 |
+
InterimRole:str
|
| 35 |
+
DesiredRole:str
|
| 36 |
+
Motivation:str
|
| 37 |
+
LearningPreference:str
|
| 38 |
+
HoursSpentLearning:str
|
| 39 |
+
Challenges:str
|
| 40 |
+
TimeframeToAchieveDreamRole:str
|
| 41 |
+
userId:str
|
| 42 |
|
| 43 |
|
| 44 |
class CourseRecommendation(BaseModel):
|
| 45 |
+
CourseName: str
|
| 46 |
+
CompletionTime: str
|
| 47 |
|
| 48 |
def extract_course_info(text: str) -> CourseRecommendation:
|
| 49 |
# Example regex patterns – adjust these as needed based on the response format.
|
|
|
|
| 56 |
coursename = course_match.group(1).strip() if course_match else "Unknown"
|
| 57 |
completiontime = time_match.group(0).strip() if time_match else "Unknown"
|
| 58 |
|
| 59 |
+
return CourseRecommendation(CourseName=coursename, CompletionTime=completiontime)
|
| 60 |
|
| 61 |
|
| 62 |
|
|
|
|
| 97 |
link = item.get('link')
|
| 98 |
snippet = item.get('snippet')
|
| 99 |
content_structure={}
|
|
|
|
| 100 |
content_structure["Course_Title"]=title
|
| 101 |
content_structure["Course_Link"]=link
|
| 102 |
content_structure["Course_Snippet"]= snippet
|
| 103 |
content_structure["Scraped_Course_Details"]= scrapeCourse(url=link)
|
|
|
|
| 104 |
content.append(content_structure)
|
| 105 |
|
| 106 |
|
|
|
|
| 117 |
content = await file.read() # Read the file content (this will return bytes)
|
| 118 |
sentences=[]
|
| 119 |
|
|
|
|
| 120 |
print(f"File name: {file.filename}")
|
| 121 |
print(f"File content type: {file.content_type}")
|
| 122 |
print(f"File size: {file.size} bytes")
|
|
|
|
| 124 |
|
| 125 |
if "pdf" == file.filename.split('.')[1]:
|
| 126 |
pdf_document = fitz.open(stream=BytesIO(content), filetype="pdf")
|
|
|
|
| 127 |
extracted_text = ""
|
| 128 |
for page_num in range(pdf_document.page_count):
|
| 129 |
page = pdf_document.load_page(page_num)
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
+
@app.post("/ask")
|
| 148 |
+
def ask_ai_about_resume(req:AiAnalysis):
|
| 149 |
# Retrieve context from your vector database
|
| 150 |
+
context = query_vector_database(query=req.Query, api_key=PINECONE_API_KEY, name_space=req.UserId)
|
| 151 |
|
| 152 |
# Ensure that an event loop is present in this thread.
|
| 153 |
try:
|
|
|
|
| 163 |
model="gemini-2.0-flash",
|
| 164 |
contents=f"""
|
| 165 |
Answer this question using the context provided:
|
| 166 |
+
question: {req.Query}
|
| 167 |
context: {context}
|
| 168 |
"""
|
| 169 |
)
|
| 170 |
|
| 171 |
return {"Ai_Response":response.text}
|
| 172 |
|
| 173 |
+
@app.post("/recommend/courses")
|
| 174 |
+
def ask_ai_about_resume(request:UserCourse):
|
| 175 |
"""
|
| 176 |
User Profile Information for Career Development
|
| 177 |
|
|
|
|
| 229 |
- "completiontime": an estimate of how long it would take to complete the course.
|
| 230 |
Do not include any extra text.
|
| 231 |
Recommend a course using this information below :
|
| 232 |
+
Which of the following best describes you?: {request.EmploymentStatus}
|
| 233 |
+
Would you like to prepare for an interim role to gain experience and income while pursuing your dream job?: {request.InterimRole}
|
| 234 |
+
What is your desired role?: {request.DesiredRole}
|
| 235 |
+
Why do you want to achieve this desired role?: {request.Motivation}
|
| 236 |
+
How do you prefer to learn new skills?: {request.LearningPreference}
|
| 237 |
+
How many hours per day can you dedicate to learning?: {request.HoursSpentLearning}
|
| 238 |
+
What are the biggest challenges or obstacles you face in reaching your dream role?: {request.Challenges}
|
| 239 |
+
What is your ideal timeframe for achieving your dream role?: {request.TimeframeToAchieveDreamRole}
|
| 240 |
|
| 241 |
|
| 242 |
"""
|
| 243 |
)
|
| 244 |
course_info = extract_course_info(response.text)
|
| 245 |
+
courses = get_course_func(query=course_info.CourseName)
|
| 246 |
+
return {"CourseInfo":course_info,"Courses":courses}
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
@app.post("/login")
|
| 251 |
+
def signUp(user:UserBody):
|
| 252 |
+
user ={"email":user.Email,"password":user.Password}
|
| 253 |
+
user_id= login_user(db_uri=MONGO_URI,db_name="crayonics",collection_name="users",document=user)
|
| 254 |
+
return {"user_id":user_id}
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
@app.post("/signup")
|
| 258 |
+
def signUp(user:UserBody):
|
| 259 |
+
user ={"email":user.Email,"password":user.Password}
|
| 260 |
+
user_id= create_user(db_uri=MONGO_URI,db_name="crayonics",collection_name="users",document=user)
|
| 261 |
+
return {"user_id":user_id}
|
utils.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import requests
|
|
|
|
| 2 |
|
| 3 |
def google_search(query, api_key, cx):
|
| 4 |
url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={api_key}&cx={cx}"
|
|
@@ -107,3 +108,74 @@ def split_text_into_chunks(text, chunk_size=400):
|
|
| 107 |
chunks = [' '.join(words[i:i + chunk_size]) for i in range(0, len(words), chunk_size)]
|
| 108 |
return chunks
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
from pymongo import MongoClient
|
| 3 |
|
| 4 |
def google_search(query, api_key, cx):
|
| 5 |
url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={api_key}&cx={cx}"
|
|
|
|
| 108 |
chunks = [' '.join(words[i:i + chunk_size]) for i in range(0, len(words), chunk_size)]
|
| 109 |
return chunks
|
| 110 |
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def create_user(db_uri: str, db_name: str, collection_name: str, document: dict) -> str:
|
| 116 |
+
"""
|
| 117 |
+
Inserts a new document into the specified MongoDB collection.
|
| 118 |
+
|
| 119 |
+
Parameters:
|
| 120 |
+
db_uri (str): MongoDB connection URI.
|
| 121 |
+
db_name (str): Name of the database.
|
| 122 |
+
collection_name (str): Name of the collection.
|
| 123 |
+
document (dict): The document to insert.
|
| 124 |
+
|
| 125 |
+
Returns:
|
| 126 |
+
str: The ID of the inserted document.
|
| 127 |
+
"""
|
| 128 |
+
# Connect to MongoDB
|
| 129 |
+
client = MongoClient(db_uri)
|
| 130 |
+
db = client[db_name]
|
| 131 |
+
collection = db[collection_name]
|
| 132 |
+
|
| 133 |
+
# Insert the document
|
| 134 |
+
s = collection.find_one(document)
|
| 135 |
+
if s==None:
|
| 136 |
+
result = collection.insert_one(document)
|
| 137 |
+
client.close()
|
| 138 |
+
return str(result.inserted_id)
|
| 139 |
+
else:
|
| 140 |
+
client.close()
|
| 141 |
+
return str(s['_id'])
|
| 142 |
+
|
| 143 |
+
# Close the connection
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def login_user(db_uri: str, db_name: str, collection_name: str, document: dict) -> str:
|
| 150 |
+
"""
|
| 151 |
+
Inserts a new document into the specified MongoDB collection.
|
| 152 |
+
|
| 153 |
+
Parameters:
|
| 154 |
+
db_uri (str): MongoDB connection URI.
|
| 155 |
+
db_name (str): Name of the database.
|
| 156 |
+
collection_name (str): Name of the collection.
|
| 157 |
+
document (dict): The document to insert.
|
| 158 |
+
|
| 159 |
+
Returns:
|
| 160 |
+
str: The ID of the inserted document.
|
| 161 |
+
"""
|
| 162 |
+
# Connect to MongoDB
|
| 163 |
+
client = MongoClient(db_uri)
|
| 164 |
+
db = client[db_name]
|
| 165 |
+
collection = db[collection_name]
|
| 166 |
+
|
| 167 |
+
# Insert the document
|
| 168 |
+
s = collection.find_one(document)
|
| 169 |
+
if s==None:
|
| 170 |
+
return False
|
| 171 |
+
else:
|
| 172 |
+
if document['password']==s['password']:
|
| 173 |
+
client.close()
|
| 174 |
+
return str(s['_id'])
|
| 175 |
+
else:
|
| 176 |
+
return True
|
| 177 |
+
# Close the connection
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
|