File size: 15,035 Bytes
c10c2de
b22f922
 
 
 
02fdb50
766be8d
74164b2
73906d6
aef6b86
b22f922
dee4e98
9087846
b22f922
 
9087846
b22f922
 
9087846
b22f922
 
74164b2
b22f922
e82f42a
 
 
e2121e2
 
 
9087846
b22f922
 
9087846
b22f922
e2121e2
 
 
 
 
 
 
b22f922
c10c2de
 
 
fff4b96
 
 
 
 
 
 
 
 
9087846
ccc2190
 
 
 
 
 
 
 
 
a86d269
c10c2de
9087846
 
 
 
 
 
 
 
c10c2de
9087846
e82f42a
9087846
e82f42a
9087846
e82f42a
9087846
 
 
caa2ecf
 
df3485e
c10c2de
 
 
9087846
02fdb50
b22f922
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02fdb50
 
9087846
02fdb50
9087846
b22f922
 
 
32e427d
02fdb50
b22f922
 
32e427d
b22f922
 
9087846
73906d6
b22f922
73906d6
02fdb50
 
 
 
 
b22f922
9087846
32e427d
02fdb50
 
 
9087846
32e427d
 
02fdb50
b22f922
9087846
ccc2190
 
 
32e427d
 
 
74164b2
e82f42a
 
74164b2
9087846
 
766be8d
 
73906d6
9087846
 
74164b2
b22f922
 
ccc2190
 
 
02fdb50
b22f922
ccc2190
7b4e4a9
2948d70
b22f922
 
376b8ed
ccc2190
b22f922
7b4e4a9
f08712c
 
 
 
 
53e8d77
f08712c
 
 
3e126b2
f08712c
 
 
 
716bf54
f08712c
 
ece0a10
f08712c
 
ece0a10
578a712
ccc2190
b22f922
 
9087846
 
 
 
 
b22f922
6770dec
b22f922
fff4b96
 
8bfb6c8
fff4b96
 
b22f922
9087846
 
 
b22f922
02fdb50
b22f922
ccc2190
b22f922
9087846
 
 
fff4b96
 
8bfb6c8
fff4b96
 
b22f922
9087846
b22f922
 
 
 
6770dec
 
 
b22f922
 
 
 
02fdb50
b22f922
02fdb50
b22f922
 
 
9087846
32e427d
 
 
 
 
 
 
 
 
 
b22f922
 
02fdb50
 
a6b89f7
 
02fdb50
 
 
 
b22f922
 
1b4d3b3
b22f922
 
 
64b6fe3
 
 
2708402
9087846
 
 
ccc2190
 
 
 
 
 
 
 
 
9087846
f08712c
766be8d
 
 
9087846
fff4b96
 
 
 
 
 
54587d2
 
 
 
fff4b96
 
 
 
 
 
54587d2
 
 
 
fff4b96
9087846
64b6fe3
9087846
ccc2190
 
 
 
 
 
 
 
 
9087846
e0f6e72
 
 
 
caa2ecf
 
a86d269
9087846
e82f42a
 
 
 
a86d269
b22f922
02fdb50
b22f922
9087846
 
 
 
 
 
 
 
 
73906d6
9087846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b4d3b3
9087846
 
 
64b6fe3
cdc8d8c
 
 
 
 
 
 
 
 
 
 
 
 
b22f922
 
 
 
74164b2
b22f922
 
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
from config import DEMO_TITLE, IS_SHARE, IS_DEBUG, CV_EXT, EXT_TXT
from config import CHEAP_API_BASE, CHEAP_API_KEY, CHEAP_MODEL
from config import STRONG_API_BASE, STRONG_API_KEY, STRONG_MODEL
from util import is_valid_url
from util import mylogger
from util import stream_together
# from util import checkAPI
from taskNonAI import extract_url, file_to_html, compile_pdf
from taskAI import TaskAI

## load data
from _data_test import mock_jd, mock_cv

## ui
import gradio as gr

## dependency
from pypandoc.pandoc_download import download_pandoc

## std
import os
import json

## debug
from icecream import ic

## platform specific
from _hf import HF, REPO_ID

logger = mylogger(__name__, "%(asctime)s:%(levelname)s:%(message)s")
info = logger.info


def init():
    try:
        os.system("shot-scraper install -b firefox")
        download_pandoc()
    except Exception as e:
        ic(e)
        if HF:
            HF.restart_space(REPO_ID)


## Config Functions

