Sobit commited on
Commit
fa302fb
·
verified ·
1 Parent(s): 5c81a3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -138
app.py CHANGED
@@ -1,12 +1,10 @@
1
- import os
2
  import streamlit as st
3
  import PyPDF2
4
- from langchain_community.llms import HuggingFaceHub
5
-
6
- # Streamlit page configuration
7
- st.set_page_config(page_title="Research Position Application Generator", page_icon="🔬")
8
 
9
- # Set Hugging Face API Key
10
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
11
 
12
  # Initialize LLM
@@ -15,145 +13,66 @@ llm = HuggingFaceHub(
15
  model_kwargs={"temperature": 0.5}
16
  )
17
 
18
- def extract_text_from_pdf(uploaded_file):
19
- """
20
- Extract text from an uploaded PDF file.
21
-
22
- Args:
23
- uploaded_file (UploadedFile): PDF file uploaded by the user
24
-
25
- Returns:
26
- str: Extracted text from the PDF
27
- """
28
- try:
29
- pdf_reader = PyPDF2.PdfReader(uploaded_file)
30
- text = ""
31
- for page in pdf_reader.pages:
32
- text += page.extract_text()
33
- return text
34
- except Exception as e:
35
- st.error(f"Error extracting PDF text: {e}")
36
- return ""
37
-
38
- def generate_cold_email(position_details, cv_text):
39
- """
40
- Generate a professional cold email using the LLM.
41
-
42
- Args:
43
- position_details (dict): Details about the research position
44
- cv_text (str): Text extracted from the CV/resume
45
-
46
- Returns:
47
- str: Generated cold email
48
- """
49
- prompt = f"""Write a professional and concise cold email to Professor {position_details['professor_name']}
50
- at {position_details['university']} about the research position in {position_details['research_focus']}.
51
- The email should:
52
- 1. Demonstrate knowledge of the professor's research
53
- 2. Highlight relevant experience from the CV
54
- 3. Express genuine interest in the position
55
- 4. Be no more than 250 words
56
-
57
- CV Details:
58
- {cv_text}
59
-
60
- Research Position Details:
61
- Research Focus: {position_details['research_focus']}
62
- Professor: {position_details['professor_name']}
63
- University: {position_details['university']}
64
- """
65
-
66
- return llm.invoke(prompt)
67
 
68
- def generate_cover_letter(position_details, cv_text):
69
- """
70
- Generate a formal cover letter using the LLM.
71
-
72
- Args:
73
- position_details (dict): Details about the research position
74
- cv_text (str): Text extracted from the CV/resume
75
-
76
- Returns:
77
- str: Generated cover letter
78
- """
79
- prompt = f"""Write a professional and formal cover letter for a research position with the following details:
80
- Research Focus: {position_details['research_focus']}
81
- University: {position_details['university']}
82
 
83
- The cover letter should:
84
- 1. Follow a standard business letter format
85
- 2. Clearly state the purpose of the letter
86
- 3. Highlight relevant skills and experiences from the CV
87
- 4. Demonstrate alignment with the research position
88
- 5. Be 300-400 words long
89
- 6. Include a strong closing paragraph
90
 
91
- CV Details:
92
- {cv_text}
93
- """
94
-
95
- return llm.invoke(prompt)
96
 
97
- def main():
98
- """
99
- Main Streamlit app function
100
- """
101
- st.title("🔬 Research Position Application Generator")
102
-
103
- # Sidebar for position details
104
- st.sidebar.header("Research Position Details")
105
- professor_name = st.sidebar.text_input("Professor's Name")
106
- university = st.sidebar.text_input("University")
107
- research_focus = st.sidebar.text_input("Research Focus")
108
-
109
- # CV Upload
110
- st.sidebar.header("Upload CV/Resume")
111
- uploaded_cv = st.sidebar.file_uploader("Choose a PDF file", type="pdf")
112
-
113
- # Generate button
114
- if st.sidebar.button("Generate Documents"):
115
- # Validate inputs
116
- if not (professor_name and university and research_focus and uploaded_cv):
117
- st.error("Please fill in all details and upload a CV")
118
- return
119
 
