File size: 15,785 Bytes
23b42e6
4fd2769
23b42e6
 
 
4fd2769
 
 
 
 
 
 
 
 
 
d358637
 
 
 
 
ac2728e
4fd2769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23b42e6
 
 
 
ac2728e
 
 
 
 
 
 
 
 
 
 
23b42e6
 
4fd2769
23b42e6
 
4fd2769
 
23b42e6
 
 
 
 
f21e85d
23b42e6
ac2728e
 
23b42e6
 
ac2728e
 
23b42e6
ac2728e
 
23b42e6
ac2728e
 
23b42e6
 
4fd2769
 
 
 
 
 
 
eef18b5
 
4fd2769
 
eef18b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23b42e6
 
eef18b5
 
 
 
 
 
 
 
 
 
 
23b42e6
eef18b5
 
 
 
 
 
 
 
 
 
23b42e6
eef18b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23b42e6
eef18b5
 
 
 
d8efce2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eef18b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23b42e6
eef18b5
 
 
 
 
 
 
 
 
 
 
 
 
 
23b42e6
eef18b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23b42e6
eef18b5
23b42e6
eef18b5
 
 
23b42e6
eef18b5
 
 
 
 
 
 
23b42e6
eef18b5
23b42e6
eef18b5
 
 
 
 
 
23b42e6
eef18b5
 
 
 
 
 
 
 
23b42e6
 
 
 
eef18b5
 
23b42e6
 
 
 
eef18b5
23b42e6
 
 
 
 
 
 
eef18b5
23b42e6
 
 
 
 
 
 
 
 
 
 
 
 
eef18b5
23b42e6
 
 
 
 
 
eef18b5
23b42e6
 
eef18b5
23b42e6
 
 
 
 
eef18b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8efce2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eef18b5
d8efce2
 
 
 
 
 
 
eef18b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23b42e6
eef18b5
 
 
 
 
 
23b42e6
4fd2769
 
23b42e6
4fd2769
eef18b5
 
4fd2769
eef18b5
23b42e6
eef18b5
23b42e6
eef18b5
 
 
 
 
 
 
23b42e6
 
 
 
 
910dedd
23b42e6
 
 
 
 
 
 
 
910dedd
23b42e6
 
 
4fd2769
eef18b5
 
 
23b42e6
 
4fd2769
eef18b5
 
23b42e6
eef18b5
4fd2769
 
 
 
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
"""Junior Associate β€” side-by-side comparison of base vs fine-tuned Qwen-3B.

Both sides receive the same input and same system prompt. The only difference
is whether the LoRA adapter is enabled. Hosted on Hugging Face Spaces with
ZeroGPU.
"""
import torch
import spaces
import gradio as gr
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer

BASE = "Qwen/Qwen2.5-3B-Instruct"
LORA = "Curious-PM/lexwell-contract-irac-qwen2.5-3b-lora"

BASE_PROMPT = (
    "You are a helpful assistant. Answer in one or two short paragraphs "
    "of plain English. Do not use headings, bullet lists, numbered lists, "
    "section labels, or markdown formatting. Do not add disclaimers."
)
FT_PROMPT = (
    "You are an associate at Lexwell Advisors, a contract-review advisory "
    "firm for SMBs. Reply in Lexwell's house IRAC format with required top "
    "and bottom disclaimers."
)

# Load model + adapter once at startup
print(f"Loading base model {BASE} ...")
tokenizer = AutoTokenizer.from_pretrained(BASE)
base_model = AutoModelForCausalLM.from_pretrained(BASE, torch_dtype=torch.bfloat16)
print(f"Loading LoRA adapter {LORA} ...")
model = PeftModel.from_pretrained(base_model, LORA)
model.eval()
print("Model ready.")


def _decode(out, input_len):
    return tokenizer.decode(out[0][input_len:], skip_special_tokens=True)


def _prepare_inputs(system_prompt, user_question):
    msgs = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_question.strip()},
    ]
    text = tokenizer.apply_chat_template(
        msgs, tokenize=False, add_generation_prompt=True
    )
    return tokenizer(text, return_tensors="pt").to("cuda")