def apply_quick_config(config_json, target_base, target_key, target_model):
    try:
        config = json.loads(config_json)
        updated_base = config.get("base", target_base)
        updated_key = config.get("key", target_key)
        updated_model = config.get("model", target_model)
        return updated_base, updated_key, updated_model
    except json.JSONDecodeError:
        return target_base, target_key, target_model  # Keep original values if JSON is invalid

def set_same_cheap_strong(
    set_same: bool,
    cheap_base,
    cheap_key,
    cheap_model,
    strong_base,
    strong_key,
    strong_model,
):
    # setup_zone = gr.Accordion("AI setup (OpenAI-compatible LLM API)", open=True)
    if set_same:
        return (
            gr.Textbox(value=cheap_base, label="API Base", interactive=False),
            gr.Textbox(
                value=cheap_key, label="API key", type="password", interactive=False
            ),
            gr.Textbox(value=cheap_model, label="Model ID", interactive=False),
            # setup_zone,
        )
    else:
        return (
            gr.Textbox(value=strong_base, label="API Base", interactive=True),
            gr.Textbox(
                value=strong_key, label="API key", type="password", interactive=True
            ),
            gr.Textbox(value=strong_model, label="Model ID", interactive=True),
            # setup_zone,
        )

def check_API_Key_provision(cheap_key, strong_key):
    if not cheap_key or not strong_key:
        raise ValueError("Please set your API Key before you start. You can get one from https://beta.openai.com/account/api-keys. Please read the in-App guide carefully.")

## Main Functions


def prepare_input(jd_info, cv_file: str, cv_text):
    if jd_info:
        if is_valid_url(jd_info):
            jd = extract_url(jd_info)
        else:
            jd = jd_info
    else:
        jd = mock_jd

    if cv_text:
        cv = cv_text
    elif cv_file:
        if any([cv_file.endswith(ext) for ext in EXT_TXT]):
            with open(cv_file, "r", encoding="utf8") as f:
                cv = f.read()
        else:
            cv = file_to_html(cv_file)
    else:
        cv = mock_cv
    return jd, cv


def run_refine(api_base, api_key, api_model, jd_info, cv_text):
    jd, cv = jd_info, cv_text
    cheapAPI = {"base": api_base, "key": api_key, "model": api_model}
    taskAI = TaskAI(cheapAPI, temperature=0.2, max_tokens=2048)  # max_tokens=2048
    info("API initialized")
    gen = stream_together(
        taskAI.jd_preprocess(input=jd),
        taskAI.cv_preprocess(input=cv),
    )
    for result in gen:
        yield result


def run_compose(api_base, api_key, api_model, min_jd, min_cv, is_debug):
    strongAPI = {"base": api_base, "key": api_key, "model": api_model}
    taskAI = TaskAI(strongAPI, is_debug=is_debug, temperature=0.6, max_tokens=4000)
    info("Composing letter with CoT ...")
    result = ""
    for response in taskAI.compose_letter_CoT(jd=min_jd, resume=min_cv):
        result += response.delta
        yield result


def finalize_letter_txt(api_base, api_key, api_model, debug_CoT):
    cheapAPI = {"base": api_base, "key": api_key, "model": api_model}
    taskAI = TaskAI(cheapAPI, temperature=0.2, max_tokens=2048)
    info("Finalizing letter ...")
    result = ""
    for response in taskAI.purify_letter(full_text=debug_CoT):
        result += response.delta
        yield result


def finalize_letter_pdf(
    api_base, api_key, api_model, jd, cv, cover_letter_text, is_debug
):
    cheapAPI = {"base": api_base, "key": api_key, "model": api_model}
    taskAI = TaskAI(cheapAPI, temperature=0.1, max_tokens=100)
    meta_data = next(taskAI.get_jobapp_meta(JD=jd, CV=cv))
    pdf_context = json.loads(meta_data)
    if is_debug:
        ic(pdf_context)
    pdf_context["letter_body"] = cover_letter_text
    return meta_data, compile_pdf(
        pdf_context,
        tmpl_path="app/typst/template_letter.tmpl",
        output_path=f"cover_letter_by_{pdf_context['applicantFullName']}_to_{pdf_context['companyFullName']}.pdf",
        is_debug=is_debug,
    )


