Spaces:
Sleeping
Sleeping
pre release milestone: generate pdf
Browse files- Signature.png +3 -0
- app.py +9 -4
- data_test.py +8 -0
- requirements.txt +1 -0
- taskNonAI.py +27 -1
- template_base.typ +62 -0
- template_letter.tmpl +15 -0
- test.py +8 -2
Signature.png
ADDED
|
Git LFS Details
|
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from config import STRONG_API_BASE, STRONG_API_KEY, STRONG_MODEL
|
|
| 4 |
from util import is_valid_url
|
| 5 |
from util import mylogger
|
| 6 |
from util import stream_together
|
| 7 |
-
from taskNonAI import extract_url, file_to_html
|
| 8 |
from taskAI import TaskAI
|
| 9 |
## load data
|
| 10 |
from data_test import mock_jd, mock_cv
|
|
@@ -14,7 +14,7 @@ import gradio as gr
|
|
| 14 |
from pypandoc.pandoc_download import download_pandoc
|
| 15 |
## std
|
| 16 |
import os
|
| 17 |
-
|
| 18 |
|
| 19 |
logger = mylogger(__name__,'%(asctime)s:%(levelname)s:%(message)s')
|
| 20 |
info = logger.info
|
|
@@ -89,6 +89,11 @@ def finalize_letter_txt(api_base, api_key, api_model, debug_CoT, jd, cv):
|
|
| 89 |
for result in gen:
|
| 90 |
yield result
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
with gr.Blocks(
|
| 93 |
title=DEMO_TITLE,
|
| 94 |
theme=gr.themes.Base(primary_hue="blue", secondary_hue="sky", neutral_hue="slate"),
|
|
@@ -175,11 +180,11 @@ with gr.Blocks(
|
|
| 175 |
).then(fn=run_compose, inputs=[strong_base, strong_key, strong_model, min_jd, min_cv], outputs=[debug_CoT]
|
| 176 |
).then(fn=lambda:gr.Accordion("Expert Zone", open=False),inputs=None, outputs=[expert_zone]
|
| 177 |
).then(fn=finalize_letter_txt, inputs=[cheap_base, cheap_key, cheap_model, debug_CoT, jd_info, cv_text], outputs=[cover_letter_text, debug_jobapp]
|
| 178 |
-
)
|
| 179 |
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
| 182 |
init()
|
| 183 |
-
app.queue(max_size=
|
| 184 |
show_error=True, debug=True, share=IS_SHARE
|
| 185 |
)
|
|
|
|
| 4 |
from util import is_valid_url
|
| 5 |
from util import mylogger
|
| 6 |
from util import stream_together
|
| 7 |
+
from taskNonAI import extract_url, file_to_html, compile_pdf
|
| 8 |
from taskAI import TaskAI
|
| 9 |
## load data
|
| 10 |
from data_test import mock_jd, mock_cv
|
|
|
|
| 14 |
from pypandoc.pandoc_download import download_pandoc
|
| 15 |
## std
|
| 16 |
import os
|
| 17 |
+
import json
|
| 18 |
|
| 19 |
logger = mylogger(__name__,'%(asctime)s:%(levelname)s:%(message)s')
|
| 20 |
info = logger.info
|
|
|
|
| 89 |
for result in gen:
|
| 90 |
yield result
|
| 91 |
|
| 92 |
+
def finalize_letter_pdf(meta_data, cover_letter_text):
|
| 93 |
+
pdf_context = json.loads(meta_data)
|
| 94 |
+
pdf_context["letter_body"] = cover_letter_text
|
| 95 |
+
return compile_pdf(pdf_context,tmpl_path="template_letter.tmpl",output_path=f"/tmp/cover_letter_{pdf_context['applicant_full_name']}_to_{pdf_context['company_full_name']}.pdf")
|
| 96 |
+
|
| 97 |
with gr.Blocks(
|
| 98 |
title=DEMO_TITLE,
|
| 99 |
theme=gr.themes.Base(primary_hue="blue", secondary_hue="sky", neutral_hue="slate"),
|
|
|
|
| 180 |
).then(fn=run_compose, inputs=[strong_base, strong_key, strong_model, min_jd, min_cv], outputs=[debug_CoT]
|
| 181 |
).then(fn=lambda:gr.Accordion("Expert Zone", open=False),inputs=None, outputs=[expert_zone]
|
| 182 |
).then(fn=finalize_letter_txt, inputs=[cheap_base, cheap_key, cheap_model, debug_CoT, jd_info, cv_text], outputs=[cover_letter_text, debug_jobapp]
|
| 183 |
+
).then(fn=finalize_letter_pdf, inputs=[debug_jobapp, cover_letter_text], outputs=[cover_letter_pdf])
|
| 184 |
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
init()
|
| 188 |
+
app.queue(max_size=1, default_concurrency_limit=1).launch(
|
| 189 |
show_error=True, debug=True, share=IS_SHARE
|
| 190 |
)
|
data_test.py
CHANGED
|
@@ -65,3 +65,11 @@ References
|
|
| 65 |
|
| 66 |
Available upon request.
|
| 67 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
Available upon request.
|
| 67 |
"""
|
| 68 |
+
pdf_context = {
|
| 69 |
+
"company_full_name": "Mastercard",
|
| 70 |
+
"job_title": "Project Management Intern",
|
| 71 |
+
"applicant_full_name": "Dorothy Gale",
|
| 72 |
+
"applicant_contact_information": "123 Main St., Emerald City, KS 12345, (123) 456-7890, dorothy@wizardofoz.com",
|
| 73 |
+
"letter_body": "text,\n\ntest test"
|
| 74 |
+
}
|
| 75 |
+
|
requirements.txt
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# UI
|
| 2 |
gradio>=4.0,<=5.0
|
| 3 |
# external tool
|
|
|
|
| 4 |
pypandoc
|
| 5 |
shot-scraper
|
| 6 |
# LLM related
|
|
|
|
| 1 |
# UI
|
| 2 |
gradio>=4.0,<=5.0
|
| 3 |
# external tool
|
| 4 |
+
typst
|
| 5 |
pypandoc
|
| 6 |
shot-scraper
|
| 7 |
# LLM related
|
taskNonAI.py
CHANGED
|
@@ -1,8 +1,14 @@
|
|
| 1 |
import pypandoc
|
|
|
|
| 2 |
## stdlib
|
| 3 |
import subprocess
|
| 4 |
import json
|
|
|
|
| 5 |
from typing import Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def file_to_html(file_path: str) -> str:
|
| 8 |
return pypandoc.convert_file(file_path, "html")
|
|
@@ -29,4 +35,24 @@ def extract_url(url: str) -> Optional[str]:
|
|
| 29 |
except:
|
| 30 |
raise Exception(
|
| 31 |
f"Please try copy-paste as input. Failed to extract content from: {url}. Didn't find content from given URL!"
|
| 32 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pypandoc
|
| 2 |
+
import typst
|
| 3 |
## stdlib
|
| 4 |
import subprocess
|
| 5 |
import json
|
| 6 |
+
import os
|
| 7 |
from typing import Optional
|
| 8 |
+
from string import Template
|
| 9 |
+
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
|
| 12 |
|
| 13 |
def file_to_html(file_path: str) -> str:
|
| 14 |
return pypandoc.convert_file(file_path, "html")
|
|
|
|
| 35 |
except:
|
| 36 |
raise Exception(
|
| 37 |
f"Please try copy-paste as input. Failed to extract content from: {url}. Didn't find content from given URL!"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def date():
|
| 41 |
+
current_date = datetime.now()
|
| 42 |
+
return current_date.strftime(
|
| 43 |
+
f"%B %d{'th' if 4 <= current_date.day <= 20 or 24 <= current_date.day <= 30 else ['st', 'nd', 'rd'][current_date.day % 10 - 1]} , %Y")
|
| 44 |
+
|
| 45 |
+
def typst_escape(s):
|
| 46 |
+
return s.replace('@','\@').replace('#','\#')
|
| 47 |
+
|
| 48 |
+
def compile_pdf(context: dict, tmpl_path: str, output_path="/tmp/cover_letter.pdf"):
|
| 49 |
+
with open(tmpl_path, "r", encoding='utf8') as f:
|
| 50 |
+
tmpl = Template(f.read())
|
| 51 |
+
context = {k: typst_escape(v) for k, v in context.items()}
|
| 52 |
+
context.update({'date_string': date()})
|
| 53 |
+
letter_typ = tmpl.safe_substitute(context)
|
| 54 |
+
with open('letter.typ', 'w', encoding='utf8') as f:
|
| 55 |
+
f.write(letter_typ)
|
| 56 |
+
typst.compile('letter.typ', output=output_path)
|
| 57 |
+
os.remove('letter.typ')
|
| 58 |
+
return output_path
|
template_base.typ
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// This function gets your whole document as its `body`
|
| 2 |
+
// and formats it as a simple letter.
|
| 3 |
+
#let letter(
|
| 4 |
+
// The letter's sender, which is display at the top of the page.
|
| 5 |
+
sender: none,
|
| 6 |
+
|
| 7 |
+
// The letter's recipient, which is displayed close to the top.
|
| 8 |
+
recipient: none,
|
| 9 |
+
|
| 10 |
+
// The date, displayed to the right.
|
| 11 |
+
date: none,
|
| 12 |
+
|
| 13 |
+
// The subject line.
|
| 14 |
+
subject: none,
|
| 15 |
+
|
| 16 |
+
// The name with which the letter closes.
|
| 17 |
+
name: none,
|
| 18 |
+
|
| 19 |
+
// The letter's content.
|
| 20 |
+
body
|
| 21 |
+
) = {
|
| 22 |
+
// Configure page and text properties.
|
| 23 |
+
set page(paper: "us-letter", margin: (top: 2cm, bottom: 1.0cm))
|
| 24 |
+
set text(font: "PT Sans")
|
| 25 |
+
|
| 26 |
+
// Display sender at top of page. If there's no sender
|
| 27 |
+
// add some hidden text to keep the same spacing.
|
| 28 |
+
[== #name]
|
| 29 |
+
text(9pt, if sender == none {
|
| 30 |
+
hide("a")
|
| 31 |
+
} else {
|
| 32 |
+
sender
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
v(1.8cm)
|
| 36 |
+
|
| 37 |
+
// Display recipient.
|
| 38 |
+
recipient
|
| 39 |
+
|
| 40 |
+
v(0.5cm)
|
| 41 |
+
|
| 42 |
+
// Display date. If there's no date add some hidden
|
| 43 |
+
// text to keep the same spacing.
|
| 44 |
+
align(right, if date != none {
|
| 45 |
+
date
|
| 46 |
+
} else {
|
| 47 |
+
hide("a")
|
| 48 |
+
})
|
| 49 |
+
|
| 50 |
+
v(2cm)
|
| 51 |
+
|
| 52 |
+
// Add the subject line, if any.
|
| 53 |
+
if subject != none {
|
| 54 |
+
pad(right: 10%, strong(subject))
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
// Add body and name.
|
| 58 |
+
body
|
| 59 |
+
// v(1.25cm)
|
| 60 |
+
image("Signature.png", height: 7%, fit: "stretch")
|
| 61 |
+
name
|
| 62 |
+
}
|
template_letter.tmpl
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#import "template_base.typ": *
|
| 2 |
+
#show: letter.with(
|
| 3 |
+
sender: [
|
| 4 |
+
${applicant_contact_information}
|
| 5 |
+
],
|
| 6 |
+
recipient: [
|
| 7 |
+
Hiring Manager \
|
| 8 |
+
${company_full_name} \
|
| 9 |
+
],
|
| 10 |
+
date: [${date_string}],
|
| 11 |
+
subject: [Cover Letter for ${job_title}],
|
| 12 |
+
name: [${applicant_full_name}],
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
${letter_body}
|
test.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from taskAI import TaskAI
|
| 2 |
-
from
|
|
|
|
| 3 |
from config_secret import api_test
|
| 4 |
|
| 5 |
from llama_index.llms.openai_like import OpenAILike
|
|
@@ -27,6 +28,11 @@ def test_taskAI():
|
|
| 27 |
for chunk in gen:
|
| 28 |
print(chunk)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if __name__ == "__main__":
|
| 31 |
-
test_taskAI()
|
| 32 |
# integration()
|
|
|
|
|
|
| 1 |
from taskAI import TaskAI
|
| 2 |
+
from taskNonAI import compile_pdf
|
| 3 |
+
from data_test import mock_jd, mock_cv, pdf_context
|
| 4 |
from config_secret import api_test
|
| 5 |
|
| 6 |
from llama_index.llms.openai_like import OpenAILike
|
|
|
|
| 28 |
for chunk in gen:
|
| 29 |
print(chunk)
|
| 30 |
|
| 31 |
+
def test_typst_pdf():
|
| 32 |
+
compile_pdf(tmpl_path='template_letter.tmpl',context=pdf_context)
|
| 33 |
+
os
|
| 34 |
+
|
| 35 |
if __name__ == "__main__":
|
| 36 |
+
# test_taskAI()
|
| 37 |
# integration()
|
| 38 |
+
test_typst_pdf()
|