Create App.py
Browse files
App.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import solara
|
4 |
+
from solara_enterprise import auth
|
5 |
+
from reacton import ipyvuetify as rv
|
6 |
+
from functools import partial
|
7 |
+
|
8 |
+
os.environ['SOLARA_OAUTH_API_BASE_URL']="dev-r2bw2n3hwf72bni6.us.auth0.com" # replace with your domain
|
9 |
+
os.environ['SOLARA_OAUTH_CLIENT_ID']="lQl2yjR7dr7XVbK480OJ2sfgoWf6U7s2" # replace with your client ID
|
10 |
+
os.environ['SOLARA_OAUTH_CLIENT_SECRET']="BsGtc_GLEZgsv4DsZnSya4YhB38nm7ohJp5kobfOVb2X8CeT9XTHMOxnXR_djQMd" # not shown here, replace with your client secret
|
11 |
+
|
12 |
+
os.environ['REPLICATE_API_TOKEN']='r8_YSD53gSw5IQbT8Fmnekc8ypEIuy28wN4IG45C'
|
13 |
+
|
14 |
+
import replicate
|
15 |
+
|
16 |
+
@solara.component
|
17 |
+
def Message(message):
|
18 |
+
icon = "mdi-robot" if message['user']=='Assistant' else 'mdi-account'
|
19 |
+
icon = rv.Icon(children=[icon])
|
20 |
+
user = rv.CardTitle(icon=icon,children=[solara.Columns([0.6,2,10],children=[icon,message['user'],rv.Spacer()])])
|
21 |
+
text = rv.CardText(children=[message['text']])
|
22 |
+
messageDiv = rv.Card(_class="mx-auto",width="100%",flat=True,children=[user,text])
|
23 |
+
return messageDiv
|
24 |
+
|
25 |
+
def Redo():
|
26 |
+
message = messages.value[-2]['text']
|
27 |
+
messages.set(messages.value[:-2])
|
28 |
+
sendMessage(message)
|
29 |
+
|
30 |
+
@solara.component
|
31 |
+
def InputText():
|
32 |
+
InputText = solara.InputText("Enter some text",value=input_text)
|
33 |
+
rv.use_event(InputText,"keypress",partial(handleEnter))
|
34 |
+
InputText
|
35 |
+
|
36 |
+
def sendMessage(message):
|
37 |
+
name = str(auth.user.value['userinfo']['name']).split(" ")[0]
|
38 |
+
messages.set(messages.value+[{'user':name,'text':message}])
|
39 |
+
prompt.value += f"[INST] {message} [/INST] \n"
|
40 |
+
messages.set(messages.value+[{'user':'Assistant','text':''}])
|
41 |
+
response = ''
|
42 |
+
for token in replicate.stream(
|
43 |
+
"meta/llama-2-7b-chat",
|
44 |
+
input={
|
45 |
+
"debug": False,
|
46 |
+
"top_p": 1,
|
47 |
+
"prompt": prompt.value,
|
48 |
+
"temperature": 0.75,
|
49 |
+
"system_prompt": f"You are a helpful, respectful and honest assistant. You are tasked with helping {name} with figuring out the correct European Binding Tariff Information for their shipment. Please address them by their name and their needs.",
|
50 |
+
"max_new_tokens": 800,
|
51 |
+
"min_new_tokens": -1,
|
52 |
+
"prompt_template": "[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n{prompt} [/INST]",
|
53 |
+
"repetition_penalty": 1
|
54 |
+
},
|
55 |
+
):
|
56 |
+
response += str(token)
|
57 |
+
messages.set(messages.value[:-1]+[{'user':'Assistant', 'text':response}])
|
58 |
+
prompt.value += f"{messages.value[-1]['text']}"
|
59 |
+
print('Boom')
|
60 |
+
|
61 |
+
|
62 |
+
def handleEnter(*args):
|
63 |
+
if str(args[2]['key'])=='Enter':
|
64 |
+
message = args[0].v_model
|
65 |
+
print(message)
|
66 |
+
sendMessage(message)
|
67 |
+
|
68 |
+
|
69 |
+
prompt = solara.reactive('')
|
70 |
+
messages = solara.reactive([])
|
71 |
+
input_text = solara.reactive('')
|
72 |
+
|
73 |
+
@solara.component
|
74 |
+
def ColumnLayout(children):
|
75 |
+
solara.Columns([2,6,2],children=[solara.Column(),solara.Column(children),solara.Column()])
|
76 |
+
|
77 |
+
|
78 |
+
@solara.component
|
79 |
+
def Page():
|
80 |
+
with solara.AppLayout(title="EBTI GPT"):
|
81 |
+
solara.AppBarTitle(children=[])
|
82 |
+
with rv.Card(flat=True):
|
83 |
+
with ColumnLayout():
|
84 |
+
if not auth.user.value:
|
85 |
+
solara.Markdown(f"### Please Login ")
|
86 |
+
solara.Button("Login", icon_name="mdi-login", href=auth.get_login_url())
|
87 |
+
else:
|
88 |
+
userinfo = auth.user.value['userinfo']
|
89 |
+
if 'name' in userinfo:
|
90 |
+
solara.Markdown(f"### Welcome {userinfo['name']}")
|
91 |
+
with ColumnLayout():
|
92 |
+
rv.Card(flat=True,children=[Message(message) for message in messages.value])
|
93 |
+
with ColumnLayout():
|
94 |
+
InputText()
|
95 |
+
with ColumnLayout():
|
96 |
+
solara.Button("Logout", icon_name="mdi-logout", href=auth.get_logout_url())
|
97 |
+
Page()
|