with gr.Blocks(
    title=DEMO_TITLE,
    theme=gr.themes.Soft(
        primary_hue="sky", secondary_hue="emerald", neutral_hue="stone"
    ),
) as app:
    intro = f"""# {DEMO_TITLE}
    > You provide job description and résumé, and I write Cover letter for you!  

    **IMPORTANT** API Keys are not provided by default. In order to enable AI capability, you have to ***BRING YOUR OWN API Key***. 
    """
    gr.Markdown(intro)
    with gr.Accordion("=== IMPORTANT: READ ME FIRST ===", open=False):
        guide = gr.Markdown("""## Setup

    > **IMPORTANT**: First setup **API Keys** for 2 AI agents, CheapAI and StrongAI, on the "AI Setup" panel before you start. If you know what you are doing, change other API configurations as well.

    + `API Key`: If you have no idea, go to https://beta.openai.com/account/api-keys  

    + `Model ID`: 
        - **CheapAI**: 
            + `gpt-3.5-turbo` any variant should be fine unless OpenAI make them lazier and dumber.  
            + `Mistral-7B-Instruct-v0.1` works well
            + `gemma-7b-it` usually can't understand instructions properly and cause error.
        - **StrongAI**: 
            + `Mistral-7B-Instruct-v0.1` can do the job.
            + Models with small context window size like won't work, such as
                - `gpt-3.5-turbo-0613`
                -  perhaps `gpt-4-0613`
    
    Model choice can be an interesting topic involing quite a few experiments. Feel free to try any other models you like!

    ## Troubleshooting
    - If your API is on localhost / local network, you may need to host this app on local network too.
    - If your API is on some AI service platform, review if you have enough balance / credits / quota on the platform.
    - If you are sure that you have set up the API correctly, but encounter an error along the way, try click the "Go!" button again.
    - Try change AI provider / model.
    - Report to [GitHub issue](https://github.com/cxumol/CoverPilot/issues) if you believe it's a bug.
""")
    with gr.Row():
        with gr.Column(scale=1):
            with gr.Accordion(
                "AI setup (OpenAI-compatible LLM API)", open=False
            ) as setup_zone:
                is_debug = gr.Checkbox(label="Debug Mode", value=IS_DEBUG)

                gr.Markdown(
                    "**CheapAI**, an honest format converter and refiner, extracts essential info from job description and résumé."
                )
                
                with gr.Row():
                    quick_config_cheapAI = gr.Textbox(label="Quick Config (JSON)",placeholder='{"base":"https://api.example.com/v1",\n"key":"sk-",\n"model":"gpt-123"}')
                    apply_cheap_btn = gr.Button("Apply Quick Config", variant="secondary")

                with gr.Group():
                    cheap_base = gr.Textbox(value=CHEAP_API_BASE, label="API Base")
                    cheap_key = gr.Textbox(
                        value=CHEAP_API_KEY, label="API key", type="password"
                    )
                    cheap_model = gr.Textbox(value=CHEAP_MODEL, label="Model ID")
                gr.Markdown(
                    "---\n**StrongAI**, a thoughtful wordsmith, generates perfect cover letters to make both you and recruiters happy."
                )
                is_same_cheap_strong = gr.Checkbox(
                    label="the same as Cheap AI", value=False, container=False
                )

                with gr.Row():
                    quick_config_strongAI = gr.Textbox(label="Quick Config (JSON)",placeholder='{"base":"https://api.example.com/v1",\n"key":"sk-",\n"model":"gpt-456"}')
                    apply_strong_btn = gr.Button("Apply Quick Config", variant="secondary")
                    
                with gr.Group():
                    strong_base = gr.Textbox(value=STRONG_API_BASE, label="API Base")
                    strong_key = gr.Textbox(
                        value=STRONG_API_KEY, label="API key", type="password"
                    )
                    strong_model = gr.Textbox(value=STRONG_MODEL, label="Model ID")
                gr.Markdown(
                    "> Thanks to **CheapAI**'s work, your spending on **StrongAI** is substaintially saved.."
                )
            with gr.Group():
                gr.Markdown("## Employer - Job Description")
                jd_info = gr.Textbox(
                    label="Job Description",
                    placeholder="Paste as Full Text (recommmend) or URL",
                    lines=5,
                    max_lines=10,
                )
            with gr.Group():
                gr.Markdown("## Applicant - CV / Résumé")
                # with gr.Row():
                cv_file = gr.File(
                    label="Allowed formats: " + " ".join(CV_EXT),
                    file_count="single",
                    file_types=CV_EXT,
                    type="filepath",
                )
                cv_text = gr.TextArea(
                    label="Or enter text",
                    placeholder="If attempting to both upload a file and enter text, only this text will be used.",
                )
        with gr.Column(scale=2):
            gr.Markdown("## Result")
            with gr.Accordion("Reformatting", open=True) as reformat_zone:
                with gr.Row():
                    min_jd = gr.TextArea(label="Clean verion of Job Description")
                    min_cv = gr.TextArea(label="Clean verion of CV / Résumé")
            with gr.Accordion("Expert Zone", open=False) as expert_zone:
                debug_CoT = gr.Textbox(label="Chain of Thoughts")
                debug_jobapp = gr.Textbox(label="Job application meta data")
            cover_letter_text = gr.Textbox(label="Cover Letter")
            cover_letter_pdf = gr.File(
                label="Cover Letter PDF",
                file_count="multiple",
                type="filepath",
            )
            infer_btn = gr.Button("Go!", variant="primary")
            with gr.Row():
                cancel_btn = gr.Button("Cancel", variant="secondary")
                regen_pdf_btn = gr.Button("Re-generate PDF", variant="secondary")
            gr.Markdown("#### Like it? Give me a star ⭐ [on GitHub](https://github.com/cxumol/CoverPilot)!")

    is_same_cheap_strong.change(
        fn=set_same_cheap_strong,
        inputs=[
            is_same_cheap_strong,
            cheap_base,
            cheap_key,
            cheap_model,
            strong_base,
            strong_key,
            strong_model,
        ],
        outputs=[strong_base, strong_key, strong_model],
    ).then(
        fn=lambda: gr.Accordion("AI setup (OpenAI-compatible LLM API)", open=True),
        inputs=None,
        outputs=[setup_zone],
    )

    # apply_quick_config
    apply_cheap_btn.click(
        fn=apply_quick_config,
        inputs=[quick_config_cheapAI, cheap_base, cheap_key, cheap_model],
        outputs=[cheap_base, cheap_key, cheap_model],
    ).then(
        fn=lambda: gr.Accordion("AI setup (OpenAI-compatible LLM API)", open=True),
        inputs=None,
        outputs=[setup_zone],
    )

    apply_strong_btn.click(
        fn=apply_quick_config,
        inputs=[quick_config_strongAI, strong_base, strong_key, strong_model],
        outputs=[strong_base, strong_key, strong_model],
    ).then(
        fn=lambda: gr.Accordion("AI setup (OpenAI-compatible LLM API)", open=True),
        inputs=None,
        outputs=[setup_zone],
    )

    main_click_event = infer_btn.click(
        fn=set_same_cheap_strong,
        inputs=[
            is_same_cheap_strong,
            cheap_base,
            cheap_key,
            cheap_model,
            strong_base,
            strong_key,
            strong_model,
        ],
        outputs=[strong_base, strong_key, strong_model],
    ).then(
        fn=lambda: gr.Accordion("AI setup (OpenAI-compatible LLM API)", open=False),
        inputs=None,
        outputs=[setup_zone],
    ).success(
        fn=check_API_Key_provision, inputs=[cheap_key, strong_key], outputs=None
    ).success(
        fn=prepare_input, inputs=[jd_info, cv_file, cv_text], outputs=[jd_info, cv_text]
    ).success(
        fn=lambda: gr.Accordion("Reformatting", open=True),
        inputs=None,
        outputs=[reformat_zone],
    ).success(
        fn=run_refine,
        inputs=[cheap_base, cheap_key, cheap_model, jd_info, cv_text],
        outputs=[min_jd, min_cv],
    ).success(
        fn=lambda: [
            gr.Accordion("Expert Zone", open=True),
            gr.Accordion("Reformatting", open=False),
        ],
        inputs=None,
        outputs=[expert_zone, reformat_zone],
    ).success(
        fn=run_compose,
        inputs=[strong_base, strong_key, strong_model, min_jd, min_cv, is_debug],
        outputs=[debug_CoT],
    ).success(
        fn=lambda: gr.Accordion("Expert Zone", open=False),
        inputs=None,
        outputs=[expert_zone],
    ).success(
        fn=finalize_letter_txt,
        inputs=[cheap_base, cheap_key, cheap_model, debug_CoT],
        outputs=[cover_letter_text],
    ).success(
        fn=finalize_letter_pdf,
        inputs=[
            cheap_base,
            cheap_key,
            cheap_model,
            jd_info,
            cv_text,
            cover_letter_text,
            is_debug,
        ],
        outputs=[debug_jobapp, cover_letter_pdf],
    )
    cancel_btn.click(fn=None, inputs=None, outputs=None, cancels=[main_click_event])
    regen_pdf_btn.click(
        fn=finalize_letter_pdf,
        inputs=[
            cheap_base,
            cheap_key,
            cheap_model,
            jd_info,
            cv_text,
            cover_letter_text,
            is_debug,
        ],
        outputs=[debug_jobapp, cover_letter_pdf],
    )


if __name__ == "__main__":
    init()
    app.queue(max_size=1, default_concurrency_limit=1).launch(
        show_error=True, debug=True, share=IS_SHARE
    )