shreyansh12183 commited on
Commit
0ae1a46
·
verified ·
1 Parent(s): 6ca7c2b

fix(gradio): use rock-solid ChatInterface to eliminate UI validation errors

Browse files
Files changed (1) hide show
  1. colab_demo_olmo2_astro_logic.ipynb +9 -8
colab_demo_olmo2_astro_logic.ipynb CHANGED
@@ -8,17 +8,18 @@
8
  "### Interactive Colab GPU Demo with Gradio (First-Gen Domain Foundation)\n",
9
  "\n",
10
  "**Architect & Creator:** Shreyansh Singh \u00b7 [ExperimentLab](https://github.com/shreyansh001boy-tech) \u00b7 [Hugging Face Repository](https://huggingface.co/shreyansh12183/olmo2-7b-astro-logic) \n",
 
11
  "**Certified Benchmark:** `Certified Astrophysical Physics & Formal Proofs | General Relativity & Symbolic Logic` \n",
12
- "**Base Foundation:** `allenai/OLMo-2-1124-7B-Instruct` | **Adapter:** `shreyansh12183/olmo2-7b-astro-logic` \n",
13
  "**License:** CC BY-NC 4.0 (Non-Commercial Sovereign License)\n",
14
  "\n",
15
  "---\n",
16
  "\n",
17
  "### \ud83d\ude80 Running This Demo on Google Colab:\n",
18
- "1. Ensure your runtime is set to **T4 GPU** (`Runtime -> Change runtime type -> T4 GPU`).\n",
19
- "2. Run **Cell 1** to install dependencies (`transformers`, `bitsandbytes`, `peft`, `gradio`).\n",
20
- "3. Run **Cell 2** to load the 7B foundation weights in 4-bit NF4 into Colab T4 GPU VRAM (~5.5 GB).\n",
21
- "4. Run **Cell 3** to launch the interactive Gradio web application with a public `https://*.gradio.live` link!\n",
22
  "\n"
23
  ]
24
  },
@@ -28,7 +29,7 @@
28
  "execution_count": null,
29
  "outputs": [],
30
  "source": [
31
- "# Cell 1: Install Required Libraries\n!pip install -q torch transformers bitsandbytes peft accelerate gradio\nprint('\u2713 Dependencies installed successfully!')\n"
32
  ]
33
  },
34
  {
@@ -37,7 +38,7 @@
37
  "execution_count": null,
38
  "outputs": [],
39
  "source": [
40
- "# Cell 2: Load 7B Base Foundation & LoRA Adapter in 4-bit NF4\nimport torch\nfrom transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextIteratorStreamer\nfrom peft import PeftModel\nimport threading\n\nBASE_ID = \"allenai/OLMo-2-1124-7B-Instruct\"\nADAPTER_ID = \"shreyansh12183/olmo2-7b-astro-logic\"\n\nprint(f\"Loading Base Foundation: {BASE_ID} in 4-bit NF4...\")\nbnb_config = BitsAndBytesConfig(\n load_in_4bit=True,\n bnb_4bit_quant_type=\"nf4\",\n bnb_4bit_compute_dtype=torch.float16,\n bnb_4bit_use_double_quant=True\n)\n\ntokenizer = AutoTokenizer.from_pretrained(BASE_ID, trust_remote_code=True)\nif tokenizer.pad_token is None:\n tokenizer.pad_token = tokenizer.eos_token\n\nbase_model = AutoModelForCausalLM.from_pretrained(\n BASE_ID,\n quantization_config=bnb_config,\n device_map=\"auto\",\n torch_dtype=torch.float16,\n trust_remote_code=True\n)\n\nprint(f\"Attaching Fine-Tuned LoRA Adapter: {ADAPTER_ID}...\")\nmodel = PeftModel.from_pretrained(base_model, ADAPTER_ID)\nmodel.eval()\nprint(f\"\u2713 {ADAPTER_ID} successfully loaded in Colab GPU VRAM (~5.5 GB)!\")\n"
41
  ]
42
  },
