from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.messages import HumanMessage from langchain_core.messages import AIMessage from langchain_community.chat_message_histories import ChatMessageHistory from pypdf import PdfReader import os import gradio as gr from langchain_openai import AzureOpenAI client = AzureOpenAI( api_key=os.getenv("API"), api_version="2023-07-01-preview", azure_endpoint=os.getenv("URL"), azure_deployment = "gpt-4o" ) def extract_text( pdf_path): # creating a pdf reader object reader = PdfReader(pdf_path) all_text = "" for page in reader.pages: all_text += page.extract_text() return all_text def get_response( candidate, chat_history, resume, jd): resume = extract_text(resume.name) jd = extract_text(jd.name) prompt = ChatPromptTemplate.from_messages( [ ( "system", """Your Task is Perform as intelligent interviewer, Your Task is ask question to the resume's candidate by following candidate Answer. at the end exit with greeting to the candidate. **Ask question follow up on the candidate response. get chat history.** """, ), MessagesPlaceholder(variable_name="messages"), ] ) chain = prompt | client # chat_histroy_prompt = chat_history answer = chain.invoke( { "messages": [ HumanMessage( content=f" job description :{jd}\n Resume :{resume}" ), AIMessage(content=f"""Perform as intelligent interviewer, Your Task is ask question to the resume's candidate by following candidate Answer. chat history : {chat_history}"""), HumanMessage(content=candidate), ], } ) # print("INTERVIEWER :", answer.content) # chat_history.append({"candidate":candidate,"interviewer":answer.content }) result = answer.content chat_history.append((candidate, result)) print("chat_history", chat_history) return "", chat_history def gradio_interface() -> None: """Create a Gradio interface for the chatbot.""" with gr.Blocks(css = "style.css" ,theme="shivi/calm_seafoam") as demo: gr.HTML("""
LOGO

Screening Assistant Chatbot

""") with gr.Row(): with gr.Column(scale=0.80): chatbot = gr.Chatbot() with gr.Column(scale=0.20): with gr.Row(): resume = gr.File(label="Resume") with gr.Row(): jd = gr.File(label="Job Description") with gr.Row(): with gr.Column(scale=0.80): msg = gr.Textbox(label="Question") with gr.Column(scale=0.20): clear = gr.ClearButton([msg, chatbot]) msg.submit(get_response, [msg, chatbot, resume, jd], [msg, chatbot]) demo.launch(debug =True, share=True) gradio_interface()