@spaces.GPU(duration=120)
def generate_both(question):
    if not question or not question.strip():
        placeholder = "_Type a contract question first._"
        return placeholder, placeholder

    model.to("cuda")

    gen_kwargs = dict(
        max_new_tokens=600,
        do_sample=False,
        pad_token_id=tokenizer.eos_token_id,
    )

    # LEFT β€” base model + generic "helpful assistant" prompt
    base_inputs = _prepare_inputs(BASE_PROMPT, question)
    with model.disable_adapter():
        with torch.no_grad():
            base_out = model.generate(**base_inputs, **gen_kwargs)
    base_reply = _decode(base_out, base_inputs.input_ids.shape[1])

    # RIGHT β€” fine-tuned + specific Lexwell prompt
    ft_inputs = _prepare_inputs(FT_PROMPT, question)
    with torch.no_grad():
        ft_out = model.generate(**ft_inputs, **gen_kwargs)
    ft_reply = _decode(ft_out, ft_inputs.input_ids.shape[1])

    return base_reply, ft_reply


EXAMPLES = [
    "Our SaaS vendor wants us to sign: 'Customer grants Vendor a perpetual, irrevocable license to use Customer Data for any purpose, including ML training.' Is this normal?",
    "Their non-compete is 2 years, all of California. Is that enforceable on a new hire?",
    "Our enterprise customer wants source code escrow with release on bankruptcy, material breach, or product discontinuation. Push back?",
    "We're hiring our first UK employee. Should we use an Employer of Record service or set up a UK subsidiary?",
    "A vendor's MSA caps liability at $1M. Our annual fees are $500K and they hold our customer database. Reasonable?",
    "Standard force majeure language β€” anything to flag?",
]

EXAMPLE_LABELS = [
    "Vendor wants perpetual ML training rights",
    "California non-compete (2 years)",
    "Source code escrow on bankruptcy",
    "EOR vs UK subsidiary for first hire",
    "MSA liability cap at $1M",
    "Standard force majeure clause",
]


CSS = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&family=JetBrains+Mono:wght@400;600&display=swap');

:root {
  --ink: #0A0A0A;
  --paper: #FFFFFF;
  --off: #FAFAFA;
  --tint: #F4F4F5;
  --hair: #E4E4E4;
  --rule: #C8C8C8;
  --muted: #6B6B6B;
  --subtle: #A1A1AA;
  --left-accent: #C8C8C8;
  --right-accent: #0A0A0A;
}

* { box-sizing: border-box; }

body, .gradio-container {
  background: var(--off) !important;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
  color: var(--ink) !important;
}

.gradio-container {
  max-width: 1280px !important;
  margin: 0 auto !important;
  padding: 56px 32px 80px !important;
}

footer { display: none !important; }
.show-api, .built-with { display: none !important; }

/* ── HEADER ───────────────────────────────────────────── */
#header-block {
  text-align: center;
  margin-bottom: 40px;
}
#header-block .title-row {
  font-family: 'Inter', sans-serif;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 2.4px;
  color: var(--muted);
  text-transform: uppercase;
  margin-bottom: 12px;
}
#header-block h1 {
  font-family: 'Inter', sans-serif !important;
  font-size: 64px !important;
  font-weight: 900 !important;
  letter-spacing: -2.4px !important;
  margin: 0 0 16px 0 !important;
  line-height: 0.98 !important;
  color: var(--ink) !important;
}
#header-block .subtitle {
  font-size: 16px;
  color: var(--muted);
  max-width: 720px;
  margin: 0 auto;
  line-height: 1.6;
  font-weight: 500;
}
#header-block .use-case {
  margin: 32px auto 0;
  max-width: 1100px;
  text-align: left;
  border-top: 1px solid var(--hair);
  border-bottom: 1px solid var(--hair);
  padding: 22px 0;
}
#header-block .use-case-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 32px;
}
#header-block .use-case-label {
  font-family: 'Inter', sans-serif;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 1.6px;
  color: var(--muted);
  margin-bottom: 6px;
}
#header-block .use-case-text {
  font-family: 'Inter', sans-serif;
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--ink);
  font-weight: 500;
}
#header-block .use-case-text b { font-weight: 700; }
#header-block .use-case-text i { color: var(--muted); }

#header-block .how-to-read {
  margin-top: 18px;
  padding: 14px 18px;
  background: var(--tint);
  border-radius: 8px;
  font-family: 'Inter', sans-serif;
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--ink);
  text-align: left;
  max-width: 1100px;
  margin-left: auto;
  margin-right: auto;
}
#header-block .how-to-read b { font-weight: 700; }

#header-block .meta-pill {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin-top: 22px;
  padding: 8px 16px;
  background: var(--tint);
  border-radius: 999px;
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 11.5px;
  color: var(--ink);
}
#header-block .meta-pill .label {
  font-family: 'Inter', sans-serif;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 1.6px;
  color: var(--muted);
}
#header-block .meta-pill a {
  color: var(--ink);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.section-label {
  font-family: 'Inter', sans-serif;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--muted);
  text-transform: uppercase;
  margin: 28px 0 10px;
}

/* ── INPUT ─────────────────────────────────────────────── */
.gr-textbox, textarea {
  border-radius: 10px !important;
  border: 1px solid var(--hair) !important;
  background: var(--paper) !important;
  padding: 16px 18px !important;
  font-family: 'Inter', sans-serif !important;
  font-size: 15px !important;
  line-height: 1.5 !important;
  color: var(--ink) !important;
  box-shadow: none !important;
}
textarea:focus { border-color: var(--ink) !important; outline: none !important; }

/* ── BUTTONS ────────────────────────────────────────────── */
button.primary, .primary-button button {
  background: var(--ink) !important;
  color: var(--paper) !important;
  border: none !important;
  border-radius: 10px !important;
  font-family: 'Inter', sans-serif !important;
  font-weight: 700 !important;
  font-size: 15px !important;
  letter-spacing: 0.2px !important;
  padding: 14px 28px !important;
  cursor: pointer !important;
  width: 100% !important;
  margin-top: 16px !important;
  transition: background 0.15s ease !important;
}
button.primary:hover, .primary-button button:hover { background: #2A2A2A !important; }

.examples-row {
  display: grid !important;
  grid-template-columns: 1fr 1fr 1fr !important;
  gap: 10px !important;
  margin-top: 8px !important;
}
.example-btn button {
  background: var(--paper) !important;
  border: 1px solid var(--hair) !important;
  border-radius: 8px !important;
  padding: 12px 14px !important;
  font-family: 'Inter', sans-serif !important;
  font-size: 12.5px !important;
  font-weight: 500 !important;
  color: var(--ink) !important;
  text-align: left !important;
  cursor: pointer !important;
  width: 100% !important;
  min-height: 48px !important;
  line-height: 1.4 !important;
  white-space: normal !important;
  transition: all 0.15s ease !important;
}
.example-btn button:hover {
  background: var(--tint) !important;
  border-color: var(--rule) !important;
}

/* ── COMPARISON OUTPUT (TWO COLUMNS) ───────────────────── */
#comparison-row { gap: 18px !important; }

.lane {
  background: var(--paper);
  border: 1px solid var(--hair);
  border-radius: 10px;
  padding: 0;
  overflow: hidden;
  min-height: 200px;
}
.lane.left-lane { border-top: 4px solid var(--left-accent); }
.lane.right-lane { border-top: 4px solid var(--right-accent); }

.lane-header {
  padding: 16px 22px 12px 22px;
  border-bottom: 1px solid var(--hair);
  background: var(--off);
}
.lane-header .lane-title {
  font-family: 'Inter', sans-serif;
  font-size: 16px;
  font-weight: 800;
  color: var(--ink);
  letter-spacing: -0.3px;
  margin: 0;
}
.lane-header .lane-meta {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
}

.lane-body, .lane-body * {
  font-family: 'Inter', sans-serif !important;
  font-size: 13.5px !important;
  line-height: 1.65 !important;
  color: var(--ink) !important;
}
.lane-body {
  padding: 20px 24px 24px 24px !important;
}
.lane-body strong { font-weight: 700 !important; }
.lane-body em { color: var(--muted) !important; font-style: italic !important; }
.lane-body p { margin: 0 0 10px 0 !important; }
.lane-body ol, .lane-body ul { padding-left: 22px !important; margin: 6px 0 10px 0 !important; }
.lane-body li { margin-bottom: 4px !important; }

/* ── FOOTER ────────────────────────────────────────────── */
#footer-block {
  text-align: center;
  margin-top: 48px;
  padding-top: 24px;
  border-top: 1px solid var(--hair);
  font-size: 12px;
  color: var(--muted);
  line-height: 1.6;
}
#footer-block a {
  color: var(--ink);
  text-decoration: underline;
  text-underline-offset: 2px;
}
"""


HEADER_HTML = """
<div id="header-block">
  <div class="title-row">Demo 03 Β· Stay Curious Β· Fine-Tuning LLMs</div>
  <h1>Junior Associate</h1>
  <div class="subtitle">
    A small open-source model fine-tuned to do <b>first-pass contract review</b>
    in the house style of a fictional B2B law firm.
  </div>

  <div class="use-case">
    <div class="use-case-row">
      <div class="use-case-cell">
        <div class="use-case-label">THE FIRM</div>
        <div class="use-case-text">
          <b>Lexwell Advisors</b> (fictional) reviews contracts for SMBs.
          Every reply must follow the firm&rsquo;s exact format.
        </div>
      </div>
      <div class="use-case-cell">
        <div class="use-case-label">THE HOUSE STYLE</div>
        <div class="use-case-text">
          Top disclaimer &rarr; <b>I</b>ssue &middot; <b>R</b>ule &middot; <b>A</b>pplication &middot; <b>C</b>onclusion
          &rarr; numbered redlines &rarr; bottom disclaimer &rarr;
          <i>&mdash; Lexwell Advisors</i>
        </div>
      </div>
      <div class="use-case-cell">
        <div class="use-case-label">WHY FINE-TUNE</div>
        <div class="use-case-text">
          Instead of a 2,000-token system prompt on every call,
          we fine-tuned Qwen-3B on <b>80 example memos</b>.
          The structure now lives in a 30&nbsp;MB adapter.
        </div>
      </div>
    </div>
  </div>

  <div class="how-to-read">
    <b>How to read this:</b> paste a contract question below.
    The <b>left column</b> shows the base model with no fine-tuning &mdash; helpful, but unstructured.
    The <b>right column</b> shows the same model with the LoRA adapter &mdash; locked to Lexwell&rsquo;s format every time.
  </div>

  <div class="meta-pill">
    <span class="label">MODEL</span>
    <a href="https://huggingface.co/Curious-PM/lexwell-contract-irac-qwen2.5-3b-lora" target="_blank">Curious-PM/lexwell-contract-irac-qwen2.5-3b-lora</a>
    <span style="color: var(--rule);">Β·</span>
    <span style="color: var(--muted);">trained via</span>
    <a href="https://huggingface.co/blog/hf-skills-training" target="_blank">hf-llm-trainer</a>
  </div>
