primary upload of the app
Browse files- LLMHelper.py +88 -0
- Readme.md +13 -0
- app.py +98 -0
- requirements.txt +5 -0
LLMHelper.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from ctransformers import AutoModelForCausalLM
|
4 |
+
from langchain.llms import OpenAI
|
5 |
+
from langchain.prompts import PromptTemplate
|
6 |
+
|
7 |
+
AVAILABLE_MODELS_GGUF = {
|
8 |
+
"TheBloke/Marcoroni-7B-v3-GGUF": {
|
9 |
+
"model_file": "marcoroni-7b-v3.Q4_K_M.gguf",
|
10 |
+
"model_type": "marcoroni"
|
11 |
+
},
|
12 |
+
"TheBloke/Mistral-7B-Instruct-v0.2-GGUF": {
|
13 |
+
"model_file": "mistral-7b-instruct-v0.2.Q5_K_M.gguf",
|
14 |
+
"model_type": "mistral"
|
15 |
+
},
|
16 |
+
"TheBloke/zephyr-7B-beta-GGUF": {
|
17 |
+
"model_file": "zephyr-7b-beta.Q5_K_M.gguf",
|
18 |
+
"model_type": "zephyr"
|
19 |
+
},
|
20 |
+
"TheBloke/una-cybertron-7B-v2-GGUF": {
|
21 |
+
"model_file": "una-cybertron-7b-v2-bf16.Q5_K_M.gguf",
|
22 |
+
"model_type": "cybertron"
|
23 |
+
},
|
24 |
+
|
25 |
+
}
|
26 |
+
|
27 |
+
AVAILABLE_MODELS_OPENAI = [
|
28 |
+
"gpt-4-1106-preview", "gpt-4-32k", "gpt-3.5-turbo-1106",
|
29 |
+
]
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
def generate_cover_letter_open_source(job_description, resume, selected_model, context_length=8000):
|
35 |
+
print(f"selected_model: {selected_model}, "
|
36 |
+
f"{AVAILABLE_MODELS_GGUF[selected_model]['model_file']}, {AVAILABLE_MODELS_GGUF[selected_model]['model_type']}")
|
37 |
+
print(f"context_length: {context_length}")
|
38 |
+
|
39 |
+
prompt = (f"Do the following steps: "
|
40 |
+
f"1. Read the following job description,"
|
41 |
+
f"2. Read the following resume, "
|
42 |
+
f"3. Write a formal cover letter to the hiring manager for the job description based on the given resume, "
|
43 |
+
# f"4. The cover letter MUST BE within {output_size_range[0]} to {output_size_range[1]} words. "
|
44 |
+
# f"4. The cover letter MUST BE within 100 words. "
|
45 |
+
f"4. Return ONLY the cover letter ONCE, nothing else. "
|
46 |
+
f"Job Description: '{job_description}'. Resume: '{resume}'")
|
47 |
+
|
48 |
+
# prompt = "What is an LLM"
|
49 |
+
|
50 |
+
llm = AutoModelForCausalLM.from_pretrained(selected_model,
|
51 |
+
model_file=AVAILABLE_MODELS_GGUF[selected_model]['model_file'],
|
52 |
+
model_type=AVAILABLE_MODELS_GGUF[selected_model]['model_type'],
|
53 |
+
context_length=context_length,
|
54 |
+
max_new_tokens=1000,
|
55 |
+
reset=True,
|
56 |
+
stream=True,
|
57 |
+
# top_k=2,
|
58 |
+
temperature=0.5
|
59 |
+
)
|
60 |
+
|
61 |
+
llm_response = llm(prompt)
|
62 |
+
|
63 |
+
return llm_response
|
64 |
+
|
65 |
+
|
66 |
+
def generate_cover_letter_openai(job_description, resume, selected_model, openai_key=None):
|
67 |
+
os.environ["OPENAI_API_KEY"] = openai_key
|
68 |
+
temp = "Do the following steps: " \
|
69 |
+
"1. Read the following job description," \
|
70 |
+
"2. Read the following resume, " \
|
71 |
+
"3. Write a formal cover letter to the hiring manager for the job description based on the given resume, " \
|
72 |
+
"4. Return ONLY the cover letter ONCE, nothing else. " \
|
73 |
+
"Job Description: '{job_description}'. Resume: '{resume}'"
|
74 |
+
|
75 |
+
prompt = PromptTemplate(
|
76 |
+
template=temp,
|
77 |
+
input_variables=["job_description", "resume"]
|
78 |
+
)
|
79 |
+
|
80 |
+
# model = OpenAI(openai_api_key=openai_key, max_tokens=-1)
|
81 |
+
print(f'openai key: {openai_key}')
|
82 |
+
# model = OpenAI(model_name=selected_model, openai_api_key="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")
|
83 |
+
model = OpenAI(model_name=selected_model)
|
84 |
+
|
85 |
+
_input = prompt.format(job_description=job_description, resume=resume)
|
86 |
+
output = model.stream(_input)
|
87 |
+
|
88 |
+
return output
|
Readme.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Llm Cover Letter Generator
|
3 |
+
emoji: 📝
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: green
|
6 |
+
sdk: streamlit
|
7 |
+
sdk_version: 1.29.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
---
|
11 |
+
|
12 |
+
1. pip install -r requirements.txt
|
13 |
+
2. streamlit run app.py
|
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import streamlit as st
|
3 |
+
import LLMHelper
|
4 |
+
|
5 |
+
|
6 |
+
def generate_open_source():
|
7 |
+
with output_col:
|
8 |
+
st.session_state.running = True
|
9 |
+
start_time = time.time()
|
10 |
+
context_length = (len(st.session_state.get('resume', '').split()) +
|
11 |
+
len(st.session_state.get('jd', '').split()) +
|
12 |
+
2000)
|
13 |
+
cover_letter_generator = LLMHelper.generate_cover_letter_open_source(
|
14 |
+
job_description=st.session_state['jd'], resume=st.session_state['resume'],
|
15 |
+
selected_model=selected_model, context_length=context_length
|
16 |
+
)
|
17 |
+
print(f'generated text: {cover_letter_generator}')
|
18 |
+
generate_response(cover_letter_generator, start_time)
|
19 |
+
st.session_state.running = False
|
20 |
+
|
21 |
+
|
22 |
+
def generate_openai():
|
23 |
+
with output_col:
|
24 |
+
start_time = time.time()
|
25 |
+
try:
|
26 |
+
cover_letter_generator = LLMHelper.generate_cover_letter_openai(
|
27 |
+
job_description=st.session_state['jd'], resume=st.session_state['resume'],
|
28 |
+
selected_model=selected_model, openai_key=open_ai_key
|
29 |
+
)
|
30 |
+
generate_response(cover_letter_generator, start_time)
|
31 |
+
except ValueError as e:
|
32 |
+
st.error("Please provide a valid Open AI API key")
|
33 |
+
st.session_state.running = False
|
34 |
+
|
35 |
+
|
36 |
+
def generate_response(cover_letter_gen, start_time):
|
37 |
+
with output_col:
|
38 |
+
if cover_letter_gen is not None:
|
39 |
+
with st.container(border=True):
|
40 |
+
with st.spinner("Generating text..."):
|
41 |
+
generated_text_placeholder = st.empty()
|
42 |
+
for chunk in cover_letter_gen:
|
43 |
+
st.session_state.cover_letter_stream += chunk
|
44 |
+
generated_text_placeholder.write(st.session_state.cover_letter_stream)
|
45 |
+
st.write(f"generated words: {len(st.session_state.cover_letter_stream.split())}")
|
46 |
+
st.write(f"generation time: {round(time.time() - start_time, 2)} seconds")
|
47 |
+
|
48 |
+
|
49 |
+
if 'running' not in st.session_state:
|
50 |
+
st.session_state.running = False
|
51 |
+
|
52 |
+
st.session_state.cover_letter_stream = ""
|
53 |
+
st.set_page_config(page_title='Cover Letter Generator', layout="wide")
|
54 |
+
st.markdown("## Cover Letter Generator")
|
55 |
+
info = st.expander("Information")
|
56 |
+
info.write(f"This project aims to:\n"
|
57 |
+
f"- Explore various open-source Large Language Models (LLMs).\n"
|
58 |
+
f"- Compare them to OpenAI models for performance. \n"
|
59 |
+
f"- Highlight benefits of open-source LLMs: \n"
|
60 |
+
f" - Faster generation on OpenAI models due to non-local execution.\n"
|
61 |
+
f" - Run open-source models on CPU with 10GB RAM (Around 5-min generation time).\n"
|
62 |
+
f" - Significantly faster generation on GPUs. \n"
|
63 |
+
f" - Free of cost and user-data ownership.\n\n"
|
64 |
+
f"Checkout my profile: https://zayedupal.github.io")
|
65 |
+
|
66 |
+
input_col, output_col = st.columns(2)
|
67 |
+
|
68 |
+
with input_col:
|
69 |
+
st.session_state['jd'] = st.text_area("Job Description",
|
70 |
+
placeholder="Paste the job description here",
|
71 |
+
disabled=st.session_state.running)
|
72 |
+
st.write(f"{len(st.session_state.get('jd', '').split())} words")
|
73 |
+
|
74 |
+
st.session_state['resume'] = st.text_area("Resume Information",
|
75 |
+
placeholder="Paste the resume content here",
|
76 |
+
disabled=st.session_state.running)
|
77 |
+
|
78 |
+
st.write(f"{len(st.session_state.get('resume', '').split())} words")
|
79 |
+
|
80 |
+
with output_col:
|
81 |
+
llm_tab = st.radio("LLM type", ["Open Source LLMs", "Open AI LLMs"], horizontal=True)
|
82 |
+
if llm_tab == "Open Source LLMs":
|
83 |
+
cover_letter_generator = None
|
84 |
+
st.session_state.cover_letter_stream = ""
|
85 |
+
selected_model = st.selectbox("Select LLM Model", options=LLMHelper.AVAILABLE_MODELS_GGUF.keys(),
|
86 |
+
disabled=st.session_state.running)
|
87 |
+
|
88 |
+
st.button("Generate Cover Letter", key='open_source_gen_key', on_click=generate_open_source,
|
89 |
+
disabled=st.session_state.running)
|
90 |
+
|
91 |
+
elif llm_tab == "Open AI Models":
|
92 |
+
cover_letter_generator = None
|
93 |
+
st.session_state.cover_letter_stream = ""
|
94 |
+
selected_model = st.selectbox("Select Open AI Model", options=LLMHelper.AVAILABLE_MODELS_OPENAI,
|
95 |
+
disabled=st.session_state.running)
|
96 |
+
open_ai_key = st.text_input("Enter your open ai API key")
|
97 |
+
st.button("Generate Cover Letter", key='open_ai_gen_key', disabled=st.session_state.running,
|
98 |
+
on_click=generate_openai)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
huggingface-hub
|
3 |
+
ctransformers
|
4 |
+
langchain
|
5 |
+
openai==0.28.1
|