Spaces:
Runtime error
Runtime error
wangrongsheng
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import ollama
|
3 |
+
|
4 |
+
model_list = ollama.list()
|
5 |
+
|
6 |
+
if "model_name" not in st.session_state:
|
7 |
+
st.session_state["model_name"] = "wangrongsheng/aurora"
|
8 |
+
|
9 |
+
if "messages" not in st.session_state:
|
10 |
+
st.session_state.messages = []
|
11 |
+
|
12 |
+
with st.sidebar:
|
13 |
+
st.subheader("Settings")
|
14 |
+
|
15 |
+
option = st.selectbox(
|
16 |
+
'Select a model',
|
17 |
+
[model['name'] for model in model_list['models']])
|
18 |
+
st.write('You selected:', option)
|
19 |
+
st.session_state["model_name"] = option
|
20 |
+
|
21 |
+
st.title(f"Chat with {st.session_state['model_name']}")
|
22 |
+
|
23 |
+
for message in st.session_state.messages:
|
24 |
+
with st.chat_message(message["role"]):
|
25 |
+
st.markdown(message["content"])
|
26 |
+
|
27 |
+
if prompt := st.chat_input("What is up?"):
|
28 |
+
|
29 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
30 |
+
|
31 |
+
with st.chat_message("user"):
|
32 |
+
st.markdown(prompt)
|
33 |
+
|
34 |
+
with st.chat_message("assistant"):
|
35 |
+
message_placeholder = st.empty()
|
36 |
+
full_response = ""
|
37 |
+
for chunk in ollama.chat(
|
38 |
+
model=st.session_state["model_name"],
|
39 |
+
messages=[
|
40 |
+
{"role": m["role"], "content": m["content"]}
|
41 |
+
for m in st.session_state.messages
|
42 |
+
],
|
43 |
+
stream=True,
|
44 |
+
):
|
45 |
+
if 'message' in chunk and 'content' in chunk['message']:
|
46 |
+
full_response += (chunk['message']['content'] or "")
|
47 |
+
message_placeholder.markdown(full_response + "▌")
|
48 |
+
message_placeholder.markdown(full_response)
|
49 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|