{ "cells": [ { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n" ] } ], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import requests\n", "import os\n", "\n", "from dotenv import load_dotenv\n", "load_dotenv()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'World!'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "url = 'https://bachephysicdun-backend.hf.space'\n", "response = requests.get(url)\n", "response.json()['Hello']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from app.data_indexing import DataIndexer" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "sqlalchemy.orm.decl_api.Base" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from app.database import Base\n", "\n", "Base" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.append('./app/')\n", "from app.models import User, Message" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The token has not been saved to the git credentials helper. Pass `add_to_git_credential=True` in this function directly or `--add-to-git-credential` if using via `huggingface-cli` if you want to set the git credential as well.\n", "Token is valid (permission: fineGrained).\n", "Your token has been saved to /Users/amin/.cache/huggingface/token\n", "Login successful\n", "{'input_ids': [128000, 9906, 11, 1268, 527, 499, 30], 'attention_mask': [1, 1, 1, 1, 1, 1, 1]}\n" ] } ], "source": [ "import os\n", "from transformers import AutoTokenizer\n", "from huggingface_hub import login\n", "\n", "login(os.environ['HF_TOKEN'])\n", "\n", "# Load the tokenizer for the gated model\n", "tokenizer = AutoTokenizer.from_pretrained(\"meta-llama/Meta-Llama-3-8B-Instruct\")\n", "\n", "# Example usage\n", "text = \"Hello, how are you?\"\n", "tokens = tokenizer(text)\n", "\n", "print(tokens)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from huggingface_hub import InferenceClient\n", "client = InferenceClient(\n", " \"meta-llama/Meta-Llama-3-8B-Instruct\",\n", " token=os.environ['HF_TOKEN'],\n", ")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Linear regression is a statistical method that is used to create a linear equation that best predicts the relationship between two or more variables. The goal of linear regression is to create a model that can be used to make predictions about the value of the dependent variable (y) based on the value of one or more independent variables (x). Linear regression is a widely used and powerful tool for modeling the relationship between variables, and it has many applications in fields such as finance, economics, and medicine.\n", "\n", "How does Linear Regression work? Linear regression works by using a set of data points, where each data point represents a pair of values for the dependent and independent variables. The algorithm then finds the line that best fits the data points, by minimizing the sum of the squared errors between the predicted values and the actual values. The line that is found is called the regression line, and it is used to make predictions about the value of the dependent variable.\n", "\n", "There are several types of linear regression, including:\n", "\n", "* Simple Linear Regression: This type of linear regression involves a single independent variable and a single dependent variable.\n", "* Multiple Linear Regression: This type of linear regression involves multiple independent variables and a single dependent variable.\n", "* Polynomial Regression: This type of linear regression involves a polynomial equation, rather than a linear equation.\n", "* Non-Linear Regression: This type of linear regression involves a non-linear equation, rather than a linear equation.\n", "\n", "What are the advantages and disadvantages of Linear Regression? The advantages of linear regression include:\n", "\n", "* It is a widely used and well-established statistical method.\n", "* It is easy to interpret and understand.\n", "* It can be used to make predictions about the value of the dependent variable.\n", "* It can be used to identify the relationship between the independent and dependent variables.\n", "\n", "The disadvantages of linear regression include:\n", "\n", "* It assumes a linear relationship between the independent and dependent variables, which may not always be the case.\n", "* It can be sensitive to outliers and noisy data.\n", "* It can be difficult to interpret the results, especially for complex models.\n", "* It can be sensitive to the choice of variables and the data used.\n", "\n", "What are some common applications of Linear Regression? Linear regression has many applications in fields such as:\n", "\n", "* Finance: Linear regression can be used to predict stock prices, interest rates, and other financial variables.\n", "* Economics: Linear regression can be used to model the relationship between economic variables, such as GDP and unemployment rates.\n", "* Medicine: Linear regression can be used to model the relationship between medical variables, such as blood pressure and heart rate.\n", "* Marketing: Linear regression can" ] } ], "source": [ "from langserve import RemoteRunnable\n", "chain = RemoteRunnable(\"http://localhost:8000/simple\")\n", "stream = chain.stream(input={'question':'What is Linear Regression?'})\n", "for chunk in stream:\n", " print(chunk, end=\"\", flush=True)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "ename": "HTTPStatusError", "evalue": "Client error '404 Not Found' for url 'https://bachephysicdun-backend.hf.space/simple/stream'\nFor more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mHTTPStatusError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[14], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m chain \u001b[39m=\u001b[39m RemoteRunnable(\u001b[39m\"\u001b[39m\u001b[39mhttps://bachephysicdun-backend.hf.space/simple\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m 3\u001b[0m stream \u001b[39m=\u001b[39m chain\u001b[39m.\u001b[39mstream(\u001b[39minput\u001b[39m\u001b[39m=\u001b[39m{\u001b[39m'\u001b[39m\u001b[39mquestion\u001b[39m\u001b[39m'\u001b[39m:\u001b[39m'\u001b[39m\u001b[39mHow are you?\u001b[39m\u001b[39m'\u001b[39m})\n\u001b[0;32m----> 4\u001b[0m \u001b[39mfor\u001b[39;49;00m chunk \u001b[39min\u001b[39;49;00m stream:\n\u001b[1;32m 5\u001b[0m \u001b[39mprint\u001b[39;49m(chunk, end\u001b[39m=\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39m\"\u001b[39;49m, flush\u001b[39m=\u001b[39;49m\u001b[39mTrue\u001b[39;49;00m)\n", "File \u001b[0;32m~/Workspace/llm_learning/week6/myenv/lib/python3.11/site-packages/langserve/client.py:544\u001b[0m, in \u001b[0;36mRemoteRunnable.stream\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 540\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m 541\u001b[0m \u001b[39mwith\u001b[39;00m connect_sse(\n\u001b[1;32m 542\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39msync_client, \u001b[39m\"\u001b[39m\u001b[39mPOST\u001b[39m\u001b[39m\"\u001b[39m, endpoint, json\u001b[39m=\u001b[39mdata\n\u001b[1;32m 543\u001b[0m ) \u001b[39mas\u001b[39;00m event_source:\n\u001b[0;32m--> 544\u001b[0m \u001b[39mfor\u001b[39;49;00m sse \u001b[39min\u001b[39;49;00m event_source\u001b[39m.\u001b[39;49miter_sse():\n\u001b[1;32m 545\u001b[0m \u001b[39mif\u001b[39;49;00m sse[\u001b[39m\"\u001b[39;49m\u001b[39mevent\u001b[39;49m\u001b[39m\"\u001b[39;49m] \u001b[39m==\u001b[39;49m \u001b[39m\"\u001b[39;49m\u001b[39mdata\u001b[39;49m\u001b[39m\"\u001b[39;49m:\n\u001b[1;32m 546\u001b[0m chunk \u001b[39m=\u001b[39;49m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_lc_serializer\u001b[39m.\u001b[39;49mloads(sse[\u001b[39m\"\u001b[39;49m\u001b[39mdata\u001b[39;49m\u001b[39m\"\u001b[39;49m])\n", "File \u001b[0;32m~/Workspace/llm_learning/week6/myenv/lib/python3.11/site-packages/langserve/server_sent_events.py:95\u001b[0m, in \u001b[0;36mEventSource.iter_sse\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 94\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39miter_sse\u001b[39m(\u001b[39mself\u001b[39m) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Iterator[ServerSentEvent]:\n\u001b[0;32m---> 95\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_check_content_type()\n\u001b[1;32m 96\u001b[0m decoder \u001b[39m=\u001b[39m SSEDecoder()\n\u001b[1;32m 97\u001b[0m \u001b[39mfor\u001b[39;00m line \u001b[39min\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_response\u001b[39m.\u001b[39miter_lines():\n", "File \u001b[0;32m~/Workspace/llm_learning/week6/myenv/lib/python3.11/site-packages/langserve/server_sent_events.py:82\u001b[0m, in \u001b[0;36mEventSource._check_content_type\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 80\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_check_content_type\u001b[39m(\u001b[39mself\u001b[39m) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m 81\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"Check that the response content type is 'text/event-stream'.\"\"\"\u001b[39;00m\n\u001b[0;32m---> 82\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_response\u001b[39m.\u001b[39;49mraise_for_status()\n\u001b[1;32m 83\u001b[0m content_type \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_response\u001b[39m.\u001b[39mheaders\u001b[39m.\u001b[39mget(\u001b[39m\"\u001b[39m\u001b[39mcontent-type\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39m\"\u001b[39m)\u001b[39m.\u001b[39mpartition(\u001b[39m\"\u001b[39m\u001b[39m;\u001b[39m\u001b[39m\"\u001b[39m)[\u001b[39m0\u001b[39m]\n\u001b[1;32m 84\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39m\"\u001b[39m\u001b[39mtext/event-stream\u001b[39m\u001b[39m\"\u001b[39m \u001b[39mnot\u001b[39;00m \u001b[39min\u001b[39;00m content_type:\n", "File \u001b[0;32m~/Workspace/llm_learning/week6/myenv/lib/python3.11/site-packages/httpx/_models.py:763\u001b[0m, in \u001b[0;36mResponse.raise_for_status\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 761\u001b[0m error_type \u001b[39m=\u001b[39m error_types\u001b[39m.\u001b[39mget(status_class, \u001b[39m\"\u001b[39m\u001b[39mInvalid status code\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m 762\u001b[0m message \u001b[39m=\u001b[39m message\u001b[39m.\u001b[39mformat(\u001b[39mself\u001b[39m, error_type\u001b[39m=\u001b[39merror_type)\n\u001b[0;32m--> 763\u001b[0m \u001b[39mraise\u001b[39;00m HTTPStatusError(message, request\u001b[39m=\u001b[39mrequest, response\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m)\n", "\u001b[0;31mHTTPStatusError\u001b[0m: Client error '404 Not Found' for url 'https://bachephysicdun-backend.hf.space/simple/stream'\nFor more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404" ] } ], "source": [ "from langserve import RemoteRunnable\n", "chain = RemoteRunnable(\"https://bachephysicdun-backend.hf.space/simple\")\n", "stream = chain.stream(input={'question':'How are you?'})\n", "for chunk in stream:\n", " print(chunk, end=\"\", flush=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "myenv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }