philmui commited on
Commit
27aacbc
·
1 Parent(s): 7737888

init checkin

Browse files
Files changed (6) hide show
  1. Dockerfile +14 -0
  2. README.md +254 -6
  3. __pycache__/app.cpython-311.pyc +0 -0
  4. app.py +80 -0
  5. chainlit.md +3 -0
  6. requirements.txt +5 -0
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ WORKDIR /code
7
+
8
+ COPY ./requirements.txt /code/requirements.txt
9
+
10
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
11
+
12
+ COPY . .
13
+
14
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,11 +1,259 @@
 
1
  ---
2
- title: Llm App
3
- emoji: 🔥
4
- colorFrom: purple
5
- colorTo: blue
6
  sdk: docker
7
  pinned: false
8
- license: mit
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <<<<<<< HEAD
2
  ---
3
+ title: BeyondChatGPT Demo
4
+ emoji: 📉
5
+ colorFrom: pink
6
+ colorTo: yellow
7
  sdk: docker
8
  pinned: false
9
+ app_port: 7860
10
  ---
11
 
12
+ <p align = "center" draggable=”false” ><img src="https://github.com/AI-Maker-Space/LLM-Dev-101/assets/37101144/d1343317-fa2f-41e1-8af1-1dbb18399719"
13
+ width="200px"
14
+ height="auto"/>
15
+ </p>
16
+
17
+
18
+ ## <h1 align="center" id="heading">:wave: Welcome to Beyond ChatGPT!!</h1>
19
+
20
+ For a step-by-step YouTube video walkthrough, watch this! [Deploying Chainlit app on Hugging Face](https://www.youtube.com/live/pRbbZcL0NMI?si=NAYhMZ_suAY84f06&t=2119)
21
+
22
+ ![Beyond ChatGPT: Build Your First LLM Application](https://github.com/AI-Maker-Space/Beyond-ChatGPT/assets/48775140/cb7a74b8-28af-4d12-a008-8f5a51d47b4c)
23
+
24
+ ## 🤖 Your First LLM App
25
+
26
+ > If you need an introduction to `git`, or information on how to set up API keys for the tools we'll be using in this repository - check out our [Interactive Dev Environment for LLM Development](https://github.com/AI-Maker-Space/Interactive-Dev-Environment-for-LLM-Development/tree/main) which has everything you'd need to get started in this repository!
27
+
28
+ In this repository, we'll walk you through the steps to create a Large Language Model (LLM) application using Chainlit, then containerize it using Docker, and finally deploy it on Huggingface Spaces.
29
+
30
+ Are you ready? Let's get started!
31
+
32
+ <details>
33
+ <summary>🖥️ Accessing "gpt-3.5-turbo" (ChatGPT) like a developer</summary>
34
+
35
+ 1. Head to [this notebook](https://colab.research.google.com/drive/1mOzbgf4a2SP5qQj33ZxTz2a01-5eXqk2?usp=sharing) and follow along with the instructions!
36
+
37
+ 2. Complete the notebook and try out your own system/assistant messages!
38
+
39
+ That's it! Head to the next step and start building your application!
40
+
41
+ </details>
42
+
43
+
44
+ <details>
45
+ <summary>🏗️ Building Your First LLM App</summary>
46
+
47
+ 1. Clone [this](https://github.com/AI-Maker-Space/Beyond-ChatGPT/tree/main) repo.
48
+
49
+ ``` bash
50
+ git clone https://github.com/AI-Maker-Space/Beyond-ChatGPT.git
51
+ ```
52
+
53
+ 2. Navigate inside this repo
54
+ ``` bash
55
+ cd Beyond-ChatGPT
56
+ ```
57
+
58
+ 3. Install the packages required for this python envirnoment in `requirements.txt`.
59
+ ``` bash
60
+ pip install -r requirements.txt
61
+ ```
62
+
63
+ 4. Open your `.env` file. Replace the `###` in your `.env` file with your OpenAI Key and save the file.
64
+ ``` bash
65
+ OPENAI_API_KEY=sk-###
66
+ ```
67
+
68
+ 5. Let's try deploying it locally. Make sure you're in the python environment where you installed Chainlit and OpenAI. Run the app using Chainlit. This may take a minute to run.
69
+ ``` bash
70
+ chainlit run app.py -w
71
+ ```
72
+
73
+ <p align = "center" draggable=”false”>
74
+ <img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/54bcccf9-12e2-4cef-ab53-585c1e2b0fb5">
75
+ </p>
76
+
77
+ Great work! Let's see if we can interact with our chatbot.
78
+
79
+ <p align = "center" draggable=”false”>
80
+ <img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/854e4435-1dee-438a-9146-7174b39f7c61">
81
+ </p>
82
+
83
+ Awesome! Time to throw it into a docker container and prepare it for shipping!
84
+ </details>
85
+
86
+
87
+
88
+ <details>
89
+ <summary>🐳 Containerizing our App</summary>
90
+
91
+ 1. Let's build the Docker image. We'll tag our image as `llm-app` using the `-t` parameter. The `.` at the end means we want all of the files in our current directory to be added to our image.
92
+
93
+ ``` bash
94
+ docker build -t llm-app .
95
+ ```
96
+
97
+ 2. Run and test the Docker image locally using the `run` command. The `-p`parameter connects our **host port #** to the left of the `:` to our **container port #** on the right.
98
+
99
+ ``` bash
100
+ docker run -p 7860:7860 llm-app
101
+ ```
102
+
103
+ 3. Visit http://localhost:7860 in your browser to see if the app runs correctly.
104
+
105
+ <p align = "center" draggable=”false”>
106
+ <img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/2c764f25-09a0-431b-8d28-32246e0ca1b7">
107
+ </p>
108
+
109
+ Great! Time to ship!
110
+ </details>
111
+
112
+
113
+ <details>
114
+ <summary>🚀 Deploying Your First LLM App</summary>
115
+
116
+ 1. Let's create a new Huggingface Space. Navigate to [Huggingface](https://huggingface.co) and click on your profile picture on the top right. Then click on `New Space`.
117
+
118
+ <p align = "center" draggable=”false”>
119
+ <img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/f0656408-28b8-4876-9887-8f0c4b882bae">
120
+ </p>
121
+
122
+ 2. Setup your space as shown below:
123
+
124
+ - Owner: Your username
125
+ - Space Name: `llm-app`
126
+ - License: `Openrail`
127
+ - Select the Space SDK: `Docker`
128
+ - Docker Template: `Blank`
129
+ - Space Hardware: `CPU basic - 2 vCPU - 16 GB - Free`
130
+ - Repo type: `Public`
131
+
132
+ <p align = "center" draggable=”false”>
133
+ <img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/8f16afd1-6b46-4d9f-b642-8fefe355c5c9">
134
+ </p>
135
+
136
+ 3. You should see something like this. We're now ready to send our files to our Huggingface Space. After cloning, move your files to this repo and push it along with your docker file. You DO NOT need to create a Dockerfile. Make sure NOT TO push your `.env` file. This should automatically be ignored.
137
+
138
+ <p align = "center" draggable=”false”>
139
+ <img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/cbf366e2-7613-4223-932a-72c67a73f9c6">
140
+ </p>
141
+
142
+ 4. After pushing all files, navigate to the settings in the top right to add your OpenAI API key.
143
+
144
+ <p align = "center" draggable=”false”>
145
+ <img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/a1123a6f-abdd-4f76-bea4-39acf9928762">
146
+ </p>
147
+
148
+ 5. Scroll down to `Variables and secrets` and click on `New secret` on the top right.
149
+
150
+ <p align = "center" draggable=”false”>
151
+ <img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/a8a4a25d-752b-4036-b572-93381370c2db">
152
+ </p>
153
+
154
+ 6. Set the name to `OPENAI_API_KEY` and add your OpenAI key under `Value`. Click save.
155
+
156
+ <p align = "center" draggable=”false”>
157
+ <img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/0a897538-1779-48ff-bcb4-486af30f7a14">
158
+ </p>
159
+
160
+ 7. To ensure your key is being used, we recommend you `Restart this Space`.
161
+
162
+ <p align = "center" draggable=”false”>
163
+ <img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/fb1d83af-6ebe-4676-8bf5-b6d88f07c583">
164
+ </p>
165
+
166
+ 8. Congratulations! You just deployed your first LLM! 🚀🚀🚀 Get on linkedin and post your results and experience! Make sure to tag us at #AIMakerspace !
167
+
168
+ Here's a template to get your post started!
169
+
170
+ ```
171
+ 🚀🎉 Exciting News! 🎉🚀
172
+
173
+ 🏗️ Today, I'm thrilled to announce that I've successfully built and shipped my first-ever LLM using the powerful combination of Chainlit, Docker, and the OpenAI API! 🖥️
174
+
175
+ Check it out 👇
176
+ [LINK TO APP]
177
+
178
+ A big shoutout to the @**AI Makerspace** for all making this possible. Couldn't have done it without the incredible community there. 🤗🙏
179
+
180
+ Looking forward to building with the community! 🙌✨ Here's to many more creations ahead! 🥂🎉
181
+
182
+ Who else is diving into the world of AI? Let's connect! 🌐💡
183
+
184
+ #FirstLLM #Chainlit #Docker #OpenAI #AIMakerspace
185
+ ```
186
+
187
+ </details>
188
+
189
+ <p></p>
190
+
191
+ ### That's it for now! And so it begins.... :)
192
+ =======
193
+ # Beyond-ChatGPT
194
+ Chainlit App using Python streaming for Level 0 MLOps
195
+
196
+ LLM Application with Chainlit, Docker, and Huggingface Spaces
197
+ In this guide, we'll walk you through the steps to create a Language Learning Model (LLM) application using Chainlit, then containerize it using Docker, and finally deploy it on Huggingface Spaces.
198
+
199
+ Prerequisites
200
+ A GitHub account
201
+ Docker installed on your local machine
202
+ A Huggingface Spaces account
203
+
204
+ ### Building our App
205
+ Clone this repo
206
+
207
+ Navigate inside this repo
208
+
209
+ ### Install requirements using `pip install -r requirements.txt`?????????
210
+
211
+ Add your OpenAI Key to `.env` file and save the file.
212
+
213
+ Let's try deploying it locally. Make sure you're in the python environment where you installed Chainlit and OpenAI.
214
+
215
+ Run the app using Chainlit
216
+
217
+ ```
218
+ chainlit run app.py -w
219
+ ```
220
+
221
+ Great work! Let's see if we can interact with our chatbot.
222
+
223
+ It works! Let's ship it!
224
+
225
+
226
+ ### Deploy to Huggingface Spaces
227
+
228
+ Login to Huggingface Spaces CLI
229
+
230
+ ``` bash
231
+ huggingface-cli login
232
+ ```
233
+
234
+ Follow the prompts to authenticate.
235
+
236
+
237
+
238
+ Push Docker Image to Huggingface Container Registry
239
+
240
+ ```
241
+ docker tag llm-app:latest huggingface/your-username/llm-app:latest
242
+ docker push huggingface/your-username/llm-app:latest
243
+ ```
244
+
245
+ Deploy to Huggingface Spaces
246
+
247
+
248
+ Deploying on Huggingface Spaces using a custom Docker image involves using their web interface. Go to Huggingface Spaces and create a new space, then set it up to use your Docker image from the Huggingface Container Registry.
249
+
250
+ Access the Application
251
+
252
+ Once deployed, access your app at:
253
+
254
+ ruby
255
+ Copy code
256
+ https://huggingface.co/spaces/your-username/llm-app
257
+ Conclusion
258
+ You've successfully created an LLM application with Chainlit, containerized it with Docker, and deployed it on Huggingface Spaces. Visit the link to interact with your deployed application.
259
+ >>>>>>> 8377e05 (Update README.md)
__pycache__/app.cpython-311.pyc ADDED
Binary file (3.46 kB). View file
 
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python)
2
+
3
+ # OpenAI Chat completion
4
+ import os
5
+ from openai import AsyncOpenAI # importing openai for API usage
6
+ import chainlit as cl # importing chainlit for our app
7
+ from chainlit.prompt import Prompt, PromptMessage # importing prompt tools
8
+ from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools
9
+ from dotenv import load_dotenv, find_dotenv
10
+
11
+ load_dotenv(find_dotenv())
12
+
13
+ # ChatOpenAI Templates
14
+ system_template = """You are a helpful assistant who always speaks in a pleasant tone!
15
+ """
16
+
17
+ user_template = """{input}
18
+ Think through your response step by step.
19
+ """
20
+
21
+
22
+ @cl.on_chat_start # marks a function that will be executed at the start of a user session
23
+ async def start_chat():
24
+ settings = {
25
+ "model": "gpt-3.5-turbo",
26
+ "temperature": 0,
27
+ "max_tokens": 500,
28
+ "top_p": 1,
29
+ "frequency_penalty": 0,
30
+ "presence_penalty": 0,
31
+ }
32
+
33
+ cl.user_session.set("settings", settings)
34
+
35
+
36
+ @cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
37
+ async def main(message: cl.Message):
38
+ settings = cl.user_session.get("settings")
39
+
40
+ client = AsyncOpenAI()
41
+
42
+ print(message.content)
43
+
44
+ prompt = Prompt(
45
+ provider=ChatOpenAI.id,
46
+ messages=[
47
+ PromptMessage(
48
+ role="system",
49
+ template=system_template,
50
+ formatted=system_template,
51
+ ),
52
+ PromptMessage(
53
+ role="user",
54
+ template=user_template,
55
+ formatted=user_template.format(input=message.content),
56
+ ),
57
+ ],
58
+ inputs={"input": message.content},
59
+ settings=settings,
60
+ )
61
+
62
+ print([m.to_openai() for m in prompt.messages])
63
+
64
+ msg = cl.Message(content="")
65
+
66
+ # Call OpenAI
67
+ async for stream_resp in await client.chat.completions.create(
68
+ messages=[m.to_openai() for m in prompt.messages], stream=True, **settings
69
+ ):
70
+ token = stream_resp.choices[0].delta.content
71
+ if not token:
72
+ token = ""
73
+ await msg.stream_token(token)
74
+
75
+ # Update the prompt object with the completion
76
+ prompt.completion = msg.content
77
+ msg.prompt = prompt
78
+
79
+ # Send and close the message stream
80
+ await msg.send()
chainlit.md ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # Beyond ChatGPT
2
+
3
+ This Chainlit app was created following instructions from [this repository!](https://github.com/AI-Maker-Space/Beyond-ChatGPT)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ chainlit==0.7.700
2
+ cohere==4.37
3
+ openai==1.3.5
4
+ tiktoken==0.5.1
5
+ python-dotenv==1.0.0