deepakchawla-cb commited on
Commit
da93d60
·
1 Parent(s): 65bfa0d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from io import StringIO
4
+ import os,re
5
+ from langchain.llms import OpenAI
6
+ import pandas as pd
7
+
8
+ from langchain.document_loaders import UnstructuredPDFLoader
9
+
10
+ from langchain.prompts import PromptTemplate
11
+ from langchain.chains import LLMChain
12
+
13
+ from langchain.embeddings.openai import OpenAIEmbeddings
14
+ from langchain.vectorstores import Chroma
15
+ from langchain.text_splitter import CharacterTextSplitter
16
+ from langchain.llms import OpenAI
17
+ from langchain.chains import RetrievalQA
18
+ from langchain.document_loaders import TextLoader
19
+ from langchain.prompts import PromptTemplate
20
+
21
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
22
+
23
+
24
+ def predict(file_obj):
25
+ loader = UnstructuredPDFLoader(file_obj.orig_name)
26
+ data = loader.load()
27
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
28
+ texts = text_splitter.split_documents(data)
29
+
30
+ embeddings = OpenAIEmbeddings()
31
+ docsearch = Chroma.from_documents(texts, embeddings)
32
+ qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="map_reduce", retriever=docsearch.as_retriever())
33
+
34
+ prompt_template = """Ignore all previous instructions. You are the world's best interviewer now. I am going to give you a resume of a candidate. Analyze the resume in 4 categories: Education, Work Experience, Projects and Internships, Others including interests, skills etc. Be simple, direct and commanding. Start with greeting the candidate with a 2 line relatable introduction emphasizing your superiority. Ask the candidate if they have a particular company and a role that they want to apply for.
35
+ If the candidate mentions either the company or the role, then ensure all questions that would be asked will are related to it.
36
+ If they don't mention either the company or role clearly, then ignore this and move to the next step.
37
+ Then, give a one line response acknowledging the candidate or if they are not clear about the company or the role then acknowledge positively that you would ask practice interview questions. Then ask the candidate topic would they like to start with. There are 4 categories of questions: educational background related, role related or technical questions, behavioral questions and HR or culture related questions. Here, the candidate will have to put an input.
38
+ Now you will have to ask interview questions. Ensure the questions are good have test the candidate's knowledge. You can choose between longer case based questions, hypothetical questions or academic questions etc. as you deem fit.
39
+ If the candidate types educational background related, ask it 3-4 most relevant questions related to their education based on their resume which are relevant for the role or the company.
40
+ If the candidate types role related or technical related then ask accordingly. Here you can even ask a coding question or test their technical understanding etc.
41
+ Similarly, do it for behavioral questions and HR or culture related questions. You can also be creative, funny, or show emotions at time.
42
+ {context}
43
+ Question: {question}
44
+ Answer in possible questions for interview:"""
45
+ PROMPT = PromptTemplate(
46
+ template=prompt_template, input_variables=["context", "question"]
47
+ )
48
+ chain_type_kwargs = {"prompt": PROMPT}
49
+ qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=docsearch.as_retriever(), chain_type_kwargs=chain_type_kwargs)
50
+ response = []
51
+ category = ["Technical", "Education Background", "Behaviour", "Project Specific"]
52
+ for value in category:
53
+
54
+ response.append({value:ai(qa, value)})
55
+
56
+ html_output = ""
57
+ for obj in response:
58
+ # Loop through the key-value pairs in the object
59
+ for key, value in obj.items():
60
+ value = re.sub(r'[\d\.]+', '', value)
61
+ value_list = value.strip().split('\n')
62
+ value_html = "<ol>"
63
+ for item in value_list:
64
+ value_html += "<li>{}</li>".format(item.strip())
65
+ value_html += "</ol>"
66
+ html_output += "<h2>{}</h2>".format(key)
67
+ html_output += value_html
68
+
69
+
70
+ return html_output
71
+
72
+ def ai(qa,category):
73
+ query = "please suggest "+ category +" interview questions"
74
+ data = list(filter(None, qa.run(query).split('\n')))
75
+ results = list(filter(lambda x: x != ' ', data))
76
+ results = "\n".join(results)
77
+
78
+ return results
79
+
80
+ title = "AI-Interviewer"
81
+
82
+ description = "Our app uses the latest technology to scan your resume, and generates personalized interview questions based on your qualifications and experience. With Resume-to-Interview, you can focus on answering questions that are tailored to your specific skills, rather than wasting time memorizing generic interview questions."
83
+
84
+
85
+
86
+ iface = gr.Interface(fn =predict,
87
+ inputs = [gr.File(type="file",label="upload resume pdf file only",file_types=[".pdf"])],
88
+ outputs = [
89
+ gr.HTML()
90
+ ,
91
+ ],allow_flagging="never", title=title, description=description, article=article)
92
+
93
+ iface.launch(debug=True)