Sobit commited on
Commit
8179b6a
·
verified ·
1 Parent(s): 64f2763

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -243
app.py CHANGED
@@ -1,255 +1,139 @@
1
  import streamlit as st
 
2
  from langchain.chains import LLMChain
3
  from langchain.prompts import PromptTemplate
4
- from langchain.llms import HuggingFaceHub
5
- import fitz # PyMuPDF for PDF extraction
6
- from PIL import Image
7
- import os
8
- import pytesseract
9
- import re
10
-
11
- # Set Hugging Face API Key (Set this in Hugging Face Secrets)
12
- os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
13
-
14
- # Load Free LLM from Hugging Face
15
- llm = HuggingFaceHub(repo_id="mistralai/Mistral-7B-Instruct-v0.3", model_kwargs={"temperature": 0.5})
16
-
17
- # Streamlit App Configuration
18
- st.set_page_config(page_title="DocuMentorAI", layout="wide", page_icon="📄")
19
- st.title("📄 DocuMentorAI")
20
- st.write("Generate professional application documents with ease!")
21
-
22
- # Custom CSS for better UI
23
- st.markdown("""
24
- <style>
25
- .stTextArea textarea { font-size: 16px !important; }
26
- .stButton button { width: 100%; background-color: #4CAF50; color: white; }
27
- .stDownloadButton button { width: 100%; background-color: #008CBA; color: white; }
28
- .stMarkdown { font-size: 18px; }
29
- </style>
30
- """, unsafe_allow_html=True)
31
-
32
- # Text Input for Job Opening Details
33
- st.subheader("📢 Enter Opening Details")
34
- job_opening_text = st.text_area(
35
- "Paste the job/research opening details here...",
36
- height=150,
37
- placeholder="Example: 'We are hiring a Research Assistant at XYZ University. The ideal candidate has experience in machine learning and data analysis...'"
38
  )
39
-
40
- # Upload CV/Resume
41
- st.subheader("📄 Upload CV/Resume")
42
- cv_resume_file = st.file_uploader(
43
- "Upload your CV/Resume (PDF or Image)",
44
- type=["pdf", "png", "jpg", "jpeg"],
45
- help="Upload a PDF or image of your CV/Resume for text extraction."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  )
47
 
