Spaces:
Runtime error
Runtime error
DHRUV SHEKHAWAT
commited on
Commit
·
fd264ea
1
Parent(s):
8a4389b
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,23 +3,16 @@ from streamlit_chat import message
|
|
| 3 |
import requests
|
| 4 |
|
| 5 |
url = 'https://api.webraft.in/v1/chat/completions'
|
| 6 |
-
|
| 7 |
-
import json
|
| 8 |
-
# Setting page title and header
|
| 9 |
st.set_page_config(page_title="WebraftAI Chat", page_icon=":robot_face:")
|
| 10 |
-
st.markdown("<h1 style='text-align: center;'>WebraftAI Chat
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
-
# Initialise session state variables
|
| 15 |
if 'generated' not in st.session_state:
|
| 16 |
st.session_state['generated'] = []
|
| 17 |
if 'past' not in st.session_state:
|
| 18 |
st.session_state['past'] = []
|
| 19 |
-
|
| 20 |
-
st.session_state['messages'] = [
|
| 21 |
-
{"role": "system", "content": "You are a helpful assistant."}
|
| 22 |
-
]
|
| 23 |
if 'model_name' not in st.session_state:
|
| 24 |
st.session_state['model_name'] = []
|
| 25 |
if 'cost' not in st.session_state:
|
|
@@ -31,29 +24,41 @@ if 'total_cost' not in st.session_state:
|
|
| 31 |
if 'api_key' not in st.session_state:
|
| 32 |
st.session_state['api_key']=""
|
| 33 |
|
| 34 |
-
# Sidebar - let user choose model, show total cost of current conversation, and let user clear the current conversation
|
| 35 |
st.sidebar.title("Settings")
|
| 36 |
-
model_name = st.sidebar.selectbox("Model:", ("gpt3.5","gpt4-32k"))
|
| 37 |
counter_placeholder = st.sidebar.empty()
|
| 38 |
-
|
|
|
|
| 39 |
st.session_state['api_key'] = api_key
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
clear_button = st.sidebar.button("Clear Conversation", key="clear")
|
| 41 |
headers = {
|
| 42 |
'Content-Type': 'application/json',
|
| 43 |
'Authorization': f'Bearer {api_key}',
|
| 44 |
}
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
if model_name == "gpt3.5":
|
| 47 |
model = "gpt-3.5-turbo"
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
model = "gpt4-32k"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
# reset everything
|
| 52 |
if clear_button:
|
| 53 |
st.session_state['generated'] = []
|
| 54 |
st.session_state['past'] = []
|
| 55 |
st.session_state['messages'] = [
|
| 56 |
-
{"role": "system", "content":
|
| 57 |
]
|
| 58 |
st.session_state['number_tokens'] = []
|
| 59 |
st.session_state['model_name'] = []
|
|
@@ -62,7 +67,6 @@ if clear_button:
|
|
| 62 |
st.session_state['total_tokens'] = []
|
| 63 |
|
| 64 |
|
| 65 |
-
|
| 66 |
def evaluate(model, question, max_tokens=4096, top_p=0.7,temperature=0.1):
|
| 67 |
|
| 68 |
data = {
|
|
@@ -79,12 +83,12 @@ def evaluate(model, question, max_tokens=4096, top_p=0.7,temperature=0.1):
|
|
| 79 |
|
| 80 |
return sentence
|
| 81 |
|
| 82 |
-
|
| 83 |
def generate_response(prompt):
|
| 84 |
st.session_state['messages'].append({"role": "user", "content": prompt})
|
| 85 |
question = st.session_state['messages']
|
| 86 |
|
| 87 |
-
sentence = evaluate(model, question)
|
| 88 |
|
| 89 |
response = sentence
|
| 90 |
st.session_state['messages'].append({"role": "assistant", "content": response})
|
|
@@ -96,9 +100,7 @@ def generate_response(prompt):
|
|
| 96 |
return response, total_tokens, prompt_tokens, completion_tokens
|
| 97 |
|
| 98 |
|
| 99 |
-
# container for chat history
|
| 100 |
response_container = st.container()
|
| 101 |
-
# container for text box
|
| 102 |
container = st.container()
|
| 103 |
|
| 104 |
with container:
|
|
@@ -113,13 +115,6 @@ with container:
|
|
| 113 |
st.session_state['model_name'].append(model_name)
|
| 114 |
st.session_state['total_tokens'].append(total_tokens)
|
| 115 |
|
| 116 |
-
# from https://openai.com/pricing#language-models
|
| 117 |
-
if model_name == "30M_6.1K":
|
| 118 |
-
cost = "1"
|
| 119 |
-
else:
|
| 120 |
-
cost = "2"
|
| 121 |
-
|
| 122 |
-
|
| 123 |
|
| 124 |
if st.session_state['generated']:
|
| 125 |
with response_container:
|
|
|
|
| 3 |
import requests
|
| 4 |
|
| 5 |
url = 'https://api.webraft.in/v1/chat/completions'
|
|
|
|
|
|
|
|
|
|
| 6 |
st.set_page_config(page_title="WebraftAI Chat", page_icon=":robot_face:")
|
| 7 |
+
st.markdown("<h1 style='text-align: center;'>WebraftAI Chat</h1>", unsafe_allow_html=True)
|
| 8 |
|
| 9 |
|
| 10 |
|
|
|
|
| 11 |
if 'generated' not in st.session_state:
|
| 12 |
st.session_state['generated'] = []
|
| 13 |
if 'past' not in st.session_state:
|
| 14 |
st.session_state['past'] = []
|
| 15 |
+
|
|
|
|
|
|
|
|
|
|
| 16 |
if 'model_name' not in st.session_state:
|
| 17 |
st.session_state['model_name'] = []
|
| 18 |
if 'cost' not in st.session_state:
|
|
|
|
| 24 |
if 'api_key' not in st.session_state:
|
| 25 |
st.session_state['api_key']=""
|
| 26 |
|
|
|
|
| 27 |
st.sidebar.title("Settings")
|
| 28 |
+
model_name = st.sidebar.selectbox("Model:", ("gpt3.5","gpt3.5-16k","gpt4-32k","mini-orca-v3-13b"))
|
| 29 |
counter_placeholder = st.sidebar.empty()
|
| 30 |
+
|
| 31 |
+
api_key = st.sidebar.text_input("API_Key", value=st.session_state['api_key'], max_chars=None,placeholder=st.session_state['api_key'], key=None, type="password", label_visibility="visible")
|
| 32 |
st.session_state['api_key'] = api_key
|
| 33 |
+
max_tokens = st.sidebar.text_input("Max_tokens", value=4096, max_chars=None, key=None,placeholder=4096, type="default")
|
| 34 |
+
top_p = st.sidebar.text_input("Top_p", value=0.7, max_chars=None, key=None,placeholder=0.7, type="default")
|
| 35 |
+
temperature = st.sidebar.text_input("Temperature", value=0.7, max_chars=None, key=None,placeholder=0.7, type="default")
|
| 36 |
+
system_message = st.sidebar.text_box("System_message", value="You are a helpful AI assistant.", max_chars=None, key=None,placeholder="You are a helpful AI assistant.", type="default")
|
| 37 |
clear_button = st.sidebar.button("Clear Conversation", key="clear")
|
| 38 |
headers = {
|
| 39 |
'Content-Type': 'application/json',
|
| 40 |
'Authorization': f'Bearer {api_key}',
|
| 41 |
}
|
| 42 |
+
if 'messages' not in st.session_state:
|
| 43 |
+
st.session_state['messages'] = [
|
| 44 |
+
{"role": "system", "content": system_message}
|
| 45 |
+
]
|
| 46 |
if model_name == "gpt3.5":
|
| 47 |
model = "gpt-3.5-turbo"
|
| 48 |
+
elif model_name == "gpt3.5-16k":
|
| 49 |
+
model = "gpt-3.5-turbo-16k"
|
| 50 |
+
elif model_name == "gpt4-32k":
|
| 51 |
model = "gpt4-32k"
|
| 52 |
+
elif model_name == "mini-orca-v3-13b":
|
| 53 |
+
model = "orca-mini-v3-13b"
|
| 54 |
+
else:
|
| 55 |
+
model = "gpt-3.5-turbo"
|
| 56 |
|
|
|
|
| 57 |
if clear_button:
|
| 58 |
st.session_state['generated'] = []
|
| 59 |
st.session_state['past'] = []
|
| 60 |
st.session_state['messages'] = [
|
| 61 |
+
{"role": "system", "content": system_message}
|
| 62 |
]
|
| 63 |
st.session_state['number_tokens'] = []
|
| 64 |
st.session_state['model_name'] = []
|
|
|
|
| 67 |
st.session_state['total_tokens'] = []
|
| 68 |
|
| 69 |
|
|
|
|
| 70 |
def evaluate(model, question, max_tokens=4096, top_p=0.7,temperature=0.1):
|
| 71 |
|
| 72 |
data = {
|
|
|
|
| 83 |
|
| 84 |
return sentence
|
| 85 |
|
| 86 |
+
|
| 87 |
def generate_response(prompt):
|
| 88 |
st.session_state['messages'].append({"role": "user", "content": prompt})
|
| 89 |
question = st.session_state['messages']
|
| 90 |
|
| 91 |
+
sentence = evaluate(model, question,top_p=top_p,max_tokens=max_tokens,temperature=temperature)
|
| 92 |
|
| 93 |
response = sentence
|
| 94 |
st.session_state['messages'].append({"role": "assistant", "content": response})
|
|
|
|
| 100 |
return response, total_tokens, prompt_tokens, completion_tokens
|
| 101 |
|
| 102 |
|
|
|
|
| 103 |
response_container = st.container()
|
|
|
|
| 104 |
container = st.container()
|
| 105 |
|
| 106 |
with container:
|
|
|
|
| 115 |
st.session_state['model_name'].append(model_name)
|
| 116 |
st.session_state['total_tokens'].append(total_tokens)
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
if st.session_state['generated']:
|
| 120 |
with response_container:
|