Bhanuprasadchouki commited on
Commit
f162d56
·
verified ·
1 Parent(s): 3bc1ad1

Create new_test.py

Browse files
Files changed (1) hide show
  1. new_test.py +134 -0
new_test.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import PyPDF2 # For PDF text extraction
2
+ import streamlit as st # For building the Streamlit app
3
+ from docx import Document # For Word document text extraction
4
+ from langchain.chains import RunnableLambda, RunnablePassthrough
5
+ from langchain.chat_models import ChatGoogleGenerativeAI
6
+ from langchain.memory import ConversationBufferMemory
7
+ from langchain.prompts import (ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder, SystemMessage)
8
+ from langchain.schema import StrOutputParser # For parsing the output
9
+
10
+
11
+ # Function to extract text from PDF
12
+ def extract_text_from_pdf(uploaded_file):
13
+ text = ""
14
+ reader = PyPDF2.PdfReader(uploaded_file)
15
+ for page in reader.pages:
16
+ text += page.extract_text()
17
+ return text
18
+
19
+ # Function to extract text from Word document
20
+ def extract_text_from_word(uploaded_file):
21
+ text = ""
22
+ doc = Document(uploaded_file)
23
+ for paragraph in doc.paragraphs:
24
+ text += paragraph.text + "\n"
25
+ return text
26
+
27
+
28
+
29
+ # Initialize Google Generative AI chat model
30
+ def initialize_chat_model():
31
+ with open("key.txt", "r") as f:
32
+ GOOGLE_API_KEY = f.read().strip()
33
+
34
+ chat_model = ChatGoogleGenerativeAI(
35
+ google_api_key=GOOGLE_API_KEY,
36
+ model="gemini-1.5-pro-latest",
37
+ temperature=0.4,
38
+ max_tokens=2000,
39
+ timeout=120,
40
+ max_retries=5,
41
+ top_p=0.9,
42
+ top_k=40,
43
+ presence_penalty=0.6,
44
+ frequency_penalty=0.3
45
+ )
46
+ return chat_model
47
+
48
+ chat_model = initialize_chat_model()
49
+
50
+ # Create Chat Template
51
+ chat_prompt_template = ChatPromptTemplate.from_messages(
52
+ [
53
+ SystemMessage(
54
+ content=""" You are a language model designed to follow user instructions exactly as given.
55
+ Do not take any actions or provide any information unless specifically directed by the user.
56
+ Your role is to fulfill the user's requests precisely without deviating from the instructions provided."""
57
+ ),
58
+ MessagesPlaceholder(variable_name="chat_history"),
59
+ HumanMessagePromptTemplate.from_template("{human_input}")
60
+ ]
61
+ )
62
+
63
+ # Initialize the Memory
64
+ memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
65
+
66
+ # Create an Output Parser
67
+ output_parser = StrOutputParser()
68
+
69
+ # Define a chain
70
+ chain = RunnablePassthrough.assign(
71
+ chat_history=RunnableLambda(lambda human_input: memory.load_memory_variables(human_input)['chat_history'])
72
+ ) | chat_prompt_template | chat_model | output_parser
73
+
74
+ # Streamlit App
75
+ st.title("Interview Preparation with AI")
76
+ st.markdown("## Part-1: Upload Files, Summarize, and Extract Keywords")
77
+
78
+ # File upload section
79
+ file1 = st.file_uploader("Upload your resume (PDF or DOCX):", type=["pdf", "docx"])
80
+ file2 = st.file_uploader("Upload the job description (PDF or DOCX):", type=["pdf", "docx"])
81
+
82
+ if file1 and file2:
83
+ try:
84
+ # Detect file type and extract text for file 1
85
+ if file1.name.endswith('.pdf'):
86
+ text1 = extract_text_from_pdf(file1)
87
+ elif file1.name.endswith('.docx'):
88
+ text1 = extract_text_from_word(file1)
89
+ else:
90
+ st.error("Unsupported file type for file 1")
91
+
92
+ # Detect file type and extract text for file 2
93
+ if file2.name.endswith('.pdf'):
94
+ text2 = extract_text_from_pdf(file2)
95
+ elif file2.name.endswith('.docx'):
96
+ text2 = extract_text_from_word(file2)
97
+ else:
98
+ st.error("Unsupported file type for file 2")
99
+
100
+
101
+ # Ensure session state variables are initialized
102
+
103
+ if "ats_score_calculated" not in st.session_state:
104
+ st.session_state.ats_score_calculated = False
105
+
106
+
107
+ # Button to Calculate ATS Score
108
+ if st.button("ATS Score") or st.session_state.ats_score_calculated:
109
+
110
+ st.session_state.ats_score_calculated = True
111
+ resume_keywords = set(keywords1)
112
+ job_description_keywords = set(keywords2)
113
+ st.markdown("### ATS Score Calculation")
114
+ query = {"human_input": f"""
115
+ You are an advanced Applicant Tracking System (ATS) designed to evaluate resumes against job descriptions with exceptional accuracy. Analyze the following keywords extracted from a job description and a resume, compare them, and calculate the match percentage.
116
+ Job Description Keywords:
117
+ {list(job_description_keywords)}
118
+ Resume Keywords:
119
+ {list(resume_keywords)}
120
+ Provide the ATS score as a percentage match between the resume and the job description in the following format:
121
+ The ATS Score of your Resume According to the Job Description is \"XX%\".
122
+ """}
123
+
124
+ response = chain.invoke(query)
125
+ memory.save_context(query, {"output": response})
126
+
127
+ st.write(response)
128
+
129
+
130
+ except Exception as e:
131
+ st.error(f"An error occurred: {e}")
132
+
133
+ else:
134
+ st.info("Please upload both files to proceed.")