File size: 13,704 Bytes
5a2b2d3
 
 
 
489f984
5a2b2d3
489f984
 
 
 
 
 
 
 
 
 
5a2b2d3
 
 
 
 
 
 
489f984
5a2b2d3
 
 
 
 
 
 
 
489f984
5a2b2d3
 
 
 
 
 
 
 
 
 
 
 
 
 
489f984
5a2b2d3
 
 
 
 
 
 
 
489f984
5a2b2d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489f984
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a2b2d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "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
}