Dy commited on
Commit
354e5e1
1 Parent(s): 3a40e06

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from bs4 import BeautifulSoup
3
+ import gradio as gr
4
+ from langchain import OpenAI, ConversationChain, LLMChain, PromptTemplate
5
+ from langchain.memory import ConversationBufferWindowMemory
6
+ import openai
7
+ import requests
8
+ from langchain.chat_models import ChatOpenAI
9
+ import docx2txt
10
+ from pypdf import PdfReader
11
+ import requests
12
+ from bs4 import BeautifulSoup
13
+ from io import BytesIO
14
+
15
+ template = '''
16
+ {history}
17
+ {human_input}
18
+ '''
19
+ prompt = PromptTemplate(
20
+ input_variables=["history", "human_input"],
21
+ template=template
22
+ )
23
+
24
+ chatgpt_chain = LLMChain(
25
+ llm=ChatOpenAI(model="gpt-3.5-turbo", temperature=0.5,openai_api_key=OPENAI_API_KEY),
26
+ prompt=prompt,
27
+ verbose=True,
28
+ memory=ConversationBufferWindowMemory(k=10),
29
+ )
30
+
31
+ def process_files(file, url):
32
+
33
+ print(file.name)
34
+ print(file.read())
35
+ # Extract text from Word or PDF files
36
+ if file.name.endswith('.docx'):
37
+ text_from_file = docx2txt.process(file.name)
38
+ elif file.name.endswith('.pdf'):
39
+ reader = PdfReader(file.name)
40
+ text_from_file = ''
41
+ print(len(reader.pages))
42
+ for page_num in range(len(reader.pages)):
43
+ page = reader.pages[page_num]
44
+ #print(page.extract_text())
45
+ text_from_file += page.extract_text()
46
+ word_list = []
47
+ for i in text_from_file.split('\n'):
48
+ word_list.append(i.replace(' ', ''))
49
+
50
+ text_from_file = ' '.join(word_list)
51
+ del word_list
52
+
53
+ # Extract text from URL
54
+
55
+ response = requests.get(url)
56
+ soup = BeautifulSoup(response.text, 'html.parser')
57
+ raw_text = soup.get_text()
58
+ text_from_url = ' '.join(raw_text.split())
59
+
60
+ prompt_input = '''
61
+
62
+ Here is my resume:
63
+ {}
64
+
65
+ Here is the company's job description:
66
+ {}
67
+
68
+ Write a cover letter in the first person perspective that is convincing and touching.
69
+ Relate points in the resume with the job description.
70
+ Write in the first person perspective.
71
+ Highlight the facts in my resume that is relevant to the job description.
72
+ The cover letter should convince the hiring manager that this candidate is highly valuable and an absolute must hire.
73
+ Start the cover letter with "Dear"
74
+
75
+ '''.format(text_from_file, text_from_url)
76
+
77
+ output=chatgpt_chain.predict(human_input=prompt_input)
78
+
79
+ os.remove(file.name)
80
+
81
+ return output
82
+
83
+ iface = gr.Interface(
84
+ fn=process_files,
85
+ inputs=[
86
+ gr.inputs.File(type="file", label="Upload .docx or .pdf file"),
87
+ gr.inputs.Textbox(lines=2, label="Job Description URL",placeholder="Enter job URL here...")],
88
+ outputs=[
89
+ gr.outputs.Textbox(label="Cover Letter")
90
+ ],
91
+ title="Auto Cover Letter",
92
+ description="Upload your resume(make sure it is a .docx or .pdf file) and insert the URL containing the job description.\nProcess takes about 40 seconds."
93
+ )
94
+
95
+ iface.launch()