-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
70 lines (57 loc) · 2.12 KB
/
app.py
File metadata and controls
70 lines (57 loc) · 2.12 KB
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
import streamlit as st
import requests
import json
from streamlit_lottie import st_lottie
from streamlit_extras.colored_header import colored_header
from streamlit_extras.add_vertical_space import add_vertical_space
st.set_page_config(page_title="Chatbot", page_icon="🦙", layout="wide")
def chat(messages):
request = requests.post(
"http://0.0.0.0:11434/api/chat",
json={"model": "llama3", "messages": messages, "stream": True},
stream=True
)
request.raise_for_status()
output = ""
for line in request.iter_lines():
body = json.loads(line)
if "error" in body:
raise Exception(body["error"])
if body.get("done") is False:
message = body.get("message", "")
content = message.get("content", "")
output += content
yield content
message = {"role": "assistant", "content": output}
return message
st.title('🦙 Llama 3 - 8B Bot')
st.write('Interact with llama 3 locally!')
with st.sidebar:
st.title("🦙 Llama 3 Chatbot")
st.markdown('''
## About
This chatbot is powered by Llama 3 - 8B model:
- 🧠 Local AI
- 💬 Interactive Chat
- 🚀 Fast Responses
''')
st.markdown("### Made by Nishchal")
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("How can I help you Nishchal?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
for response in chat(st.session_state.messages):
full_response += response
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
if st.button("Clear Chat History"):
st.session_state.messages = []