120
- # Extract CV text
121
- cv_text = extract_text_from_pdf(uploaded_cv)
122
 
123
- # Prepare position details
124
- position_details = {
125
- 'professor_name': professor_name,
126
- 'university': university,
127
- 'research_focus': research_focus
128
- }
 
 
 
 
 
 
 
 
129
 
130
- # Generate documents
131
- with st.spinner('Generating documents...'):
132
- cold_email = generate_cold_email(position_details, cv_text)
133
- cover_letter = generate_cover_letter(position_details, cv_text)
134
 
135
  # Display results
136
- st.header("Generated Documents")
137
-
138
- # Cold Email
139
- st.subheader("Cold Email")
140
- st.write(cold_email)
141
- st.download_button(
142
- label="Download Cold Email",
143
- data=cold_email,
144
- file_name="cold_email.txt",
145
- mime="text/plain"
146
- )
147
 
148
- # Cover Letter
149
- st.subheader("Cover Letter")
150
- st.write(cover_letter)
151
- st.download_button(
152
- label="Download Cover Letter",
153
- data=cover_letter,
154
- file_name="cover_letter.txt",
155
- mime="text/plain"
156
- )
157
-
158
- if __name__ == "__main__":
159
- main()
 
 
1
  import streamlit as st
2
  import PyPDF2
3
+ import os
4
+ from langchain.llms import HuggingFaceHub
5
+ from langchain.prompts import PromptTemplate
 
6
 
7
+ # Set up API token
8
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
9
 
10
  # Initialize LLM
 
13
  model_kwargs={"temperature": 0.5}
14
  )
15
 
16
+ # Function to extract text from PDF
17
+ def extract_text_from_pdf(pdf_file):
18
+ pdf_reader = PyPDF2.PdfReader(pdf_file)
19
+ text = ""
20
+ for page in pdf_reader.pages:
21
+ text += page.extract_text() + "\n"
22
+ return text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ # Streamlit UI
25
+ st.title("Cold Email & Cover Letter Generator for Professors")
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ # User inputs
28
+ position_details = st.text_area("Enter details about the research position:",
29
+ "e.g., Professor's name, research focus, university, lab details, etc.")
30
+ resume_file = st.file_uploader("Upload your CV/Resume (PDF)", type=["pdf"])
 
 
 
31
 
32
+ if st.button("Generate Cold Email & Cover Letter"):
33
+ if position_details and resume_file:
34
+ # Extract text from the uploaded PDF
35
+ resume_text = extract_text_from_pdf(resume_file)
 
36
 
37
+ # Define prompt for cold email
38
+ email_prompt = PromptTemplate.from_template(
39
+ """
40
+ Based on the following details of a research position and a student's CV, generate a professional cold email:
41
+
42
+ Research Position Details:
43
+ {position}
44
+
45
+ Student's CV:
46
+ {resume}
47
+
48
+ The email should be formal, concise, and engaging, expressing interest in the position while highlighting relevant skills.
49
+ """
50
+ )
 
 
 
 
 
 
 
 
51
 
52
+ email_content = llm(email_prompt.format(position=position_details, resume=resume_text))
 
53
 
54
+ # Define prompt for cover letter
55
+ cover_prompt = PromptTemplate.from_template(
56
+ """
57
+ Generate a professional cover letter based on the following research position details and a student's CV:
58
+
59
+ Research Position Details:
60
+ {position}
61
+
62
+ Student's CV:
63
+ {resume}
64
+
65
+ The cover letter should be well-structured, highlighting the student's background, relevant skills, research interests, and motivation for applying.
66
+ """
67
+ )
68
 
69
+ cover_content = llm(cover_prompt.format(position=position_details, resume=resume_text))
 
 
 
70
 
71
  # Display results
72
+ st.subheader("Generated Cold Email")
73
+ st.write(email_content)
 
 
 
 
 
 
 
 
 
74
 
75
+ st.subheader("Generated Cover Letter")
76
+ st.write(cover_content)
77
+ else:
78
+ st.warning("Please provide both the position details and upload a CV.")