Dhahlan2000 commited on
Commit
10493d1
·
1 Parent(s): 8ac5d88

Enhance .gitignore to include additional Python, environment, IDE, and model file exclusions. Refactor app.py to improve text generation pipeline initialization with better error handling and enhanced prompt structure for email generation. Update requirements.txt to specify minimum versions for dependencies, ensuring compatibility and stability.

Browse files
Files changed (3) hide show
  1. .gitignore +42 -1
  2. app.py +70 -33
  3. requirements.txt +6 -6
.gitignore CHANGED
@@ -1,2 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  .env
2
- .venv
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ env/
8
+ build/
9
+ develop-eggs/
10
+ dist/
11
+ downloads/
12
+ eggs/
13
+ .eggs/
14
+ lib/
15
+ lib64/
16
+ parts/
17
+ sdist/
18
+ var/
19
+ wheels/
20
+ *.egg-info/
21
+ .installed.cfg
22
+ *.egg
23
+
24
+ # Environment variables
25
  .env
26
+
27
+ # Virtual Environment
28
+ venv/
29
+ ENV/
30
+
31
+ # IDE
32
+ .idea/
33
+ .vscode/
34
+ *.swp
35
+ *.swo
36
+
37
+ # Streamlit
38
+ .streamlit/
39
+
40
+ # Model files
41
+ *.pt
42
+ *.pth
43
+ *.bin
app.py CHANGED
@@ -60,22 +60,35 @@ if st.session_state.parsed_cv:
60
  if access_token:
61
  @st.cache_resource
62
  def initialize_pipeline(access_token):
63
- tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it", token=access_token)
64
- model = AutoModelForCausalLM.from_pretrained(
65
- "google/gemma-2b-it",
66
- torch_dtype="bfloat16",
67
- token=access_token
68
- )
69
- return pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512)
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  text_gen_pipeline = initialize_pipeline(access_token)
72
 
73
  @st.cache_resource
74
- def initialize_chain():
75
- memory = ConversationBufferMemory()
76
- return ConversationChain(llm=text_gen_pipeline, memory=memory) # No LLM; handled by pipeline
77
-
78
- conversation_chain = initialize_chain()
 
79
 
80
  # Input job description
81
  job_description = st.text_area("Enter the job description:", "")
@@ -83,27 +96,51 @@ if access_token:
83
  # Display generated email
84
  if st.button("Generate Email"):
85
  if st.session_state.parsed_cv and job_description.strip():
86
- # Prompt for email generation
87
- prompt = (
88
- f"Based on the following CV details:\n\n{st.session_state.parsed_cv}\n\n"
89
- f"And the following job description:\n\n{job_description}\n\n"
90
- f"Write a professional email expressing interest in the job. "
91
- f"Make it concise, polite, and tailored to the job."
92
- )
93
-
94
- # Generate email using Hugging Face pipeline
95
- response = text_gen_pipeline(prompt)[0]['generated_text']
96
-
97
- # Update memory with job description and response
98
- conversation_chain.memory.save_context({"job_description": job_description}, {"email": response})
99
-
100
- # Display response
101
- st.subheader("Generated Email:")
102
- st.write(response)
103
-
104
- # Display conversation history
105
- st.subheader("History:")
106
- st.write(conversation_chain.memory.buffer)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  else:
108
  st.warning("Please upload your CV in the sidebar and enter a job description.")
109
  else:
 
60
  if access_token:
61
  @st.cache_resource
62
  def initialize_pipeline(access_token):
63
+ try:
64
+ tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it", token=access_token)
65
+ model = AutoModelForCausalLM.from_pretrained(
66
+ "google/gemma-2b-it",
67
+ token=access_token,
68
+ device_map="auto" # Better device handling
69
+ )
70
+ return pipeline(
71
+ "text-generation",
72
+ model=model,
73
+ tokenizer=tokenizer,
74
+ max_new_tokens=512,
75
+ temperature=0.7, # Add temperature for better generation
76
+ do_sample=True, # Enable sampling
77
+ top_p=0.95 # Add top_p for better text quality
78
+ )
79
+ except Exception as e:
80
+ st.error(f"Failed to initialize the model: {str(e)}")
81
+ return None
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:", "")
 
96
  # Display generated email
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
+
105
+ CV Summary:
106
+ {st.session_state.parsed_cv}
107
+
108
+ Job Description:
109
+ {job_description}
110
+
111
+ Instructions: Write a concise and professional email expressing interest in the position.
112
+ Highlight relevant experience and skills from the CV that match the job requirements.
113
+ 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(
121
+ prompt,
122
+ clean_up_tokenization_spaces=True,
123
+ return_full_text=False
124
+ )[0]['generated_text']
125
+
126
+ # Store in memory
127
+ memory.save_context(
128
+ {"input": f"Job Description: {job_description}"},
129
+ {"output": response}
130
+ )
131
+
132
+ # Display response
133
+ st.subheader("Generated Email:")
134
+ st.write(response)
135
+
136
+ # Display conversation history
137
+ st.subheader("Previous Generations:")
138
+ for entry in memory.load_memory_variables({})['history']:
139
+ st.write(entry)
140
+ else:
141
+ st.error("Text generation pipeline not properly initialized.")
142
+ except Exception as e:
143
+ st.error(f"Error generating email: {str(e)}")
144
  else:
145
  st.warning("Please upload your CV in the sidebar and enter a job description.")
146
  else:
requirements.txt CHANGED
@@ -1,8 +1,8 @@
1
  # add requirements
2
- streamlit
3
- python-dotenv
4
- langchain
5
- transformers
6
- torch
7
- PyPDF2
8
 
 
1
  # add requirements
2
+ streamlit>=1.24.0
3
+ python-dotenv>=0.19.0
4
+ langchain>=0.1.0
5
+ transformers>=4.36.0
6
+ torch>=2.0.0
7
+ PyPDF2>=3.0.0
8