forked from poe-platform/server-bot-quick-start
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbot_TesseractOCR.py
271 lines (199 loc) · 9.06 KB
/
bot_TesseractOCR.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
BOT_NAME="TesseractOCR"; 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:
https://pjreddie.com/static/Redmon%20Resume.pdf
"""
from __future__ import annotations
import os
from collections import defaultdict
from io import BytesIO
from typing import AsyncIterable
import requests
from fastapi_poe import PoeBot, make_app
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
SETTINGS = {
"report_feedback": True,
"context_clear_window_secs": 60 * 60,
"allow_user_context_clear": True,
}
conversation_cache = defaultdict(
lambda: [{"role": "system", "content": RESUME_SYSTEM_PROMPT}]
)
url_cache = {}
async def parse_image_document_from_url(image_url: str) -> tuple[bool, str]:
import pytesseract
print("version", pytesseract.get_tesseract_version())
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, ""
async def parse_pdf_document_from_url(pdf_url: str) -> tuple[bool, str]:
import pdftotext
try:
response = requests.get(pdf_url)
with BytesIO(response.content) as f:
pdf = pdftotext.PDF(f)
text = "\n\n".join(pdf)
text = text[:10000]
return True, text
except requests.exceptions.MissingSchema:
return False, ""
except BaseException:
return False, ""
async def parse_pdf_document_from_docx(docx_url: str) -> tuple[bool, str]:
from docx import Document
try:
response = requests.get(docx_url)
with BytesIO(response.content) as f:
document = Document(f)
text = [p.text for p in document.paragraphs]
text = "\n\n".join(text)
text = text[:10000]
return True, text
except requests.exceptions.MissingSchema as e:
print(e)
return False, ""
except BaseException as e:
print(e)
return False, ""
UPDATE_IMAGE_PARSING = """\
I am parsing your resume with Tesseract OCR ...
---
"""
# TODO: show an image, if Markdown support for that happens before image upload
UPDATE_LLM_QUERY = """\
I have received your resume.
{resume}
I am querying the language model for analysis ...
---
"""
MULTIWORD_FAILURE_REPLY = """\
Please only send a URL.
Do not include any other words in your reply.
You can get an image URL by uploading to https://postimages.org/
These are examples of resume the bot can accept.
https://raw.githubusercontent.com/jakegut/resume/master/resume.png
https://i.postimg.cc/3r0fZ5gy/resume.png
See https://poe.com/huikang/1512927999933968 for an example of an interaction.
You can also try https://poe.com/xyzFormatter for advice specifically on your bullet points.
"""
PARSE_FAILURE_REPLY = """
I could not load your resume.
---
Please upload your resume to https://postimages.org/ and reply its direct link.
---
Please ensure that you are sending something like
https://i.postimg.cc/3r0fZ5gy/resume.png
rather than
https://postimg.cc/LhRVHWQR/9fca0e7d
---
This bot is not able to accept links from Google drive.
This bot is not able to read images from Imgur.
Remember to redact sensitive information, especially contact details.
"""
# flake8: noqa: E501
RESUME_SYSTEM_PROMPT = """
You will be given text from a resume, extracted with Optical Character Recognition.
You will suggest specific improvements for a resume, by the standards of US/Canada software industry.
Do not give generic comments.
All comments has to quote the relevant sentence in the resume where there is an issue.
You will only check the resume text for formatting errors, and suggest improvements to the bullet points.
You will not evaluate the resume, as your role is to suggest improvements.
You will focus on your comments related to tech and engineering content.
Avoid commenting on extra-curricular activities.
The following are the formmatting errors to check.
If there is a formatting error, quote the original text, and suggest how should it be rewritten.
Only raise these errors if you are confident that this is an error.
- Inconsistent date formats. Prefer Mmm YYYY for date formats.
- Misuse of capitalization. Do not capitalize words that are not capitalized in professional communication.
- Misspelling of technical terminologies. (Ignore if the error is likely to due OCR parsing inaccuracies.)
- The candidate should not explictly label their level of proficiency in the skills section.
Suggest improvements to bullet points according to these standards.
Quote the original text (always), and suggest how should it be rewritten.
- Emulate the Google XYZ formula - e.g. Accomplished X, as measured by Y, by doing Z
- Ensure the bullet points are specific.
It shows exactly what feature or system the applicant worked on, and their exact contribution.
- Specify the exact method or discovery where possible.
- Ensure the metrics presented by the resume can be objectively measured.
Do not use unmeasurable metrics like “effectiveness” or “efficiency”.
- You may assume numbers of the metrics in your recommendations.
- You may assume additional facts not mentioned in the bullet points in your recommendations.
- Prefer simpler sentence structures and active language
- Instead of "Spearheaded development ...", write "Developed ..."
- Instead of "Utilized Python to increase the performance of ...", write "Increased the performance of ... with Python"
Please suggest only the most important improvements to the resume. All your suggestions should quote from the resume.
Each suggestion should start with "Suggestion X" (e.g. Suggestion 1), and followed by two new lines.
In the suggestion, quote from the resume, and write what you suggest to improve.
At the end of each suggestion, add a markdown horizontal rule, which is `---`.
Do not reproduce the full resume unless asked. You will not evaluate the resume, as your role is to suggest improvements.
"""
RESUME_STARTING_PROMPT = """
The resume is contained within the following triple backticks
```
{}
```
"""
class TesseractOCRBot(PoeBot):
async def get_response(self, query: QueryRequest) -> AsyncIterable[ServerSentEvent]:
user_statement: str = query.query[-1].content
print(query.conversation_id, user_statement)
if (
query.query[-1].attachments
and query.query[-1].attachments[0].content_type == "application/pdf"
):
content_url = query.query[-1].attachments[0].url
print("parsing pdf", content_url)
success, resume_string = await parse_pdf_document_from_url(content_url)
elif query.query[-1].attachments and query.query[-1].attachments[
0
].content_type.endswith("document"):
content_url = query.query[-1].attachments[0].url
print("parsing docx", content_url)
success, resume_string = await parse_pdf_document_from_docx(content_url)
elif query.query[-1].attachments and query.query[-1].attachments[
0
].content_type.startswith("image"):
content_url = query.query[-1].attachments[0].url
print("parsing image", content_url)
success, resume_string = await parse_image_document_from_url(content_url)
# TODO: parse other types of documents
elif query.conversation_id not in url_cache:
# TODO: validate user_statement is not malicious
if len(user_statement.strip().split()) > 1:
yield self.text_event(MULTIWORD_FAILURE_REPLY)
return
content_url = user_statement.strip()
content_url = content_url.split("?")[0] # remove query_params
# yield self.text_event(UPDATE_IMAGE_PARSING)
if content_url.endswith(".pdf"):
print("parsing pdf", content_url)
success, resume_string = await parse_pdf_document_from_url(content_url)
elif content_url.endswith(".docx"):
print("parsing docx", content_url)
success, resume_string = await parse_pdf_document_from_docx(content_url)
else: # assume image
print("parsing image", content_url)
success, resume_string = await parse_image_document_from_url(
content_url
)
print(resume_string[:100])
if not success:
yield self.text_event(PARSE_FAILURE_REPLY)
return
yield self.replace_response_event(resume_string)
return
async def get_settings(self, setting: SettingsRequest) -> SettingsResponse:
return SettingsResponse(
server_bot_dependencies={},
allow_attachments=True,
introduction_message="Please upload your document (pdf, docx).",
)