-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfparse.py
68 lines (52 loc) · 2.19 KB
/
pdfparse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
from dotenv import load_dotenv
import streamlit as st
import PyPDF2
import cohere
from dotenv import load_dotenv
#Load env variables from .env file
load_dotenv()
# Initialize Cohere API (make sure to use your actual API key)
cohere_client = cohere.Client(os.getenv('cohere-api-key'))
# Define the function to extract text from Resume PDF
def extract_text_from_pdf(pdf_file):
reader = PyPDF2.PdfReader(pdf_file)
text = ""
for page in reader.pages:
text += page.extract_text() or ""
return text
# Function to generate response from Cohere
def generate_response(prompt):
response = cohere_client.generate(
model='command-xlarge-nightly',
prompt=prompt,
max_tokens=1500,
)
return response.generations[0].text.strip()
# Chatbot interface using Streamlit
st.title("RESUME CHATBOT")
st.write("Upload your resume to start a conversation")
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
if uploaded_file:
resume_text = extract_text_from_pdf(uploaded_file)
st.write("Resume Uploaded. Here's a preview:")
st.text_area("Resume Text", resume_text, height=200)
# Initialize chat context
if "context" not in st.session_state:
st.session_state["context"] = []
# Chat input
user_input = st.text_input("Ask about the candidate:")
if user_input:
# Append the user input to the context
st.session_state["context"].append({"user": user_input})
# Construct the prompt using the resume text and the context
# Here the context prompt section will account for incorporating the chat history context to further responses
context_prompt = "\n".join([f":User {entry['user']}\nBot: {entry.get('bot', '')}" for entry in st.session_state["context"]])
prompt = f"Here is the resume:\n\n{resume_text}\n\n{context_prompt}\nUser question: {user_input}\n\nBot reply:"
# Generate the bot's reply
bot_reply = generate_response(prompt)
# Append the bot's reply to the context
st.session_state["context"][-1]["bot"] = bot_reply
# Display the bot's reply
st.write(f"Bot: {bot_reply}")
# st.write("Context:", st.session_state["context"])