43
  {
@@ -46,7 +47,7 @@
46
  "execution_count": null,
47
  "outputs": [],
48
  "source": [
49
- "# Cell 3: Launch Live Gradio Web Interface\nimport gradio as gr\n\nSYSTEM_PROMPT = \"You are OLMo-2 7B Astro Logic, an astrophysics and formal symbolic reasoning foundation model architected and fine-tuned by Shreyansh Singh (ExperimentLab). Solve complex celestial mechanics, general relativistic field equations, stellar nucleosynthesis problems, and rigorous formal symbolic logic derivations.\"\nEXAMPLES = [\"Derive the Schwarzschild metric geodesic equations and calculate the perihelion precession of Mercury.\", \"Explain the proton-proton chain vs CNO cycle in stellar nucleosynthesis and derive the Gamow peak energy window.\", \"Formulate the cosmological Friedmann equations from the FLRW metric and solve for a matter-dominated flat universe.\", \"Provide a formal symbolic natural deduction proof for the law of excluded middle in propositional logic.\"]\n\ndef generate_response(message, history):\n messages = [{\"role\": \"system\", \"content\": SYSTEM_PROMPT}]\n for user_msg, bot_msg in history:\n messages.append({\"role\": \"user\", \"content\": user_msg})\n if bot_msg:\n messages.append({\"role\": \"assistant\", \"content\": bot_msg})\n messages.append({\"role\": \"user\", \"content\": message})\n \n prompt_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n inputs = tokenizer(prompt_text, return_tensors=\"pt\").to(model.device)\n \n streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)\n gen_kwargs = dict(\n input_ids=inputs.input_ids,\n attention_mask=inputs.attention_mask,\n streamer=streamer,\n max_new_tokens=1024,\n temperature=0.3,\n top_p=0.9,\n repetition_penalty=1.1,\n do_sample=True\n )\n \n thread = threading.Thread(target=model.generate, kwargs=gen_kwargs)\n thread.start()\n \n partial_text = \"\"\n for new_text in streamer:\n partial_text += new_text\n yield partial_text\n\ncustom_css = \"\"\"\n#header { text-align: center; margin-bottom: 15px; }\n#badge { background: #1e293b; color: #38bdf8; padding: 6px 14px; border-radius: 9999px; font-weight: 600; display: inline-block; font-size: 13px; }\n\"\"\"\n\nwith gr.Blocks(title=\"\ud83d\udd2d OLMo-2 7B Astro Logic Specialist\", css=custom_css, theme=gr.themes.Soft()) as demo:\n with gr.Column(elem_id=\"header\"):\n gr.Markdown(f\"# \ud83d\udd2d OLMo-2 7B Astro Logic Specialist\")\n gr.Markdown(f\"### Interactive Colab GPU Demo with Gradio (First-Gen Domain Foundation)\")\n gr.Markdown(f\"<span id='badge'>Certified: Certified Astrophysical Physics & Formal Proofs | General Relativity & Symbolic Logic</span>\")\n gr.Markdown(f\"**Base Foundation:** `allenai/OLMo-2-1124-7B-Instruct` \u00b7 **Adapter:** [`shreyansh12183/olmo2-7b-astro-logic`](https://huggingface.co/shreyansh12183/olmo2-7b-astro-logic) \u00b7 **Architect:** Shreyansh Singh\")\n \n chatbot = gr.Chatbot(height=520, show_label=False, show_copy_button=True)\n with gr.Row():\n msg_input = gr.Textbox(placeholder=\"Enter your prompt here or click any of the curated questions below...\", scale=8, show_label=False)\n send_btn = gr.Button(\"Submit\", variant=\"primary\", scale=2)\n clear_btn = gr.Button(\"Clear Chat\", scale=1)\n \n gr.Markdown(\"### \ud83c\udfaf Curated Evaluation & Demo Questions (Click to Test):\")\n gr.Examples(examples=EXAMPLES, inputs=msg_input)\n \n def user(user_message, history):\n return \"\", history + [[user_message, None]]\n\n def bot(history):\n user_message = history[-1][0]\n history_prior = history[:-1]\n history[-1][1] = \"\"\n for chunk in generate_response(user_message, history_prior):\n history[-1][1] = chunk\n yield history\n\n msg_input.submit(user, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(\n bot, chatbot, chatbot\n )\n send_btn.click(user, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(\n bot, chatbot, chatbot\n )\n clear_btn.click(lambda: None, None, chatbot, queue=False)\n\nprint(\"\\n\" + \"=\"*75)\nprint(\"\ud83d\ude80 Launching Gradio Web App with Public Share Link...\")\nprint(\"=\"*75 + \"\\n\")\ndemo.queue()\ndemo.launch(share=True, debug=True)\n"
50
  ]
51
  }
