Curious-PM commited on
Commit
4fd2769
·
verified ·
1 Parent(s): 7f5e97d

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +19 -6
  2. app.py +108 -0
  3. requirements.txt +6 -0
README.md CHANGED
@@ -1,13 +1,26 @@
1
  ---
2
  title: Junior Associate
3
- emoji: 🦀
4
- colorFrom: yellow
5
- colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 6.14.0
8
- python_version: '3.13'
9
  app_file: app.py
10
  pinned: false
 
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: Junior Associate
3
+ emoji: 📄
4
+ colorFrom: indigo
5
+ colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 4.36.0
 
8
  app_file: app.py
9
  pinned: false
10
+ license: apache-2.0
11
+ hardware: zero-a10g
12
+ short_description: Qwen-3B fine-tuned for contract review in IRAC format
13
  ---
14
 
15
+ # Junior Associate · Contract Review in IRAC
16
+
17
+ Qwen 2.5-3B-Instruct fine-tuned with LoRA on 80 hand-crafted contract-review memos. Every reply produces the firm's IRAC house format with mandatory top + bottom disclaimers.
18
+
19
+ Built for the Curious PM "Stay Curious" session on fine-tuning. Trained via the [`hf-llm-trainer`](https://huggingface.co/blog/hf-skills-training) Claude skill on Hugging Face Jobs.
20
+
21
+ ## Model
22
+
23
+ - Base: `Qwen/Qwen2.5-3B-Instruct`
24
+ - Adapter: [`Curious-PM/lexwell-contract-irac-qwen2.5-3b-lora`](https://huggingface.co/Curious-PM/lexwell-contract-irac-qwen2.5-3b-lora)
25
+ - LoRA: `r=16`, `alpha=32`, attention modules only
26
+ - Training: 10 epochs, batch 2 × grad-accum 2, lr 2e-4, ~7 min on A10G
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Junior Associate — contract review in IRAC format.
2
+
3
+ Fine-tuned Qwen 2.5-3B-Instruct with a LoRA adapter trained on 80 hand-crafted
4
+ contract-review memos. Hosted on Hugging Face Spaces with ZeroGPU.
5
+ """
6
+ import torch
7
+ import spaces
8
+ import gradio as gr
9
+ from peft import PeftModel
10
+ from transformers import AutoModelForCausalLM, AutoTokenizer
11
+
12
+ BASE = "Qwen/Qwen2.5-3B-Instruct"
13
+ LORA = "Curious-PM/lexwell-contract-irac-qwen2.5-3b-lora"
14
+
15
+ SYSTEM_PROMPT = (
16
+ "You are an associate at Lexwell Advisors, a contract-review advisory "
17
+ "firm for SMBs. Reply in Lexwell's house IRAC format with required top "
18
+ "and bottom disclaimers."
19
+ )
20
+
21
+ # Load model + adapter once at startup
22
+ print(f"Loading base model {BASE} ...")
23
+ tokenizer = AutoTokenizer.from_pretrained(BASE)
24
+ base_model = AutoModelForCausalLM.from_pretrained(BASE, torch_dtype=torch.bfloat16)
25
+ print(f"Loading LoRA adapter {LORA} ...")
26
+ model = PeftModel.from_pretrained(base_model, LORA)
27
+ model.eval()
28
+ print("Model ready.")
29
+
30
+
31
+ @spaces.GPU(duration=60)
32
+ def generate_reply(question):
33
+ if not question or not question.strip():
34
+ return "_Type a contract question first._"
35
+
36
+ msgs = [
37
+ {"role": "system", "content": SYSTEM_PROMPT},
38
+ {"role": "user", "content": question.strip()},
39
+ ]
40
+ inputs = tokenizer.apply_chat_template(
41
+ msgs, return_tensors="pt", add_generation_prompt=True
42
+ ).to("cuda")
43
+ model.to("cuda")
44
+ with torch.no_grad():
45
+ out = model.generate(
46
+ inputs,
47
+ max_new_tokens=600,
48
+ do_sample=False,
49
+ pad_token_id=tokenizer.eos_token_id,
50
+ )
51
+ reply = tokenizer.decode(out[0][inputs.shape[1]:], skip_special_tokens=True)
52
+ return reply
53
+
54
+
55
+ CSS = """
56
+ .gradio-container { max-width: 980px !important; }
57
+ #title { text-align: center; margin-bottom: 4px; font-weight: 800; letter-spacing: -1px; }
58
+ #sub { text-align: center; color: #6B6B6B; margin-bottom: 24px; font-size: 14.5px; }
59
+ .output-box textarea, .output-box .markdown {
60
+ font-size: 14.5px !important; line-height: 1.6 !important;
61
+ }
62
+ """
63
+
64
+ EXAMPLES = [
65
+ "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?",
66
+ "Their non-compete is 2 years, all of California. Is that enforceable on a new hire?",
67
+ "Our enterprise customer wants source code escrow with release on bankruptcy, material breach, or product discontinuation. Push back?",
68
+ "We're hiring our first UK employee. Should we use an Employer of Record service or set up a UK subsidiary?",
69
+ "A vendor's MSA caps liability at $1M for any claim. Our annual fees are $500K and they hold our customer database. Is the cap reasonable?",
70
+ ]
71
+
72
+ with gr.Blocks(title="Junior Associate · Contract Review (IRAC)", css=CSS, theme=gr.themes.Soft()) as demo:
73
+ gr.HTML('<h1 id="title">Junior Associate</h1>')
74
+ gr.HTML(
75
+ '<div id="sub">Qwen 2.5-3B fine-tuned on 80 hand-crafted contract-review memos. '
76
+ 'Every reply: top disclaimer, IRAC analysis, numbered redlines, bottom disclaimer, sign-off. '
77
+ 'Built with the <a href="https://huggingface.co/blog/hf-skills-training" target="_blank">'
78
+ '<code>hf-llm-trainer</code></a> Claude skill on Hugging Face Jobs.</div>'
79
+ )
80
+
81
+ question = gr.Textbox(
82
+ label="Paste a contract clause or ask a question",
83
+ placeholder="e.g. Their MSA caps liability at $1M. Is that reasonable?",
84
+ lines=3,
85
+ )
86
+ submit = gr.Button("Ask the Junior Associate ▸", variant="primary", size="lg")
87
+
88
+ output = gr.Markdown(
89
+ value="_The reply will appear here. First request takes ~10s as the GPU warms up._",
90
+ elem_classes="output-box",
91
+ )
92
+
93
+ submit.click(fn=generate_reply, inputs=question, outputs=output)
94
+ question.submit(fn=generate_reply, inputs=question, outputs=output)
95
+
96
+ gr.Examples(examples=EXAMPLES, inputs=question, label="Try one of these")
97
+
98
+ gr.HTML(
99
+ '<div style="text-align: center; color: #888; font-size: 12px; margin-top: 32px;">'
100
+ 'Model: <a href="https://huggingface.co/Curious-PM/lexwell-contract-irac-qwen2.5-3b-lora" target="_blank">'
101
+ 'Curious-PM/lexwell-contract-irac-qwen2.5-3b-lora</a>'
102
+ ' · Built for <a href="https://curious.pm" target="_blank">Curious PM</a> &middot; Stay Curious session on fine-tuning'
103
+ '</div>'
104
+ )
105
+
106
+
107
+ if __name__ == "__main__":
108
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch>=2.4.0
2
+ transformers>=4.45.0
3
+ peft>=0.13.0
4
+ accelerate>=1.0.0
5
+ gradio>=4.36.0
6
+ spaces