</div>
"""

FOOTER_HTML = """
<div id="footer-block">
  Built for <a href="https://curious.pm" target="_blank">Curious PM</a> &middot; Stay Curious session on fine-tuning &middot; <a href="https://huggingface.co/Curious-PM/lexwell-contract-irac-qwen2.5-3b-lora" target="_blank">View the model</a>
</div>
"""

PLACEHOLDER = "_The reply will appear here. First request takes ~15s while the GPU warms up._"


with gr.Blocks(title="Junior Associate Β· Stay Curious", css=CSS, theme=gr.themes.Base()) as demo:

    gr.HTML(HEADER_HTML)

    gr.HTML('<div class="section-label">Ask a contract question</div>')

    question = gr.Textbox(
        placeholder="e.g. Our SaaS vendor wants perpetual ML training rights on our customer data. Is that normal?",
        lines=3,
        show_label=False,
        container=False,
    )

    submit = gr.Button("Compare base vs fine-tuned β†’", elem_classes="primary-button", variant="primary")

    gr.HTML('<div class="section-label">Or try one of these</div>')

    with gr.Row(elem_classes="examples-row"):
        example_buttons = []
        for label, full_text in zip(EXAMPLE_LABELS, EXAMPLES):
            btn = gr.Button(label, elem_classes="example-btn")
            example_buttons.append((btn, full_text))

    with gr.Row(elem_id="comparison-row"):
        with gr.Column(elem_classes="lane left-lane"):
            gr.HTML(
                '<div class="lane-header">'
                '  <div class="lane-title">Base Qwen 2.5-3B</div>'
                '  <div class="lane-meta">no fine-tuning</div>'
                '</div>'
            )
            base_output = gr.Markdown(value=PLACEHOLDER, elem_classes="lane-body")

        with gr.Column(elem_classes="lane right-lane"):
            gr.HTML(
                '<div class="lane-header">'
                '  <div class="lane-title">+ LoRA adapter (fine-tuned)</div>'
                '  <div class="lane-meta">trained on 80 contract-review memos</div>'
                '</div>'
            )
            ft_output = gr.Markdown(value=PLACEHOLDER, elem_classes="lane-body")

    gr.HTML(FOOTER_HTML)

    # Wire up
    submit.click(fn=generate_both, inputs=question, outputs=[base_output, ft_output])
    question.submit(fn=generate_both, inputs=question, outputs=[base_output, ft_output])

    for btn, full_text in example_buttons:
        btn.click(fn=lambda t=full_text: t, inputs=None, outputs=question).then(
            fn=generate_both, inputs=question, outputs=[base_output, ft_output]
        )


if __name__ == "__main__":
    demo.launch()