File size: 8,564 Bytes
91a4119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "T4"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# πŸ”¬ Jan v1 Research Assistant - Google Colab Version\n",
        "\n",
        "Run Jan v1 (4B params) for FREE with Google Colab GPU!\n",
        "\n",
        "**Instructions:**\n",
        "1. Go to Runtime β†’ Change runtime type\n",
        "2. Select GPU: T4 (free)\n",
        "3. Run all cells\n",
        "4. Use the Gradio interface at the bottom"
      ],
      "metadata": {
        "id": "view-in-github"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 1️⃣ Install Dependencies"
      ],
      "metadata": {
        "id": "step1"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "install"
      },
      "outputs": [],
      "source": [
        "!pip install transformers torch gradio accelerate bitsandbytes sentencepiece beautifulsoup4 requests -q\n",
        "print(\"βœ… Dependencies installed!\")"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 2️⃣ Load Jan v1 Model"
      ],
      "metadata": {
        "id": "step2"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from transformers import AutoModelForCausalLM, AutoTokenizer\n",
        "import torch\n",
        "\n",
        "print(\"πŸš€ Loading Jan v1 model...\")\n",
        "model_name = \"janhq/Jan-v1-4B\"\n",
        "\n",
        "# Load with 8-bit quantization to save memory\n",
        "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
        "model = AutoModelForCausalLM.from_pretrained(\n",
        "    model_name,\n",
        "    torch_dtype=torch.float16,\n",
        "    device_map=\"auto\",\n",
        "    load_in_8bit=True\n",
        ")\n",
        "\n",
        "print(\"βœ… Model loaded successfully!\")\n",
        "print(f\"Model size: {model.num_parameters()/1e9:.2f}B parameters\")"
      ],
      "metadata": {
        "id": "load_model"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 3️⃣ Define Research Functions"
      ],
      "metadata": {
        "id": "step3"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import requests\n",
        "from bs4 import BeautifulSoup\n",
        "import gradio as gr\n",
        "\n",
        "def scrape_url(url: str) -> str:\n",
        "    \"\"\"Scrape and extract text from URL\"\"\"\n",
        "    try:\n",
        "        headers = {'User-Agent': 'Mozilla/5.0'}\n",
        "        response = requests.get(url, headers=headers, timeout=10)\n",
        "        soup = BeautifulSoup(response.content, 'html.parser')\n",
        "        \n",
        "        for script in soup([\"script\", \"style\"]):\n",
        "            script.decompose()\n",
        "        \n",
        "        text = soup.get_text()\n",
        "        lines = (line.strip() for line in text.splitlines())\n",
        "        chunks = (phrase.strip() for line in lines for phrase in line.split(\"  \"))\n",
        "        text = ' '.join(chunk for chunk in chunks if chunk)\n",
        "        \n",
        "        return text[:4000]\n",
        "    except Exception as e:\n",
        "        return f\"Error: {str(e)}\"\n",
        "\n",
        "def research_assistant(query: str, context: str = \"\", temperature: float = 0.6):\n",
        "    \"\"\"Main research function using Jan v1\"\"\"\n",
        "    \n",
        "    # Check if context is URL\n",
        "    if context.startswith('http'):\n",
        "        context = scrape_url(context)\n",
        "    \n",
        "    prompt = f\"\"\"You are an expert research analyst. Provide comprehensive analysis.\n",
        "\n",
        "Context: {context if context else 'No specific context'}\n",
        "\n",
        "Query: {query}\n",
        "\n",
        "Provide:\n",
        "1. Key findings\n",
        "2. Critical analysis\n",
        "3. Supporting evidence\n",
        "4. Follow-up questions\n",
        "\n",
        "Analysis:\"\"\"\n",
        "    \n",
        "    inputs = tokenizer(prompt, return_tensors=\"pt\", truncation=True, max_length=2048)\n",
        "    inputs = inputs.to(model.device)\n",
        "    \n",
        "    with torch.no_grad():\n",
        "        outputs = model.generate(\n",
        "            **inputs,\n",
        "            max_new_tokens=1024,\n",
        "            temperature=temperature,\n",
        "            top_p=0.95,\n",
        "            top_k=20,\n",
        "            do_sample=True,\n",
        "            pad_token_id=tokenizer.eos_token_id\n",
        "        )\n",
        "    \n",
        "    response = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
        "    response = response.replace(prompt, \"\").strip()\n",
        "    \n",
        "    return response\n",
        "\n",
        "print(\"βœ… Functions defined!\")"
      ],
      "metadata": {
        "id": "functions"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 4️⃣ Create Gradio Interface"
      ],
      "metadata": {
        "id": "step4"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create Gradio interface\n",
        "with gr.Blocks(title=\"Jan v1 Research Assistant\", theme=gr.themes.Soft()) as demo:\n",
        "    gr.Markdown(\"\"\"\n",
        "    # πŸ”¬ Jan v1 Research Assistant (Google Colab)\n",
        "    \n",
        "    Powered by Jan-v1-4B - Running on FREE Google Colab GPU!\n",
        "    \"\"\")\n",
        "    \n",
        "    with gr.Row():\n",
        "        with gr.Column():\n",
        "            query = gr.Textbox(\n",
        "                label=\"Research Query\",\n",
        "                placeholder=\"What would you like to research?\",\n",
        "                lines=2\n",
        "            )\n",
        "            context = gr.Textbox(\n",
        "                label=\"Context (text or URL)\",\n",
        "                placeholder=\"Paste text or URL to analyze\",\n",
        "                lines=5\n",
        "            )\n",
        "            temp = gr.Slider(0.1, 1.0, value=0.6, label=\"Temperature\")\n",
        "            btn = gr.Button(\"πŸ” Analyze\", variant=\"primary\")\n",
        "        \n",
        "        with gr.Column():\n",
        "            output = gr.Textbox(\n",
        "                label=\"Analysis Results\",\n",
        "                lines=15\n",
        "            )\n",
        "    \n",
        "    btn.click(\n",
        "        research_assistant,\n",
        "        inputs=[query, context, temp],\n",
        "        outputs=output\n",
        "    )\n",
        "    \n",
        "    gr.Examples(\n",
        "        examples=[\n",
        "            [\"What are the key trends in AI research?\", \"\", 0.6],\n",
        "            [\"Analyze this article for bias\", \"https://example.com/article\", 0.4],\n",
        "            [\"Generate research questions about climate change\", \"\", 0.7]\n",
        "        ],\n",
        "        inputs=[query, context, temp]\n",
        "    )\n",
        "\n",
        "# Launch the interface\n",
        "demo.launch(share=True)  # share=True creates a public link"
      ],
      "metadata": {
        "id": "gradio"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## πŸ“ Quick Test"
      ],
      "metadata": {
        "id": "test"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Test the model directly\n",
        "test_result = research_assistant(\n",
        "    \"What are the implications of large language models for research?\",\n",
        "    \"Large language models have billions of parameters and can process vast amounts of text.\"\n",
        ")\n",
        "\n",
        "print(\"Test Result:\")\n",
        "print(test_result)"
      ],
      "metadata": {
        "id": "test_code"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}