daniel Foley commited on
Commit
bfd18d6
·
1 Parent(s): 848af7c

Initial sample chainlit

Browse files
Files changed (3) hide show
  1. Dockerfile +111 -0
  2. app.py +265 -0
  3. requirements.txt +9 -0
Dockerfile ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+ # Create a non-root user
10
+
11
+
12
+
13
+ RUN useradd -m -u 1000 user
14
+
15
+
16
+
17
+ USER user
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+ # Set PATH to include user's local bin
26
+
27
+
28
+
29
+ ENV PATH="/home/user/.local/bin:$PATH"
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+ # Set working directory
38
+
39
+
40
+
41
+ WORKDIR /app
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+ # Copy requirements file with appropriate ownership
50
+
51
+
52
+
53
+ COPY --chown=user ./requirements.txt requirements.txt
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+ # Install dependencies
62
+
63
+
64
+
65
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+ # Copy application files with appropriate ownership
74
+
75
+
76
+
77
+ COPY --chown=user . /app
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+ # Set environment variables for Chainlit
86
+
87
+
88
+
89
+ ENV HOST=0.0.0.0
90
+
91
+
92
+
93
+ ENV PORT=7860
94
+
95
+
96
+
97
+ ENV CHAINLIT_SERVER_PORT=7860
98
+
99
+
100
+
101
+ ENV CHAINLIT_HOST="0.0.0.0"
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+ # Change the CMD to use chainlit
110
+
111
+ CMD ["chainlit", "run", "app.py"]
app.py ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import chainlit as cl
2
+
3
+
4
+
5
+ from typing import Optional
6
+
7
+
8
+
9
+ import time
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+ # Store conversation history
18
+
19
+
20
+
21
+ conversation_memory = []
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+ @cl.on_chat_start
30
+
31
+
32
+
33
+ async def start():
34
+
35
+
36
+
37
+ """Initializes the chat session"""
38
+
39
+
40
+
41
+ # Send an initial message
42
+
43
+
44
+
45
+ await cl.Message(
46
+
47
+
48
+
49
+ content="👋 Hello! I'm your AI assistant. How can I help you today?",
50
+
51
+
52
+
53
+ author="Assistant"
54
+
55
+
56
+
57
+ ).send()
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+ # Set some session variables
66
+
67
+
68
+
69
+ cl.user_session.set("conversation_started", True)
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+ @cl.on_message
78
+
79
+
80
+
81
+ async def main(message: cl.Message):
82
+
83
+
84
+
85
+ """Main message handler"""
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+ # Simulate some processing time
94
+
95
+
96
+
97
+ with cl.Step("Processing...") as step:
98
+
99
+
100
+
101
+ time.sleep(1) # Simulated delay
102
+
103
+
104
+
105
+ step.output = "Processed message"
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+ # Store message in conversation history
114
+
115
+
116
+
117
+ conversation_memory.append({
118
+
119
+
120
+
121
+ "role": "user",
122
+
123
+
124
+
125
+ "content": message.content
126
+
127
+
128
+
129
+ })
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+ # Create a response
138
+
139
+
140
+
141
+ response = f"I received your message: '{message.content}'. This is a demo response."
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+ # Store response in conversation history
150
+
151
+
152
+
153
+ conversation_memory.append({
154
+
155
+
156
+
157
+ "role": "assistant",
158
+
159
+
160
+
161
+ "content": response
162
+
163
+
164
+
165
+ })
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+ # Send response with typing effect
174
+
175
+
176
+
177
+ await cl.Message(
178
+
179
+
180
+
181
+ content=response,
182
+
183
+
184
+
185
+ author="Assistant"
186
+
187
+
188
+
189
+ ).send()
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+ @cl.password_auth_callback
198
+
199
+
200
+
201
+ def auth_callback(username: str, password: str) -> Optional[cl.User]:
202
+
203
+
204
+
205
+ """Basic authentication handler"""
206
+
207
+
208
+
209
+ # This is a simple example - in production, use proper authentication
210
+
211
+
212
+
213
+ if username == "demo" and password == "password":
214
+
215
+
216
+
217
+ return cl.User(identifier="demo", metadata={"role": "user"})
218
+
219
+
220
+
221
+ return None
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+ @cl.on_chat_end
230
+
231
+
232
+
233
+ async def end():
234
+
235
+
236
+
237
+ """Cleanup when chat ends"""
238
+
239
+
240
+
241
+ await cl.Message(content="👋 Thank you for chatting! Goodbye!").send()
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+ # Custom action handler example
250
+
251
+
252
+
253
+ @cl.action_callback("feedback")
254
+
255
+
256
+
257
+ async def on_action(action):
258
+
259
+
260
+
261
+ """Handles custom feedback action"""
262
+
263
+
264
+
265
+ await cl.Message(content=f"Received feedback: {action.value}").send()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+
3
+
4
+
5
+ uvicorn[standard]
6
+
7
+
8
+
9
+ chainlit