{ "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": [] } ] }