48
- # Function to extract text from PDF
49
- def extract_text_from_pdf(pdf_file):
50
- pdf_bytes = pdf_file.read()
51
- with fitz.open(stream=pdf_bytes, filetype="pdf") as doc:
52
- return " ".join([page.get_text() for page in doc])
53
-
54
- # Function to extract text from Image using OCR
55
- def extract_text_from_image(image_file):
56
- image = Image.open(image_file)
57
- return pytesseract.image_to_string(image)
58
-
59
- # Function to extract text from uploaded files
60
- def extract_text(uploaded_file):
61
- if uploaded_file:
62
- file_type = uploaded_file.type
63
- if file_type == "application/pdf":
64
- return extract_text_from_pdf(uploaded_file)
65
- else:
66
- return extract_text_from_image(uploaded_file)
67
- return ""
68
-
69
- # Extract text from CV/Resume
70
- cv_resume_text = extract_text(cv_resume_file)
71
-
72
- # Display Extracted Text
73
- if job_opening_text:
74
- with st.expander("🔍 View Entered Opening Details"):
75
- st.markdown(f"**Job Opening Details:**\n\n{job_opening_text}")
76
-
77
- if cv_resume_text:
78
- with st.expander("🔍 View Extracted CV/Resume Details"):
79
- st.markdown(f"**CV/Resume Details:**\n\n{cv_resume_text}")
80
-
81
- # Function to extract professor name, designation, and university
82
- def extract_professor_details(text):
83
- professor_pattern = r"(Dr\.|Professor|Prof\.?)\s+([A-Z][a-z]+\s[A-Z][a-z]+)"
84
- university_pattern = r"(University|Institute|College|School of [A-Za-z]+)"
85
-
86
- professor_match = re.search(professor_pattern, text)
87
- university_match = re.search(university_pattern, text)
88
-
89
- professor_name = professor_match.group(0) if professor_match else "Not Found"
90
- university_name = university_match.group(0) if university_match else "Not Found"
91
-
92
- return professor_name, university_name
93
-
94
- # Extract professor details if job opening is uploaded
95
- professor_name, university_name = extract_professor_details(job_opening_text)
96
-
97
- # LLM Prompt Templates
98
- email_template = PromptTemplate.from_template("""
99
- Write a professional cold email for a research position.
100
- - Address the professor formally.
101
- - Introduce yourself and academic background.
102
- - Express interest in their research.
103
- - Highlight key skills from your CV.
104
- - Conclude with a polite request.
105
- ### Input:
106
- - Professor: {professor_name}
107
- - University: {university_name}
108
- - Research Interests: {research_interests}
109
- - Why This Lab: {reason}
110
- - CV Highlights: {resume_text}
111
- ### Output:
112
- A well-structured, professional cold email.
113
- """)
114
-
115
- cover_letter_template = PromptTemplate.from_template("""
116
- Write a compelling job application cover letter.
117
- - Address the employer formally.
118
- - Mention job title and where you found it.
119
- - Highlight key skills and experiences.
120
- - Relate background to the company.
121
- - Conclude with enthusiasm.
122
- ### Input:
123
- - Job Title: {job_title}
124
- - Company: {company}
125
- - Key Skills: {key_skills}
126
- - CV Highlights: {resume_text}
127
- ### Output:
128
- A strong, well-formatted cover letter.
129
- """)
130
-
131
- research_statement_template = PromptTemplate.from_template("""
132
- Write a research statement for Ph.D. applications.
133
- - Discuss research background and motivation.
134
- - Explain key research experiences and findings.
135
- - Outline future research interests and goals.
136
- - Highlight contributions to the field.
137
- ### Input:
138
- - Research Background: {research_background}
139
- - Key Research Projects: {key_projects}
140
- - Future Goals: {future_goals}
141
- ### Output:
142
- A well-structured, professional research statement.
143
- """)
144
-
145
- sop_template = PromptTemplate.from_template("""
146
- Write a compelling Statement of Purpose (SOP).
147
- - Introduce motivation for graduate studies.
148
- - Discuss academic background.
149
- - Explain relevant experiences and research.
150
- - Outline career goals.
151
- - Justify fit for the program.
152
- ### Input:
153
- - Motivation: {motivation}
154
- - Academic Background: {academic_background}
155
- - Research & Projects: {research_experiences}
156
- - Career Goals: {career_goals}
157
- - Why This Program: {why_this_program}
158
- ### Output:
159
- A well-structured SOP.
160
- """)
161
-
162
- # LangChain Chains
163
- email_chain = LLMChain(llm=llm, prompt=email_template)
164
- cover_letter_chain = LLMChain(llm=llm, prompt=cover_letter_template)
165
- research_statement_chain = LLMChain(llm=llm, prompt=research_statement_template)
166
- sop_chain = LLMChain(llm=llm, prompt=sop_template)
167
-
168
- # User Inputs
169
- st.subheader("📩 Generate Application Documents")
170
- tab1, tab2, tab3, tab4 = st.tabs(["Cold Email", "Cover Letter", "Research Statement", "SOP"])
171
-
172
- # Cold Email Generation
173
- with tab1:
174
- st.write(f"🧑‍🏫 **Detected Professor:** {professor_name} at {university_name}")
175
- research_interests = st.text_area("Research Interests", placeholder="Example: Machine Learning, Data Analysis, etc.")
176
- reason = st.text_area("Why this professor/lab?", placeholder="Example: I am particularly interested in your work on...")
177
-
178
- if st.button("Generate Cold Email"):
179
- if not job_opening_text or not cv_resume_text:
180
- st.error("Please provide job opening details and upload your CV/Resume.")
181
- else:
182
- with st.spinner("Generating Cold Email..."):
183
- email = email_chain.run({
184
- "professor_name": professor_name,
185
- "university_name": university_name,
186
- "research_interests": research_interests,
187
- "reason": reason,
188
- "resume_text": cv_resume_text
189
- })
190
- st.markdown("**Generated Cold Email:**")
191
- st.markdown(email)
192
- st.download_button("Download Email", email, file_name="cold_email.txt")
193
-
194
- # Cover Letter Generation
195
- with tab2:
196
- job_title = st.text_input("Job Title", placeholder="Example: Research Assistant")
197
- company_name = university_name if university_name != "Not Found" else st.text_input("Company/University", placeholder="Example: XYZ University")
198
- key_skills = st.text_area("Key Skills", placeholder="Example: Python, Machine Learning, Data Analysis")
199
 
200
  if st.button("Generate Cover Letter"):
