Spaces:
Running
Running
Commit
·
2c8df0b
1
Parent(s):
6772880
Add app
Browse files- Dockerfile +28 -0
- LICENSE +21 -0
- README.md +1 -0
- app.py +68 -0
- requirements.txt +1 -0
Dockerfile
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.11
|
2 |
+
|
3 |
+
# Set up a new user named "user" with user ID 1000
|
4 |
+
RUN useradd -m -u 1000 user
|
5 |
+
|
6 |
+
# Switch to the "user" user
|
7 |
+
USER user
|
8 |
+
|
9 |
+
# Set home to the user's home directory
|
10 |
+
ENV HOME=/home/user \
|
11 |
+
PATH=/home/user/.local/bin:$PATH
|
12 |
+
|
13 |
+
# Set the working directory to the user's home directory
|
14 |
+
WORKDIR $HOME/app
|
15 |
+
|
16 |
+
# Try and run pip command after setting the user with `USER user` to avoid permission issues with Python
|
17 |
+
RUN pip install --no-cache-dir --upgrade pip
|
18 |
+
|
19 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
20 |
+
COPY --chown=user . $HOME/app
|
21 |
+
|
22 |
+
COPY --chown=user requirements.txt .
|
23 |
+
|
24 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
25 |
+
|
26 |
+
COPY --chown=user app.py app.py
|
27 |
+
|
28 |
+
ENTRYPOINT ["solara", "run", "app.py", "--host=0.0.0.0", "--port", "7860"]
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 Alonso Silva Allende
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -6,6 +6,7 @@ colorTo: indigo
|
|
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
|
|
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
license: mit
|
9 |
+
app_port: 7860
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import solara
|
2 |
+
import time
|
3 |
+
import random
|
4 |
+
from typing import List
|
5 |
+
from typing_extensions import TypedDict
|
6 |
+
|
7 |
+
# Streamed response emulator
|
8 |
+
def response_generator():
|
9 |
+
response = random.choice(
|
10 |
+
[
|
11 |
+
"Hello! How can I assist you today?",
|
12 |
+
"Hey there! If you have any questions or need help with something, feel free to ask.",
|
13 |
+
]
|
14 |
+
)
|
15 |
+
for word in response.split():
|
16 |
+
yield word + " "
|
17 |
+
time.sleep(0.05)
|
18 |
+
|
19 |
+
class MessageDict(TypedDict):
|
20 |
+
role: str
|
21 |
+
content: str
|
22 |
+
|
23 |
+
messages: solara.Reactive[List[MessageDict]] = solara.reactive([])
|
24 |
+
|
25 |
+
def add_chunk_to_ai_message(chunk: str):
|
26 |
+
messages.value = [
|
27 |
+
*messages.value[:-1],
|
28 |
+
{
|
29 |
+
"role": "assistant",
|
30 |
+
"content": messages.value[-1]["content"] + chunk,
|
31 |
+
},
|
32 |
+
]
|
33 |
+
|
34 |
+
@solara.component
|
35 |
+
def Page():
|
36 |
+
with solara.Column(style={"padding": "30px"}):
|
37 |
+
solara.Title("StreamBot")
|
38 |
+
solara.Markdown("#StreamBot")
|
39 |
+
user_message_count = len([m for m in messages.value if m["role"] == "user"])
|
40 |
+
|
41 |
+
def send(message):
|
42 |
+
messages.value = [
|
43 |
+
*messages.value,
|
44 |
+
{"role": "user", "content": message},
|
45 |
+
]
|
46 |
+
|
47 |
+
def response(message):
|
48 |
+
messages.value = [*messages.value, {"role": "assistant", "content": ""}]
|
49 |
+
for chunk in response_generator():
|
50 |
+
add_chunk_to_ai_message(chunk)
|
51 |
+
|
52 |
+
|
53 |
+
def result():
|
54 |
+
if messages.value !=[]: response(messages.value[-1]["content"])
|
55 |
+
|
56 |
+
result = solara.lab.use_task(result, dependencies=[user_message_count]) # type: ignore
|
57 |
+
|
58 |
+
with solara.Column(style={"width": "70%"}):
|
59 |
+
with solara.lab.ChatBox():
|
60 |
+
for item in messages.value:
|
61 |
+
with solara.lab.ChatMessage(
|
62 |
+
user=item["role"] == "user",
|
63 |
+
name="Echobot" if item["role"] == "assistant" else "User",
|
64 |
+
avatar_background_color="#33cccc" if item["role"] == "assistant" else "#ff991f",
|
65 |
+
border_radius="20px",
|
66 |
+
):
|
67 |
+
solara.Markdown(item["content"])
|
68 |
+
solara.lab.ChatInput(send_callback=send)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
solara==1.31.0
|