Spaces:
Sleeping
Sleeping
Commit
·
9167e85
1
Parent(s):
5c9072c
Refactor app.py to streamline email generation process by removing unused memory initialization and integrating email history tracking. Added session state management for email history and improved display of previous email generations. Cleaned up comments and enhanced code readability.
Browse files
app.py
CHANGED
@@ -1,6 +1,4 @@
|
|
1 |
import streamlit as st
|
2 |
-
from langchain.chains import ConversationChain
|
3 |
-
from langchain.memory import ConversationBufferMemory
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
5 |
import PyPDF2
|
6 |
from dotenv import load_dotenv
|
@@ -9,7 +7,7 @@ import os
|
|
9 |
# Load environment variables from .env
|
10 |
load_dotenv()
|
11 |
|
12 |
-
#
|
13 |
access_token = os.getenv("API_KEY")
|
14 |
|
15 |
# Streamlit App Title
|
@@ -28,6 +26,9 @@ uploaded_file = st.sidebar.file_uploader("Upload your CV (PDF format):", type=["
|
|
28 |
if "parsed_cv" not in st.session_state:
|
29 |
st.session_state.parsed_cv = None
|
30 |
|
|
|
|
|
|
|
31 |
if uploaded_file is not None:
|
32 |
try:
|
33 |
# Extract text from PDF
|
@@ -37,7 +38,6 @@ if uploaded_file is not None:
|
|
37 |
|
38 |
# Parse CV details and save to session state
|
39 |
def parse_cv(cv_text):
|
40 |
-
# Basic parsing logic (can be extended for specific details)
|
41 |
return f"""
|
42 |
Name: [Extracted Name]
|
43 |
Contact Information: [Extracted Contact Info]
|
@@ -72,9 +72,9 @@ if access_token:
|
|
72 |
model=model,
|
73 |
tokenizer=tokenizer,
|
74 |
max_new_tokens=512,
|
75 |
-
temperature=0.7,
|
76 |
-
do_sample=True,
|
77 |
-
top_p=0.95
|
78 |
)
|
79 |
except Exception as e:
|
80 |
st.error(f"Failed to initialize the model: {str(e)}")
|
@@ -82,14 +82,6 @@ if access_token:
|
|
82 |
|
83 |
text_gen_pipeline = initialize_pipeline(access_token)
|
84 |
|
85 |
-
@st.cache_resource
|
86 |
-
def initialize_memory():
|
87 |
-
return ConversationBufferMemory(
|
88 |
-
input_key="input",
|
89 |
-
output_key="output",
|
90 |
-
return_messages=True
|
91 |
-
)
|
92 |
-
|
93 |
# Input job description
|
94 |
job_description = st.text_area("Enter the job description:", "")
|
95 |
|
@@ -97,8 +89,6 @@ if access_token:
|
|
97 |
if st.button("Generate Email"):
|
98 |
if st.session_state.parsed_cv and job_description.strip():
|
99 |
try:
|
100 |
-
memory = initialize_memory()
|
101 |
-
|
102 |
# Improved prompt template
|
103 |
prompt = f"""Task: Write a professional job application email.
|
104 |
|
@@ -114,7 +104,6 @@ Keep the tone professional and enthusiastic.
|
|
114 |
|
115 |
Email:
|
116 |
"""
|
117 |
-
|
118 |
# Generate email using the pipeline
|
119 |
if text_gen_pipeline:
|
120 |
response = text_gen_pipeline(
|
@@ -123,20 +112,23 @@ Email:
|
|
123 |
return_full_text=False
|
124 |
)[0]['generated_text']
|
125 |
|
126 |
-
#
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
)
|
131 |
|
132 |
# Display response
|
133 |
st.subheader("Generated Email:")
|
134 |
st.write(response)
|
135 |
|
136 |
# Display conversation history
|
137 |
-
st.
|
138 |
-
|
139 |
-
st.
|
|
|
|
|
|
|
140 |
else:
|
141 |
st.error("Text generation pipeline not properly initialized.")
|
142 |
except Exception as e:
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
import PyPDF2
|
4 |
from dotenv import load_dotenv
|
|
|
7 |
# Load environment variables from .env
|
8 |
load_dotenv()
|
9 |
|
10 |
+
# API Key
|
11 |
access_token = os.getenv("API_KEY")
|
12 |
|
13 |
# Streamlit App Title
|
|
|
26 |
if "parsed_cv" not in st.session_state:
|
27 |
st.session_state.parsed_cv = None
|
28 |
|
29 |
+
if "email_history" not in st.session_state:
|
30 |
+
st.session_state.email_history = []
|
31 |
+
|
32 |
if uploaded_file is not None:
|
33 |
try:
|
34 |
# Extract text from PDF
|
|
|
38 |
|
39 |
# Parse CV details and save to session state
|
40 |
def parse_cv(cv_text):
|
|
|
41 |
return f"""
|
42 |
Name: [Extracted Name]
|
43 |
Contact Information: [Extracted Contact Info]
|
|
|
72 |
model=model,
|
73 |
tokenizer=tokenizer,
|
74 |
max_new_tokens=512,
|
75 |
+
temperature=0.7,
|
76 |
+
do_sample=True,
|
77 |
+
top_p=0.95
|
78 |
)
|
79 |
except Exception as e:
|
80 |
st.error(f"Failed to initialize the model: {str(e)}")
|
|
|
82 |
|
83 |
text_gen_pipeline = initialize_pipeline(access_token)
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
# Input job description
|
86 |
job_description = st.text_area("Enter the job description:", "")
|
87 |
|
|
|
89 |
if st.button("Generate Email"):
|
90 |
if st.session_state.parsed_cv and job_description.strip():
|
91 |
try:
|
|
|
|
|
92 |
# Improved prompt template
|
93 |
prompt = f"""Task: Write a professional job application email.
|
94 |
|
|
|
104 |
|
105 |
Email:
|
106 |
"""
|
|
|
107 |
# Generate email using the pipeline
|
108 |
if text_gen_pipeline:
|
109 |
response = text_gen_pipeline(
|
|
|
112 |
return_full_text=False
|
113 |
)[0]['generated_text']
|
114 |
|
115 |
+
# Save response in history
|
116 |
+
st.session_state.email_history.append({
|
117 |
+
"job_description": job_description,
|
118 |
+
"email": response
|
119 |
+
})
|
120 |
|
121 |
# Display response
|
122 |
st.subheader("Generated Email:")
|
123 |
st.write(response)
|
124 |
|
125 |
# Display conversation history
|
126 |
+
if st.session_state.email_history:
|
127 |
+
st.subheader("Previous Generations:")
|
128 |
+
for idx, entry in enumerate(st.session_state.email_history, 1):
|
129 |
+
st.write(f"### Email {idx}")
|
130 |
+
st.write(f"**Job Description:** {entry['job_description']}")
|
131 |
+
st.write(f"**Generated Email:** {entry['email']}")
|
132 |
else:
|
133 |
st.error("Text generation pipeline not properly initialized.")
|
134 |
except Exception as e:
|