Sidhartha-Rajput commited on
Commit
50379ce
·
verified ·
1 Parent(s): c40185a

Upload Sid_GPT_Demo.ipynb with huggingface_hub

Browse files
Files changed (1) hide show
  1. Sid_GPT_Demo.ipynb +83 -0
Sid_GPT_Demo.ipynb ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Sid-GPT demo\n",
8
+ "A 92M-parameter GPT built from scratch that rewrites weak PM resume bullets into quantified, action-led ones.\n",
9
+ "\n",
10
+ "Run the cells top to bottom (Runtime \u2192 Run all). The last cell gives you a public link to a small web app."
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "metadata": {},
16
+ "execution_count": null,
17
+ "outputs": [],
18
+ "source": [
19
+ "!pip -q install torch tokenizers huggingface_hub gradio"
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "code",
24
+ "metadata": {},
25
+ "execution_count": null,
26
+ "outputs": [],
27
+ "source": [
28
+ "import sys, torch\n",
29
+ "from huggingface_hub import snapshot_download\n",
30
+ "path = snapshot_download(\"Sidhartha-Rajput/sid-gpt-resume-llm\")\n",
31
+ "sys.path.insert(0, path)\n",
32
+ "from model import GPT\n",
33
+ "from tokenizers import ByteLevelBPETokenizer\n",
34
+ "\n",
35
+ "tok = ByteLevelBPETokenizer(f\"{path}/tokenizer/vocab.json\", f\"{path}/tokenizer/merges.txt\")\n",
36
+ "ckpt = torch.load(f\"{path}/sft.pt\", map_location=\"cpu\", weights_only=False)\n",
37
+ "model = GPT(ckpt[\"config\"]); model.load_state_dict(ckpt[\"model\"]); model.eval()\n",
38
+ "UID, AID, EOT = (tok.token_to_id(t) for t in (\"<|user|>\", \"<|assistant|>\", \"<|endoftext|>\"))\n",
39
+ "TEMPLATE = \"Turn this into a strong, quantified PM resume bullet:\\n{}\"\n",
40
+ "\n",
41
+ "def rewrite(bullet, temperature=0.5, top_k=20, max_new_tokens=70):\n",
42
+ " ids = [UID] + tok.encode(TEMPLATE.format(bullet.strip())).ids + [AID]\n",
43
+ " with torch.no_grad():\n",
44
+ " out = model.generate(torch.tensor([ids]), max_new_tokens=int(max_new_tokens),\n",
45
+ " temperature=float(temperature), top_k=int(top_k))\n",
46
+ " new = out[0, len(ids):].tolist()\n",
47
+ " if EOT in new: new = new[:new.index(EOT)]\n",
48
+ " return tok.decode(new).strip()\n",
49
+ "\n",
50
+ "print(rewrite(\"Responsible for managing the product roadmap for the mobile app.\"))"
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "metadata": {},
56
+ "execution_count": null,
57
+ "outputs": [],
58
+ "source": [
59
+ "import gradio as gr\n",
60
+ "gr.Interface(\n",
61
+ " fn=rewrite,\n",
62
+ " inputs=[gr.Textbox(label=\"Your resume bullet\", lines=3),\n",
63
+ " gr.Slider(0.2, 1.2, value=0.5, label=\"Temperature\"),\n",
64
+ " gr.Slider(1, 100, value=20, step=1, label=\"Top-k\"),\n",
65
+ " gr.Slider(20, 120, value=70, step=5, label=\"Max new tokens\")],\n",
66
+ " outputs=gr.Textbox(label=\"Sid-GPT rewrite\", lines=4),\n",
67
+ " title=\"Sid-GPT: PM resume bullet rewriter\",\n",
68
+ ").launch(share=True)"
69
+ ]
70
+ }
71
+ ],
72
+ "metadata": {
73
+ "kernelspec": {
74
+ "display_name": "Python 3",
75
+ "name": "python3"
76
+ },
77
+ "language_info": {
78
+ "name": "python"
79
+ }
80
+ },
81
+ "nbformat": 4,
82
+ "nbformat_minor": 5
83
+ }