201
- if not job_opening_text or not cv_resume_text:
202
- st.error("Please provide job opening details and upload your CV/Resume.")
203
- else:
204
- with st.spinner("Generating Cover Letter..."):
205
- cover_letter = cover_letter_chain.run({
206
- "job_title": job_title,
207
- "company": company_name,
208
- "key_skills": key_skills,
209
- "resume_text": cv_resume_text
210
- })
211
- st.markdown("**Generated Cover Letter:**")
212
- st.markdown(cover_letter)
213
- st.download_button("Download Cover Letter", cover_letter, file_name="cover_letter.txt")
214
-
215
- # Research Statement Generation
216
- with tab3:
217
- research_background = st.text_area("Research Background", placeholder="Example: My research focuses on...")
218
- key_projects = st.text_area("Key Research Projects", placeholder="Example: Developed a machine learning model for...")
219
- future_goals = st.text_area("Future Research Goals", placeholder="Example: I aim to explore...")
220
 
221
  if st.button("Generate Research Statement"):
222
- with st.spinner("Generating Research Statement..."):
223
- research_statement = research_statement_chain.run({
224
- "research_background": research_background,
225
- "key_projects": key_projects,
226
- "future_goals": future_goals
227
- })
228
- st.markdown("**Generated Research Statement:**")
229
- st.markdown(research_statement)
230
- st.download_button("Download Research Statement", research_statement, file_name="research_statement.txt")
231
-
232
- # SOP Generation
233
- with tab4:
234
- motivation = st.text_area("Motivation for Graduate Studies", placeholder="Example: I have always been passionate about...")
235
- academic_background = st.text_area("Academic Background", placeholder="Example: I completed my undergraduate degree in...")
236
- research_experiences = st.text_area("Research & Projects", placeholder="Example: During my undergraduate studies, I worked on...")
237
- career_goals = st.text_area("Career Goals", placeholder="Example: My long-term goal is to...")
238
- why_this_program = st.text_area("Why This Program", placeholder="Example: This program aligns with my research interests because...")
239
 
240
  if st.button("Generate SOP"):
241
- with st.spinner("Generating SOP..."):
242
- sop = sop_chain.run({
243
- "motivation": motivation,
244
- "academic_background": academic_background,
245
- "research_experiences": research_experiences,
246
- "career_goals": career_goals,
247
- "why_this_program": why_this_program
248
- })
249
- st.markdown("**Generated SOP:**")
250
- st.markdown(sop)
251
- st.download_button("Download SOP", sop, file_name="sop.txt")
252
-
253
- # Reset Button
254
- if st.button("🔄 Reset All Inputs and Outputs"):
255
- st.experimental_rerun()
 
1
  import streamlit as st
2
+ from langchain.chat_models import ChatOpenAI
3
  from langchain.chains import LLMChain
4
  from langchain.prompts import PromptTemplate
