Dy
Update app.py
4b66db0
import os
from bs4 import BeautifulSoup
import gradio as gr
from langchain import OpenAI, ConversationChain, LLMChain, PromptTemplate
from langchain.memory import ConversationBufferWindowMemory
import openai
import requests
from langchain.chat_models import ChatOpenAI
import docx2txt
from pypdf import PdfReader
import requests
from bs4 import BeautifulSoup
from io import BytesIO
OPENAI_API_KEY = os.environ['OPENAI_API_KEY']
template = '''
{history}
{human_input}
'''
prompt = PromptTemplate(
input_variables=["history", "human_input"],
template=template
)
chatgpt_chain = LLMChain(
llm=ChatOpenAI(model="gpt-3.5-turbo", temperature=0.5,openai_api_key=OPENAI_API_KEY),
prompt=prompt,
verbose=True,
memory=ConversationBufferWindowMemory(k=0),
)
def process_files(file, url):
print(file.name)
print(file.read())
# Extract text from Word or PDF files
if file.name.endswith('.docx'):
text_from_file = docx2txt.process(file.name)
elif file.name.endswith('.pdf'):
reader = PdfReader(file.name)
text_from_file = ''
print(len(reader.pages))
for page_num in range(len(reader.pages)):
page = reader.pages[page_num]
#print(page.extract_text())
text_from_file += page.extract_text()
word_list = []
for i in text_from_file.split('\n'):
word_list.append(i.replace(' ', ''))
text_from_file = ' '.join(word_list)
del word_list
# Extract text from URL
headers={"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"}
response = requests.get(url,headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
raw_text = soup.get_text()
text_from_url = ' '.join(raw_text.split())
prompt_input = '''
Here is my resume:
{}
Here is the company's job description:
{}
Write a cover letter in the first person perspective that is convincing and touching.
Relate points in the resume with the job description.
Write in the first person perspective.
Highlight the facts in my resume that is relevant to the job description.
The cover letter should convince the hiring manager that this candidate is highly valuable and an absolute must hire.
Start the cover letter with "Dear"
'''.format(text_from_file, text_from_url)
output=chatgpt_chain.predict(human_input=prompt_input)
os.remove(file.name)
return output
iface = gr.Interface(
fn=process_files,
inputs=[
gr.inputs.File(type="file", label="Upload .docx or .pdf file"),
gr.inputs.Textbox(lines=2, label="Job Description URL",placeholder="Enter job URL here...")],
outputs=[
gr.outputs.Textbox(label="Cover Letter")
],
title="Auto Cover Letter",
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."
)
iface.launch()