52
  ],
 
8
  "### Interactive Colab GPU Demo with Gradio (First-Gen Domain Foundation)\n",
9
  "\n",
10
  "**Architect & Creator:** Shreyansh Singh \u00b7 [ExperimentLab](https://github.com/shreyansh001boy-tech) \u00b7 [Hugging Face Repository](https://huggingface.co/shreyansh12183/olmo2-7b-astro-logic) \n",
11
+ "**Architecture Lineage:** `Generation 1 \u00b7 Domain Specialist` \n",
12
  "**Certified Benchmark:** `Certified Astrophysical Physics & Formal Proofs | General Relativity & Symbolic Logic` \n",
13
+ "**Model Identifier:** [`shreyansh12183/olmo2-7b-astro-logic`](https://huggingface.co/shreyansh12183/olmo2-7b-astro-logic) \n",
14
  "**License:** CC BY-NC 4.0 (Non-Commercial Sovereign License)\n",
15
  "\n",
16
  "---\n",
17
  "\n",
18
  "### \ud83d\ude80 Running This Demo on Google Colab:\n",
19
+ "1. Ensure runtime is set to **T4 GPU** (`Runtime -> Change runtime type -> T4 GPU`).\n",
20
+ "2. Run **Cell 1** to install required libraries (`transformers`, `bitsandbytes`, `peft`, `gradio`).\n",
21
+ "3. Run **Cell 2** to load model weights in 4-bit NF4 into Colab GPU VRAM.\n",
22
+ "4. Run **Cell 3** to launch the interactive Gradio web app and receive your public `https://*.gradio.live` link!\n",
23
  "\n"
24
  ]
25
  },
 
29
  "execution_count": null,
30
  "outputs": [],
31
  "source": [
32
+ "# Cell 1: Install Required Libraries\n!pip install -q \"gradio>=4.44.1\" torch transformers bitsandbytes peft accelerate\nprint('\u2713 Dependencies installed successfully!')\n"
33
  ]
34
  },
35
  {
 
38
  "execution_count": null,
39
  "outputs": [],
40
  "source": [
41
+ "# Cell 2: Load Base Model & LoRA Adapter in 4-bit NF4\nimport torch\nfrom transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextIteratorStreamer\nfrom peft import PeftModel\nimport threading\n\nBASE_ID = \"allenai/OLMo-2-1124-7B-Instruct\"\nADAPTER_ID = \"shreyansh12183/olmo2-7b-astro-logic\"\n\nprint(f\"Loading Base Foundation: {BASE_ID} in 4-bit NF4...\")\nbnb_config = BitsAndBytesConfig(\n load_in_4bit=True,\n bnb_4bit_quant_type=\"nf4\",\n bnb_4bit_compute_dtype=torch.float16,\n bnb_4bit_use_double_quant=True\n)\n\ntokenizer = AutoTokenizer.from_pretrained(BASE_ID, trust_remote_code=True)\nif tokenizer.pad_token is None:\n tokenizer.pad_token = tokenizer.eos_token\n\nbase_model = AutoModelForCausalLM.from_pretrained(\n BASE_ID,\n quantization_config=bnb_config,\n device_map=\"auto\",\n torch_dtype=torch.float16,\n trust_remote_code=True\n)\n\nprint(f\"Attaching Fine-Tuned LoRA Adapter: {ADAPTER_ID}...\")\nmodel = PeftModel.from_pretrained(base_model, ADAPTER_ID)\nmodel.eval()\nprint(f\"\u2713 {ADAPTER_ID} successfully loaded in Colab GPU VRAM!\")\n"
42
  ]
43
  },
