Spaces:
Sleeping
Sleeping
fix: bugs regarding event update; improve debug experience
Browse files- app.py +16 -6
- requirements.txt +1 -0
- taskAI.py +4 -2
- taskNonAI.py +2 -1
- util.py +4 -1
app.py
CHANGED
|
@@ -21,6 +21,9 @@ from pypandoc.pandoc_download import download_pandoc
|
|
| 21 |
import os
|
| 22 |
import json
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
logger = mylogger(__name__, "%(asctime)s:%(levelname)s:%(message)s")
|
| 25 |
info = logger.info
|
| 26 |
|
|
@@ -33,7 +36,7 @@ def init():
|
|
| 33 |
## Config Functions
|
| 34 |
|
| 35 |
|
| 36 |
-
def set_same_cheap_strong(set_same: bool, cheap_base, cheap_key, cheap_model):
|
| 37 |
# setup_zone = gr.Accordion("AI setup (OpenAI-compatible LLM API)", open=True)
|
| 38 |
if set_same:
|
| 39 |
return (
|
|
@@ -46,11 +49,11 @@ def set_same_cheap_strong(set_same: bool, cheap_base, cheap_key, cheap_model):
|
|
| 46 |
)
|
| 47 |
else:
|
| 48 |
return (
|
| 49 |
-
gr.Textbox(value=
|
| 50 |
gr.Textbox(
|
| 51 |
-
value=
|
| 52 |
),
|
| 53 |
-
gr.Textbox(value=
|
| 54 |
# setup_zone,
|
| 55 |
)
|
| 56 |
|
|
@@ -111,6 +114,7 @@ def finalize_letter_txt(api_base, api_key, api_model, debug_CoT):
|
|
| 111 |
for response in taskAI.purify_letter(full_text=debug_CoT):
|
| 112 |
result += response.delta
|
| 113 |
yield result
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
def finalize_letter_pdf(api_base, api_key, api_model, jd, cv, cover_letter_text, is_debug):
|
|
@@ -118,6 +122,8 @@ def finalize_letter_pdf(api_base, api_key, api_model, jd, cv, cover_letter_text,
|
|
| 118 |
taskAI = TaskAI(cheapAPI, temperature=0.1, max_tokens=100)
|
| 119 |
meta_data = next(taskAI.get_jobapp_meta(JD=jd, CV=cv))
|
| 120 |
pdf_context = json.loads(meta_data)
|
|
|
|
|
|
|
| 121 |
pdf_context["letter_body"] = cover_letter_text
|
| 122 |
return meta_data, compile_pdf(
|
| 123 |
pdf_context,
|
|
@@ -205,16 +211,20 @@ with gr.Blocks(
|
|
| 205 |
|
| 206 |
is_same_cheap_strong.change(
|
| 207 |
fn=set_same_cheap_strong,
|
| 208 |
-
inputs=[is_same_cheap_strong, cheap_base, cheap_key, cheap_model],
|
| 209 |
outputs=[strong_base, strong_key, strong_model],
|
| 210 |
)
|
| 211 |
|
| 212 |
infer_btn.click(
|
| 213 |
fn=set_same_cheap_strong,
|
| 214 |
-
inputs=[is_same_cheap_strong, cheap_base, cheap_key, cheap_model],
|
| 215 |
outputs=[strong_base, strong_key, strong_model],
|
| 216 |
).success(
|
| 217 |
fn=prepare_input, inputs=[jd_info, cv_file, cv_text], outputs=[jd_info, cv_text]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
).success(
|
| 219 |
fn=run_refine,
|
| 220 |
inputs=[cheap_base, cheap_key, cheap_model, jd_info, cv_text],
|
|
|
|
| 21 |
import os
|
| 22 |
import json
|
| 23 |
|
| 24 |
+
## debug
|
| 25 |
+
from icecream import ic
|
| 26 |
+
|
| 27 |
logger = mylogger(__name__, "%(asctime)s:%(levelname)s:%(message)s")
|
| 28 |
info = logger.info
|
| 29 |
|
|
|
|
| 36 |
## Config Functions
|
| 37 |
|
| 38 |
|
| 39 |
+
def set_same_cheap_strong(set_same: bool, cheap_base, cheap_key, cheap_model, strong_base, strong_key, strong_model):
|
| 40 |
# setup_zone = gr.Accordion("AI setup (OpenAI-compatible LLM API)", open=True)
|
| 41 |
if set_same:
|
| 42 |
return (
|
|
|
|
| 49 |
)
|
| 50 |
else:
|
| 51 |
return (
|
| 52 |
+
gr.Textbox(value=strong_base, label="API Base", interactive=True),
|
| 53 |
gr.Textbox(
|
| 54 |
+
value=strong_key, label="API key", type="password", interactive=True
|
| 55 |
),
|
| 56 |
+
gr.Textbox(value=strong_model, label="Model ID", interactive=True),
|
| 57 |
# setup_zone,
|
| 58 |
)
|
| 59 |
|
|
|
|
| 114 |
for response in taskAI.purify_letter(full_text=debug_CoT):
|
| 115 |
result += response.delta
|
| 116 |
yield result
|
| 117 |
+
|
| 118 |
|
| 119 |
|
| 120 |
def finalize_letter_pdf(api_base, api_key, api_model, jd, cv, cover_letter_text, is_debug):
|
|
|
|
| 122 |
taskAI = TaskAI(cheapAPI, temperature=0.1, max_tokens=100)
|
| 123 |
meta_data = next(taskAI.get_jobapp_meta(JD=jd, CV=cv))
|
| 124 |
pdf_context = json.loads(meta_data)
|
| 125 |
+
if is_debug:
|
| 126 |
+
ic(pdf_context)
|
| 127 |
pdf_context["letter_body"] = cover_letter_text
|
| 128 |
return meta_data, compile_pdf(
|
| 129 |
pdf_context,
|
|
|
|
| 211 |
|
| 212 |
is_same_cheap_strong.change(
|
| 213 |
fn=set_same_cheap_strong,
|
| 214 |
+
inputs=[is_same_cheap_strong, cheap_base, cheap_key, cheap_model, strong_base, strong_key, strong_model],
|
| 215 |
outputs=[strong_base, strong_key, strong_model],
|
| 216 |
)
|
| 217 |
|
| 218 |
infer_btn.click(
|
| 219 |
fn=set_same_cheap_strong,
|
| 220 |
+
inputs=[is_same_cheap_strong, cheap_base, cheap_key, cheap_model, strong_base, strong_key, strong_model],
|
| 221 |
outputs=[strong_base, strong_key, strong_model],
|
| 222 |
).success(
|
| 223 |
fn=prepare_input, inputs=[jd_info, cv_file, cv_text], outputs=[jd_info, cv_text]
|
| 224 |
+
).success(
|
| 225 |
+
fn=lambda: gr.Accordion("Reformatting", open=True),
|
| 226 |
+
inputs=None,
|
| 227 |
+
outputs=[reformat_zone],
|
| 228 |
).success(
|
| 229 |
fn=run_refine,
|
| 230 |
inputs=[cheap_base, cheap_key, cheap_model, jd_info, cv_text],
|
requirements.txt
CHANGED
|
@@ -9,5 +9,6 @@ llama-index-llms-openai-like
|
|
| 9 |
tiktoken
|
| 10 |
# lib
|
| 11 |
requests
|
|
|
|
| 12 |
# dev tools
|
| 13 |
ruff
|
|
|
|
| 9 |
tiktoken
|
| 10 |
# lib
|
| 11 |
requests
|
| 12 |
+
icecream
|
| 13 |
# dev tools
|
| 14 |
ruff
|
taskAI.py
CHANGED
|
@@ -7,6 +7,8 @@ from llama_index.core import ChatPromptTemplate
|
|
| 7 |
from util import mylogger
|
| 8 |
from util import checkAPI
|
| 9 |
|
|
|
|
|
|
|
| 10 |
logger = mylogger(__name__, "%(asctime)s:%(filename)s:%(levelname)s:%(message)s")
|
| 11 |
## define templates
|
| 12 |
|
|
@@ -129,7 +131,7 @@ class TaskAI(OpenAILike):
|
|
| 129 |
meta_CV = self.chat(
|
| 130 |
JSON_API.format_messages(
|
| 131 |
template=keys_to_template(
|
| 132 |
-
["
|
| 133 |
),
|
| 134 |
content=CV,
|
| 135 |
)
|
|
@@ -139,7 +141,7 @@ class TaskAI(OpenAILike):
|
|
| 139 |
meta_JD = json.loads(meta_JD.strip())
|
| 140 |
meta_CV = json.loads(meta_CV.strip())
|
| 141 |
except Exception as e:
|
| 142 |
-
|
| 143 |
raise ValueError(
|
| 144 |
f"AI didn't return a valid JSON string. Try again or consider a better model for CheapAI. \n{meta_JD}\n{meta_CV}"
|
| 145 |
)
|
|
|
|
| 7 |
from util import mylogger
|
| 8 |
from util import checkAPI
|
| 9 |
|
| 10 |
+
from icecream import ic
|
| 11 |
+
|
| 12 |
logger = mylogger(__name__, "%(asctime)s:%(filename)s:%(levelname)s:%(message)s")
|
| 13 |
## define templates
|
| 14 |
|
|
|
|
| 131 |
meta_CV = self.chat(
|
| 132 |
JSON_API.format_messages(
|
| 133 |
template=keys_to_template(
|
| 134 |
+
["applicantFullName", "applicantContactInformation"]
|
| 135 |
),
|
| 136 |
content=CV,
|
| 137 |
)
|
|
|
|
| 141 |
meta_JD = json.loads(meta_JD.strip())
|
| 142 |
meta_CV = json.loads(meta_CV.strip())
|
| 143 |
except Exception as e:
|
| 144 |
+
ic(e)
|
| 145 |
raise ValueError(
|
| 146 |
f"AI didn't return a valid JSON string. Try again or consider a better model for CheapAI. \n{meta_JD}\n{meta_CV}"
|
| 147 |
)
|
taskNonAI.py
CHANGED
|
@@ -11,6 +11,8 @@ from string import Template
|
|
| 11 |
from datetime import datetime
|
| 12 |
from pathlib import Path
|
| 13 |
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def file_to_html(file_path: str) -> str:
|
| 16 |
return pypandoc.convert_file(file_path, "html")
|
|
@@ -56,7 +58,6 @@ def _ensure_no_signature_in_body(cover_letter_body: str) -> str:
|
|
| 56 |
if not cover_letter_body.strip().endswith(","):
|
| 57 |
# remove last line
|
| 58 |
cover_letter_body = "\n".join(cover_letter_body.split("\n")[:-1])
|
| 59 |
-
print(cover_letter_body)
|
| 60 |
return cover_letter_body
|
| 61 |
|
| 62 |
def compile_pdf(
|
|
|
|
| 11 |
from datetime import datetime
|
| 12 |
from pathlib import Path
|
| 13 |
|
| 14 |
+
from icecream import ic
|
| 15 |
+
|
| 16 |
|
| 17 |
def file_to_html(file_path: str) -> str:
|
| 18 |
return pypandoc.convert_file(file_path, "html")
|
|
|
|
| 58 |
if not cover_letter_body.strip().endswith(","):
|
| 59 |
# remove last line
|
| 60 |
cover_letter_body = "\n".join(cover_letter_body.split("\n")[:-1])
|
|
|
|
| 61 |
return cover_letter_body
|
| 62 |
|
| 63 |
def compile_pdf(
|
util.py
CHANGED
|
@@ -4,6 +4,8 @@ from urllib.parse import urlparse
|
|
| 4 |
import requests
|
| 5 |
import logging
|
| 6 |
|
|
|
|
|
|
|
| 7 |
from typing import Generator
|
| 8 |
|
| 9 |
|
|
@@ -38,7 +40,8 @@ def is_valid_openai_api_key(api_base: str, api_key: str) -> bool:
|
|
| 38 |
headers = {"Authorization": f"Bearer {api_key}"}
|
| 39 |
test_url = f"{api_base}/models"
|
| 40 |
response = requests.get(test_url, headers=headers)
|
| 41 |
-
|
|
|
|
| 42 |
return response.status_code == 200
|
| 43 |
|
| 44 |
|
|
|
|
| 4 |
import requests
|
| 5 |
import logging
|
| 6 |
|
| 7 |
+
from icecream import ic
|
| 8 |
+
|
| 9 |
from typing import Generator
|
| 10 |
|
| 11 |
|
|
|
|
| 40 |
headers = {"Authorization": f"Bearer {api_key}"}
|
| 41 |
test_url = f"{api_base}/models"
|
| 42 |
response = requests.get(test_url, headers=headers)
|
| 43 |
+
if response.status_code != 200:
|
| 44 |
+
ic(response.json())
|
| 45 |
return response.status_code == 200
|
| 46 |
|
| 47 |
|