forked from poe-platform/server-bot-quick-start
-
Notifications
You must be signed in to change notification settings - Fork 14
/
bot_ResumeReviewEx.py
242 lines (181 loc) · 8.21 KB
/
bot_ResumeReviewEx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
Note: inactive
BOT_NAME="ResumeReviewEx"; modal deploy --name $BOT_NAME bot_${BOT_NAME}.py; curl -X POST https://api.poe.com/bot/fetch_settings/$BOT_NAME/$POE_ACCESS_KEY
Test message:
(download this and upload)
https://pjreddie.com/static/Redmon%20Resume.pdf
"""
from __future__ import annotations
import os
from io import BytesIO
from typing import AsyncIterable
import fastapi_poe.client
import pytesseract
import requests
from fastapi_poe import PoeBot, make_app
from fastapi_poe.client import MetaMessage, stream_request
from fastapi_poe.types import QueryRequest, SettingsRequest, SettingsResponse
from modal import Image, Stub, asgi_app
from PIL import Image as PILImage
from sse_starlette.sse import ServerSentEvent
fastapi_poe.client.MAX_EVENT_COUNT = 10000
async def parse_image_document_from_url(image_url: str) -> tuple[bool, str]:
try:
response = requests.get(image_url.strip())
img = PILImage.open(BytesIO(response.content))
custom_config = "--psm 4"
text = pytesseract.image_to_string(img, config=custom_config)
text = text[:10000]
return True, text
except BaseException as e:
print(e)
return False, ""
# Unimplemented guidelines
# - For date ranges, use en dashes (–), not hyphens (-) or the word "to".
# - Do not state how many people you worked with.
# - Specify what you exactly did.
RESUME_SUFFIX_PROMPT = """
You will review the attached image scan of a resume.
You will iterate over the following guidelines and evaluate whether the resume meets the guidelines.
# Layout
- Use a single column layout.
- The font size should be at least 11.
- Do not add additional indentation to your sections and bullet points.
- The sections and section entries should be left-aligned.
- The dates should be right-aligned.
- Bullet point text should not be justified.
- Your bullet points should not go further right than your dates.
- Ensure sufficient vertical and horizontal margins.
- Ensure sufficient and consistent line spacing between bullet points.
- Ensure sufficient and consistent line spacing between sections/subsections.
# Formatting
- Use Mmm YYYY to format the dates.
- Do not specify the days of the year.
- For date ranges, ensure that there is a space before and after the en dashes.
- Use "Present" if currently working at a job or project.
- Use a modern, easy-to-read font.
- Hyperlinks should be black in color.
- Avoid excessive bolding in bullet points.
# Content
- Do not include your photo in your resume.
- Do not include your identification number, address or birth date in your resume.
- Use standard capitalization and nomenclature.
- Acronyms (except the well-known acronyms) should be fully spelled out.
- Avoid repeating the same verb in the bullet points from the same section.
# Bullet points
- Begin with a verb, in past tense.
- Emphasize your accomplishments first.
- Improvements, if mentioned, should be quantified with an objective, precise and measurable metric.
Reply in the following format
(If the user did not upload an image of their resume, first remind them to upload an image of their resume.)
# Layout
| Guideline | Analysis | OK |
| --------------------------------------------- | -------------------------------------------------------- | -- |
| Use a single column layout. | The resume uses a single column layout. | ✅ |
| Bullet point font size should be at least 11. | As the image is not given, the font size could not be estimated. | ❔ |
(and so on)
# Formatting
| Guideline | Analysis | OK |
| --------------------------------------------- | -------------------------------------------------------- | -- |
| Use Mmm YYYY to format the dates. | The period in Jun. 2022 is not necessary. | ❌ |
(and so on)
# Content
| Guideline | Analysis | OK |
| --------------------------------------------- | -------------------------------------------------------- | -- |
| Do not include your photo in your resume. | The resume does not have a photo | ✅ |
(and so on)
# Bullet points
> Developed a tool to review resumes. (Cite the bullet point FROM THE RESUME. DO NOT HALLUCINATE.)
| Guideline | Analysis | OK |
| --------------------------------------------- | -------------------------------------------------------- | -- |
| Begin with a verb, in past tense. | The bullet point began with "Developed". | ✅ |
(and so on)
> Collected all the job descriptions. (Cite the bullet point FROM THE RESUME. DO NOT HALLUCINATE.)
| Guideline | Analysis | OK |
| --------------------------------------------- | -------------------------------------------------------- | -- |
| Begin with a verb, in past tense. | The bullet point began with "Collected". | ✅ |
(and so on)
---
REMINDER:
- If there is an issue, you will always cite the relevant part of the resume.
- The bullet points being cited MUST BE FROM THE RESUME. DO NOT INVENT THE BULLET POINT.
- You will always follow the reply format (Markdown table).
"""
class EchoBot(PoeBot):
async def get_response(
self, request: QueryRequest
) -> AsyncIterable[ServerSentEvent]:
original_message_id = request.message_id
for query_message in request.query:
# possible attachment inputs - docx, pdf OR png (enforce one attachment)
# Only supporting (one) png for now, until Poe supports sending images without cloudinary
# attachment for the bot - text AND png
# output - attachments: transcript.txt, analysis.txt
# Only the attachments for now
if not query_message.content:
query_message.content = " "
resume_strings = []
if query_message.attachments:
for attachment in query_message.attachments:
if attachment.content_type.startswith("image/"):
print("parsing image", attachment.url)
success, resume_string = await parse_image_document_from_url(
attachment.url
)
# query_message.content += (
# f"\n\n This is the extracted text from the image: <text>\n{resume_string}\n<\text>"
# )
resume_strings.append(resume_string)
if resume_strings and False:
with open("transcript.txt", "w") as f:
f.write("\n\n".join(resume_strings))
with open("transcript.txt", "rb") as f:
txt_data = f.read()
_ = await self.post_message_attachment(
os.environ["POE_ACCESS_KEY"],
original_message_id,
file_data=txt_data,
filename="transcript.txt",
is_inline=False,
)
query_message.content += "\n\n" + RESUME_SUFFIX_PROMPT
current_message = ""
async for msg in stream_request(request, "RekaFlash", request.api_key):
# Note: See https://poe.com/ResumeReviewTool for the prompt
if isinstance(msg, MetaMessage):
continue
elif msg.is_suggested_reply:
yield self.suggested_reply_event(msg.text)
elif msg.is_replace_response:
yield self.replace_response_event(msg.text)
else:
current_message += msg.text
yield self.replace_response_event(current_message)
async def get_settings(self, setting: SettingsRequest) -> SettingsResponse:
return SettingsResponse(
server_bot_dependencies={"RekaFlash": 1},
allow_attachments=True, # to update when ready
introduction_message="Please upload a screenshot of your resume.",
)
bot = EchoBot()
image = (
Image.debian_slim()
.apt_install("libpoppler-cpp-dev")
.apt_install("tesseract-ocr-eng")
.pip_install(
"fastapi-poe==0.0.32",
"huggingface-hub==0.16.4",
"requests==2.31.0",
"pdftotext==2.2.2",
"Pillow==9.5.0",
"openai==0.27.8",
"pytesseract==0.3.10",
"python-docx",
)
).env({"POE_ACCESS_KEY": os.environ["POE_ACCESS_KEY"]})
stub = Stub("poe-bot-quickstart")
@stub.function(image=image)
@asgi_app()
def fastapi_app():
app = make_app(bot, api_key=os.environ["POE_ACCESS_KEY"])
return app