Spaces:
Runtime error
Runtime error
DHRUV SHEKHAWAT
commited on
Commit
·
275e579
1
Parent(s):
0d81cea
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
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/h1>", unsafe_allow_html=True)
|
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 |
+
if 'messages' not in st.session_state:
|
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:
|
26 |
+
st.session_state['cost'] = []
|
27 |
+
if 'total_tokens' not in st.session_state:
|
28 |
+
st.session_state['total_tokens'] = []
|
29 |
+
if 'total_cost' not in st.session_state:
|
30 |
+
st.session_state['total_cost'] = 1
|
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 |
+
api_key = st.sidebar.text_input("API_Key", value=st.session_state['api_key'], max_chars=None, key=None, type="password", label_visibility="visible")
|
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 |
+
# Map model names to OpenAI model IDs
|
46 |
+
if model_name == "gpt3.5":
|
47 |
+
model = "gpt-3.5-turbo"
|
48 |
+
else:
|
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": "You are a helpful assistant."}
|
57 |
+
]
|
58 |
+
st.session_state['number_tokens'] = []
|
59 |
+
st.session_state['model_name'] = []
|
60 |
+
st.session_state['cost'] = []
|
61 |
+
st.session_state['total_cost'] = 0.0
|
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 = {
|
69 |
+
"model": model,
|
70 |
+
"max_tokens": max_tokens,
|
71 |
+
"messages": question,
|
72 |
+
"temperature":temperature,
|
73 |
+
"top_p":top_p
|
74 |
+
|
75 |
+
}
|
76 |
+
response = requests.post(url, headers=headers, json=data)
|
77 |
+
response = response.json()
|
78 |
+
sentence = response['choices'][0]['message']['content']
|
79 |
+
|
80 |
+
return sentence
|
81 |
+
|
82 |
+
# generate a response
|
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})
|
91 |
+
|
92 |
+
# print(st.session_state['messages'])
|
93 |
+
total_tokens = "153"
|
94 |
+
prompt_tokens = "153"
|
95 |
+
completion_tokens = "153"
|
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:
|
105 |
+
with st.form(key='my_form', clear_on_submit=True):
|
106 |
+
user_input = st.text_area("You:", key='input', height=2)
|
107 |
+
submit_button = st.form_submit_button(label='✉')
|
108 |
+
|
109 |
+
if submit_button and user_input:
|
110 |
+
output, total_tokens, prompt_tokens, completion_tokens = generate_response(user_input)
|
111 |
+
st.session_state['past'].append(user_input)
|
112 |
+
st.session_state['generated'].append(output)
|
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:
|
126 |
+
for i in range(len(st.session_state['generated'])):
|
127 |
+
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user')
|
128 |
+
message(st.session_state["generated"][i], key=str(i))
|