Spaces:
Running
Running
omitakahiro
commited on
Commit
•
220b833
1
Parent(s):
b92bc92
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
|
5 |
+
import requests
|
6 |
+
import streamlit as st
|
7 |
+
|
8 |
+
CHATBOT_ENDPOINT = os.environ["CHATBOT_ENDPOINT"]
|
9 |
+
TOKEN = os.environ["TOKEN"]
|
10 |
+
|
11 |
+
def generate(prompt):
|
12 |
+
|
13 |
+
try:
|
14 |
+
r = requests.post(
|
15 |
+
CHATBOT_ENDPOINT,
|
16 |
+
data=json.dumps({"instruction": prompt, "token": TOKEN}),
|
17 |
+
headers = {"content-type": "application/json"},
|
18 |
+
timeout = 20,
|
19 |
+
stream = True,
|
20 |
+
)
|
21 |
+
new_str = b""
|
22 |
+
for s in r.iter_content():
|
23 |
+
new_str += s
|
24 |
+
try:
|
25 |
+
output_str = new_str.decode("utf-8")
|
26 |
+
yield output_str.replace("\n", " \n")
|
27 |
+
new_str = b""
|
28 |
+
except:
|
29 |
+
pass
|
30 |
+
except:
|
31 |
+
yield "<<Some errors occured>>"
|
32 |
+
|
33 |
+
|
34 |
+
st.title("Stockmark-LLM-100b")
|
35 |
+
|
36 |
+
prompt = st.session_state.get("prompt", "")
|
37 |
+
response = st.session_state.get("response", "")
|
38 |
+
|
39 |
+
if prompt == "" or response:
|
40 |
+
print("new_session")
|
41 |
+
prompt_new = st.text_area("Prompt:")
|
42 |
+
if prompt_new:
|
43 |
+
st.session_state["prompt"] = prompt_new
|
44 |
+
st.session_state["response"] = ""
|
45 |
+
st.rerun()
|
46 |
+
else:
|
47 |
+
prompt = st.text_area("Prompt:", value=prompt, disabled=True)
|
48 |
+
|
49 |
+
if prompt:
|
50 |
+
|
51 |
+
if response:
|
52 |
+
with st.chat_message("assistant"):
|
53 |
+
st.write(response)
|
54 |
+
else:
|
55 |
+
with st.chat_message("assistant"):
|
56 |
+
response = st.write_stream(generate(prompt))
|
57 |
+
|
58 |
+
st.session_state["response"] = response
|
59 |
+
st.rerun()
|