CoolFace
Apppublic

BiplabSil/AI-Interviewer

sourceHugging Facemitupdated 1y agoView on Hugging Face
1likes
app.py167 linesDownload Raw Back to root
1from dotenv import load_dotenv
2load_dotenv()
3import os
4os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY')
5os.environ['TAVILY_API_KEY'] = os.getenv('TAVILY_API_KEY')
6
7# from langchain.chat_models import init_chat_model
8# llm = init_chat_model('llama-3.3-70b-versatile', model_provider = 'groq')
9
10from langchain_google_genai import ChatGoogleGenerativeAI
11llm = ChatGoogleGenerativeAI(model = "gemini-2.5-flash")
12
13# from langchain_tavily import TavilySearch
14# search_tool = TavilySearch()
15# tools = [search_tool]
16
17from langchain_community.tools import DuckDuckGoSearchResults
18search_tool = DuckDuckGoSearchResults()
19tools = [search_tool]
20
21llm_with_tool = llm.bind_tools(tools)
22
23# from langchain_community.document_loaders import TextLoader
24# loader = TextLoader('../Dataset/web_dump.txt')
25# urls = loader.load()
26urls = [
27    'https://www.datacamp.com/blog/genai-interview-questions',
28    'https://www.geeksforgeeks.org/artificial-intelligence/generative-ai-interview-question-with-answer/',
29    'https://faun.pub/complete-llm-genai-interview-guide-50-essential-questions-answers-0da9f126cb68',
30    'https://bgiri-gcloud.medium.com/generative-ai-engineer-interview-questions-and-how-to-prepare-for-interview-3a9dcded1628',
31    'https://www.gsdcouncil.org/blogs/top-10-generative-ai-interview-questions-you-must-prepare-for',
32    'https://generativeaimasters.in/generative-ai-interview-questions/',
33    'https://timespro.com/blog/generative-ai-interview-questions',
34
35]
36
37from langchain_core.prompts import ChatPromptTemplate
38prompt_template = ChatPromptTemplate.from_messages([
39    ('system','''You are an interviewer to help to prepare interview questionnaries,
40      prepare just 1 short interview question on Generative AI technology, no answer or explanation is required.
41     goal is to understant candiates understanding on generative AI.
42     your question should include either any one of the topics like LLM,NLP,AI Agent, Agentic AI, RAG, Tools, huggingface, Langchain and more.'''),
43    ('human','{input}')
44])
45
46chain = prompt_template|llm_with_tool
47
48from typing import Annotated
49from typing_extensions import TypedDict, Optional
50from langgraph.graph import StateGraph, START, END
51from langgraph.graph.message import add_messages
52from langgraph.prebuilt import tool_node, tools_condition
53from langgraph.types import Command, interrupt
54from langgraph.checkpoint.memory import MemorySaver
55# Add a checkpointer
56checkpointer = MemorySaver()
57
58class State(TypedDict):
59    messages: Annotated[list, add_messages]
60    ready : Optional[str]
61    question_1: Optional[str]
62    question_2: Optional[str]
63    question_3: Optional[str]
64    question_4: Optional[str]
65    question_5: Optional[str]
66    answer_1: Optional[str]
67    answer_2: Optional[str]
68    answer_3: Optional[str]
69    answer_4: Optional[str]
70    answer_5: Optional[str]
71
72
73graph_builder = StateGraph(State)
74
75#chatbot-1
76def interviewer_1(state: State):
77    if 'ready' not in state or not state['ready']:
78        ready = interrupt("Hello Candidate, are you ready for the interview now?")
79        state['ready'] = ready
80        #return {"messages":"Great, let's start.."}
81
82#chatbot-2
83def questionnaires_builder(state: State):
84    question1 = chain.invoke({"input":"prepare 1 very basic question for my Generative AI interview to start the interview"})
85    #return {"messages": [chain.invoke({"input":"prepare 2 question and answers set for my Generative AI interview"})]}
86    #print(question.content)
87    if 'question_1' not in state or not state['question_1']:
88        state['question_1'] = question1.content
89        answer_1 = interrupt(state['question_1'])
90        state['answer_1'] = answer_1
91   
92    question2 = chain.invoke({"input":"prepare 1 question for my Generative AI interview, this is the 2nd question and should be moderate level."})
93    if 'question_2' not in state or not state['question_2']:
94        state['question_2'] = question2.content
95        answer_2 = interrupt("Good, Your answer is recored, here is your second question.\n"+state['question_2'])
96        state['answer_2'] = answer_2
97        
98    question3 = chain.invoke({"input":"prepare 1 question for my Generative AI interview, this is the 3rd question and should be moderate level."})
99    if 'question_3' not in state or not state['question_3']:
100        state['question_3'] = question3.content
101        answer_3 = interrupt("Good, Your answer is recored, here is your third question.\n"+state['question_3'])
102        state['answer_3'] = answer_3
103        return state
104    # question4 = chain.invoke({"input":"prepare 1 question for my Generative AI interview, this is the 4th question and should be moderate to difficult level."})
105    # if 'question_4' not in state or not state['question_4']:
106    #     state['question_4'] = question4.content
107    #     answer_4 = interrupt("Good, Your answer is recored, here is your fourth question.\n"+state['question_4'])
108    #     state['answer_4'] = answer_4
109    # question5 = chain.invoke({"input":"prepare 1 question for my Generative AI interview, this is the final question and should be moderate to difficult level."})
110    # if 'question_5' not in state or not state['question_5']:
111    #     state['question_5'] = question5.content
112    #     answer_5 = interrupt("Good, Your answer is recored, here is your final question.\n"+state['question_5'])
113    #     state['answer_5'] = answer_5
114    #     return {"messages": "Good, Your answer is recorded, will evaluate at the end."}
115
116graph_builder.add_node('questionnaires_builder', questionnaires_builder)
117
118
119graph_builder.add_node('interviewer_1', interviewer_1)
120
121#chatbot-3
122def score_provider(state:State):
123    score = 0
124    for i in range(1,4):
125        q = 'question_'+str(i)
126        print('question: ',q)
127        a = 'answer_'+str(i)
128        print('answer: ',a)
129        score = 0
130        score_prompt = ChatPromptTemplate.from_messages([
131            ("system", "You are an interviewer. you have the question: {} and answer: {}, based on your knowledge evaluate the answer logically to validate if the user provided answer is conceptually correct, if correct or partially corrrect retuen an interger score out of 100, return only the score number , no other string.".format(state[q],state[a]['messages'])),
132            ("user","{input}")
133        ])
134        score_chain = score_prompt|llm
135        s = score_chain.invoke({'input':'give the score.'})
136        print('here is s:',s.content)
137        score =score+int(s.content)
138    if int(score)>60:
139        return {"messages": f'Your total score is {score}. Status : Passed'}
140    else:
141        return {"messages": f'Your total score is {score}. Status : Failed, Need Improvement.'}
142
143from langgraph.prebuilt import ToolNode, tools_condition
144tool_node = ToolNode(tools)
145# graph_builder.add_node("Search_Tool", tool_node)
146
147graph_builder.add_node('score_provider',score_provider)
148
149graph_builder.add_edge(START, 'interviewer_1')
150graph_builder.add_edge('interviewer_1', 'questionnaires_builder')
151# graph_builder.add_conditional_edges(
152#      "questionnaires_builder",
153#     tools_condition,
154#     {"tools":"Search_Tool","__end__":END}
155# )
156# graph_builder.add_edge('Search_Tool', 'questionnaires_builder')
157
158graph_builder.add_edge('questionnaires_builder','score_provider')
159graph_builder.add_edge('score_provider', END)
160
161graph = graph_builder.compile(checkpointer = checkpointer)
162
163# res = graph.invoke(input={"messaages":"hi"})
164
165# print(res)
166
167