Nattyboi commited on
Commit
3c12613
·
verified ·
1 Parent(s): 5335c19

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
8
+ import fitz
9
+ load_dotenv()
10
+
11
+ CX = os.getenv("SEARCH_ENGINE_ID")
12
+ API_KEY = os.getenv("API_KEY")
13
+ PINECONE_API_KEY=os.getenv("PINECONE_API_KEY")
14
+ GEMINI_API_KEY=os.getenv("GEMINI_API_KEY")
15
+ app = FastAPI()
16
+
17
+ @app.get("/get/course")
18
+ def get_course(query):
19
+ # Example search query
20
+ results = google_search(query, API_KEY, CX)
21
+ content=[]
22
+
23
+ if results:
24
+ for item in results.get('items', []):
25
+ title = item.get('title')
26
+ link = item.get('link')
27
+ snippet = item.get('snippet')
28
+ content_structure={}
29
+
30
+ content_structure["Course_Title"]=title
31
+ content_structure["Course_Link"]=link
32
+ content_structure["Course_Snippet"]= snippet
33
+
34
+ content.append(content_structure)
35
+
36
+
37
+ return JSONResponse(content,status_code=200)
38
+
39
+
40
+
41
+
42
+
43
+
44
+ @app.post("/upload")
45
+ async def upload_file(user_id,file: UploadFile = File(...)):
46
+ content = await file.read() # Read the file content (this will return bytes)
47
+ sentences=[]
48
+
49
+ # Print file details for debugging
50
+ print(f"File name: {file.filename}")
51
+ print(f"File content type: {file.content_type}")
52
+ print(f"File size: {file.size} bytes")
53
+
54
+
55
+ if "pdf" == file.filename.split('.')[1]:
56
+ pdf_document = fitz.open(stream=BytesIO(content), filetype="pdf")
57
+ # Print the content of the file (if it's text, you can decode it)
58
+ extracted_text = ""
59
+ for page_num in range(pdf_document.page_count):
60
+ page = pdf_document.load_page(page_num)
61
+ extracted_text += page.get_text()
62
+
63
+ elif "docx" == file.filename.split('.')[1]:
64
+ docx_file = BytesIO(content)
65
+ doc = docx.Document(docx_file)
66
+ extracted_text = ""
67
+ for para in doc.paragraphs:
68
+ extracted_text += para.text + "\n"
69
+
70
+ sentences = split_text_into_chunks(extracted_text,chunk_size=200)
71
+ docs = generate_embedding_for_user_resume(data=sentences,user_id=file.filename)
72
+ response= insert_embeddings_into_pinecone_database(doc=docs,api_key=PINECONE_API_KEY,name_space=user_id)
73
+
74
+ return {"filename": file.filename,"response":str(response) }
75
+
76
+
77
+
78
+
79
+ @app.get("/ask")
80
+ def ask_ai_about_resume(query,user_id):
81
+ context = query_vector_database(query=query,api_key=PINECONE_API_KEY,name_space=user_id)
82
+ from google import genai
83
+
84
+ client = genai.Client(api_key=GEMINI_API_KEY)
85
+ response = client.models.generate_content(
86
+ model="gemini-2.0-flash", contents=f"""
87
+ Answer this question using the context provided
88
+ question: {query}
89
+ context: {context}
90
+ """
91
+ )
92
+
93
+ return response.text