44
  {
 
47
  "execution_count": null,
48
  "outputs": [],
49
  "source": [
50
+ "# Cell 3: Launch Live Gradio Web Interface (Rock-Solid ChatInterface)\nimport gradio as gr\nimport threading\nfrom transformers import TextIteratorStreamer\n\nSYSTEM_PROMPT = \"You are OLMo-2 7B Astro Logic, an astrophysics and formal symbolic reasoning foundation model architected and fine-tuned by Shreyansh Singh (ExperimentLab). Solve complex celestial mechanics, general relativistic field equations, stellar nucleosynthesis problems, and rigorous formal symbolic logic derivations.\"\nEXAMPLES = [\"Derive the Schwarzschild metric geodesic equations and calculate the perihelion precession of Mercury.\", \"Explain the proton-proton chain vs CNO cycle in stellar nucleosynthesis and derive the Gamow peak energy window.\", \"Formulate the cosmological Friedmann equations from the FLRW metric and solve for a matter-dominated flat universe.\", \"Provide a formal symbolic natural deduction proof for the law of excluded middle in propositional logic.\"]\nTITLE = \"\\ud83d\\udd2d OLMo-2 7B Astro Logic Specialist\"\nDESCRIPTION = \"**Generation:** `Generation 1 \\u00b7 Domain Specialist` \\u00b7 **Model:** [`shreyansh12183/olmo2-7b-astro-logic`](https://huggingface.co/shreyansh12183/olmo2-7b-astro-logic) \\u00b7 **Certified:** `Certified Astrophysical Physics & Formal Proofs | General Relativity & Symbolic Logic`\"\n\ndef predict(message, history):\n messages = [{\"role\": \"system\", \"content\": SYSTEM_PROMPT}]\n for item in history:\n if isinstance(item, dict):\n role = item.get(\"role\", \"user\")\n content = item.get(\"content\", \"\")\n if content:\n messages.append({\"role\": role, \"content\": content})\n elif isinstance(item, (list, tuple)) and len(item) == 2:\n u_msg, a_msg = item[0], item[1]\n if u_msg:\n messages.append({\"role\": \"user\", \"content\": str(u_msg)})\n if a_msg:\n messages.append({\"role\": \"assistant\", \"content\": str(a_msg)})\n messages.append({\"role\": \"user\", \"content\": message})\n \n prompt_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n inputs = tokenizer(prompt_text, return_tensors=\"pt\").to(model.device)\n \n streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)\n gen_kwargs = dict(\n input_ids=inputs.input_ids,\n attention_mask=inputs.attention_mask,\n streamer=streamer,\n max_new_tokens=1024,\n temperature=0.3,\n top_p=0.9,\n repetition_penalty=1.1,\n do_sample=True\n )\n thread = threading.Thread(target=model.generate, kwargs=gen_kwargs)\n thread.start()\n \n partial_text = \"\"\n for new_text in streamer:\n partial_text += new_text\n yield partial_text\n\ndemo = gr.ChatInterface(\n fn=predict,\n title=TITLE,\n description=DESCRIPTION,\n examples=EXAMPLES,\n)\n\nprint(\"\\n\" + \"=\"*75)\nprint(\"\ud83d\ude80 Launching Gradio Web App with Public Share Link...\")\nprint(\"=\"*75 + \"\\n\")\ndemo.launch(share=True)\n"
51
  ]
52
  }
53
  ],