Georgefifth commited on
Commit
c09d0f4
·
verified ·
1 Parent(s): f518f05

Upload demo_colab.ipynb with huggingface_hub

Browse files
Files changed (1) hide show
  1. demo_colab.ipynb +118 -0
demo_colab.ipynb ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Tiny Browser Planner — Live Demo\n",
8
+ "**Reason-First model** | MiniCPM5-1B + LoRA\n",
9
+ "\n",
10
+ "Run all cells below. At the end, a public URL will appear — click it to use the demo."
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": null,
16
+ "metadata": {},
17
+ "outputs": [],
18
+ "source": [
19
+ "# Install dependencies\n",
20
+ "!pip install unsloth gradio datasets transformers torch --quiet\n",
21
+ "!pip install bitsandbytes accelerate --quiet"
22
+ ]
23
+ },
24
+ {
25
+ "cell_type": "code",
26
+ "execution_count": null,
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "import re, torch\n",
31
+ "from unsloth import FastLanguageModel\n",
32
+ "import gradio as gr\n",
33
+ "\n",
34
+ "MODEL_ID = \"Georgefifth/tiny-browser-planner-reason\"\n",
35
+ "\n",
36
+ "print(\"Loading model...\")\n",
37
+ "model, tokenizer = FastLanguageModel.from_pretrained(\n",
38
+ " MODEL_ID, max_seq_length=2048, load_in_4bit=True, dtype=torch.bfloat16,\n",
39
+ ")\n",
40
+ "model = FastLanguageModel.get_peft_model(model, r=16,\n",
41
+ " target_modules=['q_proj','k_proj','v_proj','o_proj','gate_proj','up_proj','down_proj'],\n",
42
+ " lora_alpha=16)\n",
43
+ "model.load_adapter(MODEL_ID, 'default')\n",
44
+ "FastLanguageModel.for_inference(model)\n",
45
+ "print(\"Loaded!\")"
46
+ ]
47
+ },
48
+ {
49
+ "cell_type": "code",
50
+ "execution_count": null,
51
+ "metadata": {},
52
+ "outputs": [],
53
+ "source": [
54
+ "def predict(task, history_text):\n",
55
+ " history = [l.strip() for l in history_text.strip().split(chr(10)) if l.strip()]\n",
56
+ " hist_str = chr(10).join(history)\n",
57
+ " msgs = [\n",
58
+ " {'role': 'system', 'content': 'You are a browser planner. First reason about the situation, then output the next action.'},\n",
59
+ " {'role': 'user', 'content': f'Task: {task}\\n\\nHistory:\\n{hist_str}\\n\\nWhat is the next action?'},\n",
60
+ " ]\n",
61
+ " prompt = tokenizer.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)\n",
62
+ " inputs = tokenizer(prompt, return_tensors='pt').to('cuda')\n",
63
+ " input_len = inputs['input_ids'].shape[1]\n",
64
+ " outs = model.generate(**inputs, max_new_tokens=64, temperature=0.01, do_sample=False, pad_token_id=tokenizer.eos_token_id)\n",
65
+ " output = tokenizer.decode(outs[0][input_len:], skip_special_tokens=True).strip()\n",
66
+ " reason_m = re.search(r'Reason:\\s*(.+?)(?:\\n|\\$)', output)\n",
67
+ " action_m = re.search(r'Action:\\s*(\\S+)', output)\n",
68
+ " return (reason_m.group(1).strip() if reason_m else '?'), (action_m.group(1).strip().lower() if action_m else '?')"
69
+ ]
70
+ },
71
+ {
72
+ "cell_type": "code",
73
+ "execution_count": null,
74
+ "metadata": {},
75
+ "outputs": [],
76
+ "source": [
77
+ "PRESETS = [\n",
78
+ " ('Find Apple stock price', '[search] Search completed.\\n[open_page] Price displayed prominently at $198'),\n",
79
+ " ('Find Apple stock price', '[search] Search completed.\\n[open_page] Product review page, not stock data'),\n",
80
+ " ('Find CEO of OpenAI', '[search] Search completed.\\n[open_page] API pricing page, not CEO info'),\n",
81
+ " ('Find AWS EC2 pricing', '[search] Search completed.\\n[open_page] Pricing behind login wall'),\n",
82
+ " ('Find Python 3.12 release date', '[search] Search completed.\\n[open_page] Release date listed on official page'),\n",
83
+ " ('Find Tesla Model Y price', '[search] Search completed.\\n[open_page] Shows Model 3 pricing, not Model Y'),\n",
84
+ "]\n",
85
+ "\n",
86
+ "with gr.Blocks(title='Tiny Browser Planner', theme='soft') as demo:\n",
87
+ " gr.Markdown('''# Tiny Browser Planner\n",
88
+ "**Reason-First** — MiniCPM5-1B + LoRA | Actions: search, open_page, extract, refine_search, back, finish''')\n",
89
+ " task = gr.Textbox(label='Task', placeholder='Find Apple stock price')\n",
90
+ " history = gr.Textbox(label='History (one action per line)', lines=4,\n",
91
+ " placeholder='[search] Search completed.\\n[open_page] Price displayed')\n",
92
+ " btn = gr.Button('Predict', variant='primary')\n",
93
+ " with gr.Row():\n",
94
+ " reason = gr.Textbox(label='Reason', interactive=False, lines=2)\n",
95
+ " action = gr.Textbox(label='Action', interactive=False, lines=1)\n",
96
+ " btn.click(fn=predict, inputs=[task, history], outputs=[reason, action])\n",
97
+ " \n",
98
+ " gr.Markdown('### Quick Examples')\n",
99
+ " for t, h in PRESETS:\n",
100
+ " gr.Button(t, size='sm').click(\n",
101
+ " fn=lambda t=t, h=h: (t, h), outputs=[task, history]\n",
102
+ " ).then(fn=predict, inputs=[task, history], outputs=[reason, action])\n",
103
+ "\n",
104
+ "print('\\n=== Click the URL below to open the demo ===\\n')\n",
105
+ "demo.launch(share=True, server_name='0.0.0.0')"
106
+ ]
107
+ }
108
+ ],
109
+ "metadata": {
110
+ "accelerator": "GPU",
111
+ "language_info": {
112
+ "name": "python",
113
+ "version": "3.10"
114
+ }
115
+ },
116
+ "nbformat": 4,
117
+ "nbformat_minor": 4
118
+ }