Spaces:
Sleeping
Sleeping
한용
commited on
Commit
•
b3d271f
1
Parent(s):
ccefbb7
dd
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import time
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
|
6 |
+
# Streamed response emulator
|
7 |
+
def response_generator(responseDatsa):
|
8 |
+
for word in responseDatsa.split():
|
9 |
+
yield word + " "
|
10 |
+
time.sleep(0.05)
|
11 |
+
|
12 |
+
st.title("KRA 챗봇")
|
13 |
+
|
14 |
+
|
15 |
+
model_radio = st.sidebar.radio("Select model",(
|
16 |
+
"KoAlpaca","FineTuned")
|
17 |
+
)
|
18 |
+
st.sidebar.text("ver 022715")
|
19 |
+
|
20 |
+
# Initialize chat history
|
21 |
+
if "messages" not in st.session_state:
|
22 |
+
st.session_state.messages = []
|
23 |
+
|
24 |
+
# Display chat messages from history on app rerun
|
25 |
+
for message in st.session_state.messages:
|
26 |
+
with st.chat_message(message["role"]):
|
27 |
+
st.markdown(message["content"])
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
# React to user input
|
32 |
+
if prompt := st.chat_input("What is up?"):
|
33 |
+
# Display user message in chat message container
|
34 |
+
with st.chat_message("user"):
|
35 |
+
st.markdown(prompt)
|
36 |
+
# Add user message to chat history
|
37 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
38 |
+
|
39 |
+
|
40 |
+
data={
|
41 |
+
'content': prompt
|
42 |
+
}
|
43 |
+
print(data)
|
44 |
+
# REST API 를 호출해야함.
|
45 |
+
url = "http://3.39.53.42:8000/chat" if model_radio == "FineTuned" else "http://3.37.154.147:8000/chat"
|
46 |
+
|
47 |
+
serverRsp = requests.post(url, json=data, headers={"Content-Type": "application/json"},verify=False)
|
48 |
+
#print(json.dumps(serverRsp))
|
49 |
+
#serverRsp = requests.get(url)
|
50 |
+
if serverRsp.status_code == 200:
|
51 |
+
#print(serverRsp.json())
|
52 |
+
data = serverRsp.json()
|
53 |
+
response = data["content"]
|
54 |
+
|
55 |
+
else:
|
56 |
+
response = "에러가 발생하였습니다."
|
57 |
+
|
58 |
+
|
59 |
+
#response = f"Echo: {prompt}"
|
60 |
+
|
61 |
+
# Display assistant response in chat message container
|
62 |
+
with st.chat_message("assistant"):
|
63 |
+
st.write_stream(response_generator(response))
|
64 |
+
# Add assistant response to chat history
|
65 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
66 |
+
|
67 |
+
|