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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": [],
|
| 7 |
+
"gpuType": "T4"
|
| 8 |
+
},
|
| 9 |
+
"kernelspec": {
|
| 10 |
+
"name": "python3",
|
| 11 |
+
"display_name": "Python 3"
|
| 12 |
+
},
|
| 13 |
+
"language_info": {
|
| 14 |
+
"name": "python"
|
| 15 |
+
},
|
| 16 |
+
"accelerator": "GPU"
|
| 17 |
+
},
|
| 18 |
+
"cells": [
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "markdown",
|
| 21 |
+
"source": [
|
| 22 |
+
"# π¬ Jan v1 Research Assistant - Google Colab Version\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"Run Jan v1 (4B params) for FREE with Google Colab GPU!\n",
|
| 25 |
+
"\n",
|
| 26 |
+
"**Instructions:**\n",
|
| 27 |
+
"1. Go to Runtime β Change runtime type\n",
|
| 28 |
+
"2. Select GPU: T4 (free)\n",
|
| 29 |
+
"3. Run all cells\n",
|
| 30 |
+
"4. Use the Gradio interface at the bottom"
|
| 31 |
+
],
|
| 32 |
+
"metadata": {
|
| 33 |
+
"id": "view-in-github"
|
| 34 |
+
}
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"cell_type": "markdown",
|
| 38 |
+
"source": [
|
| 39 |
+
"## 1οΈβ£ Install Dependencies"
|
| 40 |
+
],
|
| 41 |
+
"metadata": {
|
| 42 |
+
"id": "step1"
|
| 43 |
+
}
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"cell_type": "code",
|
| 47 |
+
"execution_count": null,
|
| 48 |
+
"metadata": {
|
| 49 |
+
"id": "install"
|
| 50 |
+
},
|
| 51 |
+
"outputs": [],
|
| 52 |
+
"source": [
|
| 53 |
+
"!pip install transformers torch gradio accelerate bitsandbytes sentencepiece beautifulsoup4 requests -q\n",
|
| 54 |
+
"print(\"β
Dependencies installed!\")"
|
| 55 |
+
]
|
| 56 |
+
},
|
| 57 |
+
{
|
| 58 |
+
"cell_type": "markdown",
|
| 59 |
+
"source": [
|
| 60 |
+
"## 2οΈβ£ Load Jan v1 Model"
|
| 61 |
+
],
|
| 62 |
+
"metadata": {
|
| 63 |
+
"id": "step2"
|
| 64 |
+
}
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"cell_type": "code",
|
| 68 |
+
"source": [
|
| 69 |
+
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
|
| 70 |
+
"import torch\n",
|
| 71 |
+
"\n",
|
| 72 |
+
"print(\"π Loading Jan v1 model...\")\n",
|
| 73 |
+
"model_name = \"janhq/Jan-v1-4B\"\n",
|
| 74 |
+
"\n",
|
| 75 |
+
"# Load with 8-bit quantization to save memory\n",
|
| 76 |
+
"tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
|
| 77 |
+
"model = AutoModelForCausalLM.from_pretrained(\n",
|
| 78 |
+
" model_name,\n",
|
| 79 |
+
" torch_dtype=torch.float16,\n",
|
| 80 |
+
" device_map=\"auto\",\n",
|
| 81 |
+
" load_in_8bit=True\n",
|
| 82 |
+
")\n",
|
| 83 |
+
"\n",
|
| 84 |
+
"print(\"β
Model loaded successfully!\")\n",
|
| 85 |
+
"print(f\"Model size: {model.num_parameters()/1e9:.2f}B parameters\")"
|
| 86 |
+
],
|
| 87 |
+
"metadata": {
|
| 88 |
+
"id": "load_model"
|
| 89 |
+
},
|
| 90 |
+
"execution_count": null,
|
| 91 |
+
"outputs": []
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"cell_type": "markdown",
|
| 95 |
+
"source": [
|
| 96 |
+
"## 3οΈβ£ Define Research Functions"
|
| 97 |
+
],
|
| 98 |
+
"metadata": {
|
| 99 |
+
"id": "step3"
|
| 100 |
+
}
|
| 101 |
+
},
|
| 102 |
+
{
|
| 103 |
+
"cell_type": "code",
|
| 104 |
+
"source": [
|
| 105 |
+
"import requests\n",
|
| 106 |
+
"from bs4 import BeautifulSoup\n",
|
| 107 |
+
"import gradio as gr\n",
|
| 108 |
+
"\n",
|
| 109 |
+
"def scrape_url(url: str) -> str:\n",
|
| 110 |
+
" \"\"\"Scrape and extract text from URL\"\"\"\n",
|
| 111 |
+
" try:\n",
|
| 112 |
+
" headers = {'User-Agent': 'Mozilla/5.0'}\n",
|
| 113 |
+
" response = requests.get(url, headers=headers, timeout=10)\n",
|
| 114 |
+
" soup = BeautifulSoup(response.content, 'html.parser')\n",
|
| 115 |
+
" \n",
|
| 116 |
+
" for script in soup([\"script\", \"style\"]):\n",
|
| 117 |
+
" script.decompose()\n",
|
| 118 |
+
" \n",
|
| 119 |
+
" text = soup.get_text()\n",
|
| 120 |
+
" lines = (line.strip() for line in text.splitlines())\n",
|
| 121 |
+
" chunks = (phrase.strip() for line in lines for phrase in line.split(\" \"))\n",
|
| 122 |
+
" text = ' '.join(chunk for chunk in chunks if chunk)\n",
|
| 123 |
+
" \n",
|
| 124 |
+
" return text[:4000]\n",
|
| 125 |
+
" except Exception as e:\n",
|
| 126 |
+
" return f\"Error: {str(e)}\"\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"def research_assistant(query: str, context: str = \"\", temperature: float = 0.6):\n",
|
| 129 |
+
" \"\"\"Main research function using Jan v1\"\"\"\n",
|
| 130 |
+
" \n",
|
| 131 |
+
" # Check if context is URL\n",
|
| 132 |
+
" if context.startswith('http'):\n",
|
| 133 |
+
" context = scrape_url(context)\n",
|
| 134 |
+
" \n",
|
| 135 |
+
" prompt = f\"\"\"You are an expert research analyst. Provide comprehensive analysis.\n",
|
| 136 |
+
"\n",
|
| 137 |
+
"Context: {context if context else 'No specific context'}\n",
|
| 138 |
+
"\n",
|
| 139 |
+
"Query: {query}\n",
|
| 140 |
+
"\n",
|
| 141 |
+
"Provide:\n",
|
| 142 |
+
"1. Key findings\n",
|
| 143 |
+
"2. Critical analysis\n",
|
| 144 |
+
"3. Supporting evidence\n",
|
| 145 |
+
"4. Follow-up questions\n",
|
| 146 |
+
"\n",
|
| 147 |
+
"Analysis:\"\"\"\n",
|
| 148 |
+
" \n",
|
| 149 |
+
" inputs = tokenizer(prompt, return_tensors=\"pt\", truncation=True, max_length=2048)\n",
|
| 150 |
+
" inputs = inputs.to(model.device)\n",
|
| 151 |
+
" \n",
|
| 152 |
+
" with torch.no_grad():\n",
|
| 153 |
+
" outputs = model.generate(\n",
|
| 154 |
+
" **inputs,\n",
|
| 155 |
+
" max_new_tokens=1024,\n",
|
| 156 |
+
" temperature=temperature,\n",
|
| 157 |
+
" top_p=0.95,\n",
|
| 158 |
+
" top_k=20,\n",
|
| 159 |
+
" do_sample=True,\n",
|
| 160 |
+
" pad_token_id=tokenizer.eos_token_id\n",
|
| 161 |
+
" )\n",
|
| 162 |
+
" \n",
|
| 163 |
+
" response = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
|
| 164 |
+
" response = response.replace(prompt, \"\").strip()\n",
|
| 165 |
+
" \n",
|
| 166 |
+
" return response\n",
|
| 167 |
+
"\n",
|
| 168 |
+
"print(\"β
Functions defined!\")"
|
| 169 |
+
],
|
| 170 |
+
"metadata": {
|
| 171 |
+
"id": "functions"
|
| 172 |
+
},
|
| 173 |
+
"execution_count": null,
|
| 174 |
+
"outputs": []
|
| 175 |
+
},
|
| 176 |
+
{
|
| 177 |
+
"cell_type": "markdown",
|
| 178 |
+
"source": [
|
| 179 |
+
"## 4οΈβ£ Create Gradio Interface"
|
| 180 |
+
],
|
| 181 |
+
"metadata": {
|
| 182 |
+
"id": "step4"
|
| 183 |
+
}
|
| 184 |
+
},
|
| 185 |
+
{
|
| 186 |
+
"cell_type": "code",
|
| 187 |
+
"source": [
|
| 188 |
+
"# Create Gradio interface\n",
|
| 189 |
+
"with gr.Blocks(title=\"Jan v1 Research Assistant\", theme=gr.themes.Soft()) as demo:\n",
|
| 190 |
+
" gr.Markdown(\"\"\"\n",
|
| 191 |
+
" # π¬ Jan v1 Research Assistant (Google Colab)\n",
|
| 192 |
+
" \n",
|
| 193 |
+
" Powered by Jan-v1-4B - Running on FREE Google Colab GPU!\n",
|
| 194 |
+
" \"\"\")\n",
|
| 195 |
+
" \n",
|
| 196 |
+
" with gr.Row():\n",
|
| 197 |
+
" with gr.Column():\n",
|
| 198 |
+
" query = gr.Textbox(\n",
|
| 199 |
+
" label=\"Research Query\",\n",
|
| 200 |
+
" placeholder=\"What would you like to research?\",\n",
|
| 201 |
+
" lines=2\n",
|
| 202 |
+
" )\n",
|
| 203 |
+
" context = gr.Textbox(\n",
|
| 204 |
+
" label=\"Context (text or URL)\",\n",
|
| 205 |
+
" placeholder=\"Paste text or URL to analyze\",\n",
|
| 206 |
+
" lines=5\n",
|
| 207 |
+
" )\n",
|
| 208 |
+
" temp = gr.Slider(0.1, 1.0, value=0.6, label=\"Temperature\")\n",
|
| 209 |
+
" btn = gr.Button(\"π Analyze\", variant=\"primary\")\n",
|
| 210 |
+
" \n",
|
| 211 |
+
" with gr.Column():\n",
|
| 212 |
+
" output = gr.Textbox(\n",
|
| 213 |
+
" label=\"Analysis Results\",\n",
|
| 214 |
+
" lines=15\n",
|
| 215 |
+
" )\n",
|
| 216 |
+
" \n",
|
| 217 |
+
" btn.click(\n",
|
| 218 |
+
" research_assistant,\n",
|
| 219 |
+
" inputs=[query, context, temp],\n",
|
| 220 |
+
" outputs=output\n",
|
| 221 |
+
" )\n",
|
| 222 |
+
" \n",
|
| 223 |
+
" gr.Examples(\n",
|
| 224 |
+
" examples=[\n",
|
| 225 |
+
" [\"What are the key trends in AI research?\", \"\", 0.6],\n",
|
| 226 |
+
" [\"Analyze this article for bias\", \"https://example.com/article\", 0.4],\n",
|
| 227 |
+
" [\"Generate research questions about climate change\", \"\", 0.7]\n",
|
| 228 |
+
" ],\n",
|
| 229 |
+
" inputs=[query, context, temp]\n",
|
| 230 |
+
" )\n",
|
| 231 |
+
"\n",
|
| 232 |
+
"# Launch the interface\n",
|
| 233 |
+
"demo.launch(share=True) # share=True creates a public link"
|
| 234 |
+
],
|
| 235 |
+
"metadata": {
|
| 236 |
+
"id": "gradio"
|
| 237 |
+
},
|
| 238 |
+
"execution_count": null,
|
| 239 |
+
"outputs": []
|
| 240 |
+
},
|
| 241 |
+
{
|
| 242 |
+
"cell_type": "markdown",
|
| 243 |
+
"source": [
|
| 244 |
+
"## π Quick Test"
|
| 245 |
+
],
|
| 246 |
+
"metadata": {
|
| 247 |
+
"id": "test"
|
| 248 |
+
}
|
| 249 |
+
},
|
| 250 |
+
{
|
| 251 |
+
"cell_type": "code",
|
| 252 |
+
"source": [
|
| 253 |
+
"# Test the model directly\n",
|
| 254 |
+
"test_result = research_assistant(\n",
|
| 255 |
+
" \"What are the implications of large language models for research?\",\n",
|
| 256 |
+
" \"Large language models have billions of parameters and can process vast amounts of text.\"\n",
|
| 257 |
+
")\n",
|
| 258 |
+
"\n",
|
| 259 |
+
"print(\"Test Result:\")\n",
|
| 260 |
+
"print(test_result)"
|
| 261 |
+
],
|
| 262 |
+
"metadata": {
|
| 263 |
+
"id": "test_code"
|
| 264 |
+
},
|
| 265 |
+
"execution_count": null,
|
| 266 |
+
"outputs": []
|
| 267 |
+
}
|
| 268 |
+
]
|
| 269 |
+
}
|