5
+ from langchain.memory import ConversationBufferMemory
6
+
7
+ # Initialize OpenAI LLM
8
+ llm = ChatOpenAI(model="gpt-4", temperature=0.7)
9
+
10
+ # Define Prompts
11
+ email_prompt = PromptTemplate(
12
+ input_variables=["professor_name", "university_name", "research_interests", "reason", "resume_text"],
13
+ template="""
14
+ Write a professional cold email to {professor_name} at {university_name} expressing interest in their research.
15
+ Highlight relevant research interests: {research_interests}.
16
+ Explain why you want to work with them: {reason}. Keep it concise and professional.
17
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  )
19
+ cover_letter_prompt = PromptTemplate(
20
+ input_variables=["job_title", "company", "key_skills", "resume_text"],
21
+ template="""
22
+ Write a professional cover letter for the {job_title} position at {company}.
23
+ Highlight key skills: {key_skills}. Keep it formal and tailored to the job.
24
+ """
25
+ )
26
+ research_statement_prompt = PromptTemplate(
27
+ input_variables=["research_background", "key_projects", "future_goals"],
28
+ template="""
29
+ Write a research statement based on the following:
30
+ - Background: {research_background}
31
+ - Key Projects: {key_projects}
32
+ - Future Research Goals: {future_goals}
33
+ Keep it structured and professional.
34
+ """
35
+ )
36
+ sop_prompt = PromptTemplate(
37
+ input_variables=["motivation", "academic_background", "research_experiences", "career_goals", "why_this_program"],
38
+ template="""
39
+ Write a Statement of Purpose (SOP) with:
40
+ - Motivation: {motivation}
41
+ - Academic Background: {academic_background}
42
+ - Research Experiences: {research_experiences}
43
+ - Career Goals: {career_goals}
44
+ - Why this program: {why_this_program}
45
+ Ensure it is compelling and well-structured.
46
+ """
47
  )
48
 
49
+ # Create LLM Chains
50
+ email_chain = LLMChain(llm=llm, prompt=email_prompt, memory=ConversationBufferMemory(memory_key="history"))
51
+ cover_letter_chain = LLMChain(llm=llm, prompt=cover_letter_prompt, memory=ConversationBufferMemory(memory_key="history"))
52
+ research_statement_chain = LLMChain(llm=llm, prompt=research_statement_prompt, memory=ConversationBufferMemory(memory_key="history"))
53
+ sop_chain = LLMChain(llm=llm, prompt=sop_prompt, memory=ConversationBufferMemory(memory_key="history"))
54
+
55
+ # Streamlit App
56
+ st.title("AI-Powered Academic & Job Application Generator")
57
+
58
+ # File Upload for Resume
59
+ st.sidebar.header("Upload Your Resume (Optional)")
60
+ uploaded_file = st.sidebar.file_uploader("Upload a .txt file", type=["txt"])
61
+
62
+ cv_resume_text = ""
63
+ if uploaded_file is not None:
64
+ cv_resume_text = uploaded_file.read().decode("utf-8")
65
+
66
+ # Tabs for Different Documents
67
+ tabs = st.tabs(["Cold Email", "Cover Letter", "Research Statement", "Statement of Purpose"])
68
+
69
+ # Cold Email Generator
70
+ with tabs[0]:
71
+ st.header("Generate a Cold Email")
72
+ professor_name = st.text_input("Professor's Name")
73
+ university_name = st.text_input("University Name")
74
+ research_interests = st.text_area("Your Research Interests")
75
+ reason = st.text_area("Why do you want to work with this professor?")
76
+
77
+ if st.button("Generate Email"):
78
+ email_output = email_chain.run({
79
+ "professor_name": professor_name,
80
+ "university_name": university_name,
81
+ "research_interests": research_interests,
82
+ "reason": reason,
83
+ "resume_text": cv_resume_text
84
+ }).strip()
85
+ st.markdown(email_output)
86
+ st.download_button("Download Email", email_output, file_name="cold_email.txt")
87
+
88
+ # Cover Letter Generator
89
+ with tabs[1]:
90
+ st.header("Generate a Cover Letter")
91
+ job_title = st.text_input("Job Title")
92
+ company_name = st.text_input("Company Name")
93
+ key_skills = st.text_area("Key Skills for the Job")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
  if st.button("Generate Cover Letter"):
96
+ cover_letter_output = cover_letter_chain.run({
97
+ "job_title": job_title,
98
+ "company": company_name,
99
+ "key_skills": key_skills,
100
+ "resume_text": cv_resume_text
101
+ }).strip()
102
+ st.markdown(cover_letter_output)
103
+ st.download_button("Download Cover Letter", cover_letter_output, file_name="cover_letter.txt")
104
+
105
+ # Research Statement Generator
106
+ with tabs[2]:
107
+ st.header("Generate a Research Statement")
108
+ research_background = st.text_area("Your Research Background")
109
+ key_projects = st.text_area("Key Projects You've Worked On")
110
+ future_goals = st.text_area("Future Research Goals")
 
 
 
 
111
 
112
  if st.button("Generate Research Statement"):
113
+ research_statement_output = research_statement_chain.run({
114
+ "research_background": research_background,
115
+ "key_projects": key_projects,
116
+ "future_goals": future_goals
117
+ }).strip()
118
+ st.markdown(research_statement_output)
119
+ st.download_button("Download Research Statement", research_statement_output, file_name="research_statement.txt")
120
+
121
+ # Statement of Purpose Generator
122
+ with tabs[3]:
123
+ st.header("Generate a Statement of Purpose (SOP)")
124
+ motivation = st.text_area("Your Motivation for Graduate Studies")
125
+ academic_background = st.text_area("Your Academic Background")
126
+ research_experiences = st.text_area("Your Research Experiences")
127
+ career_goals = st.text_area("Your Career Goals")
128
+ why_this_program = st.text_area("Why This Program?")
 
129
 
130
  if st.button("Generate SOP"):
131
+ sop_output = sop_chain.run({
132
+ "motivation": motivation,
133
+ "academic_background": academic_background,
134
+ "research_experiences": research_experiences,
135
+ "career_goals": career_goals,
136
+ "why_this_program": why_this_program
137
+ }).strip()
138
+ st.markdown(sop_output)
139
+ st.download_button("Download SOP", sop_output, file_name="sop.txt")