Commit
•
97cdf09
1
Parent(s):
c68da4a
add app
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import requests
|
4 |
+
import streamlit as st
|
5 |
+
from IPython.core.display import HTML
|
6 |
+
from langchain.agents import AgentType, initialize_agent, load_tools
|
7 |
+
from langchain.chat_models import ChatOpenAI
|
8 |
+
from PIL import Image
|
9 |
+
from rmrkl import ChatZeroShotAgent, RetryAgentExecutor
|
10 |
+
|
11 |
+
from chemcrow.agents import ChemCrow
|
12 |
+
#from chemcrow
|
13 |
+
from chemcrow.agents.prompts import (FORMAT_INSTRUCTIONS, QUESTION_PROMPT,
|
14 |
+
SUFFIX)
|
15 |
+
from chemcrow.frontend.streamlit_callback_handler import \
|
16 |
+
StreamlitCallbackHandlerChem
|
17 |
+
from langchain.callbacks import StreamlitCallbackHandler
|
18 |
+
from chemcrow.frontend.utils import cdk
|
19 |
+
#from chemcrow.mol_utils.generals import is_smiles
|
20 |
+
|
21 |
+
from dotenv import load_dotenv
|
22 |
+
|
23 |
+
load_dotenv()
|
24 |
+
ss = st.session_state
|
25 |
+
|
26 |
+
#tools = ChemTools().all_tools
|
27 |
+
|
28 |
+
|
29 |
+
agent = ChemCrow(
|
30 |
+
#tools,
|
31 |
+
model='gpt-4',
|
32 |
+
temp=0.1,
|
33 |
+
).agent_executor
|
34 |
+
|
35 |
+
|
36 |
+
#tool_list = pd.Series(
|
37 |
+
# {f"✅ {t.name}":t.description for t in tools}
|
38 |
+
#).reset_index()
|
39 |
+
#tool_list.columns = ['Tool', 'Description']
|
40 |
+
|
41 |
+
|
42 |
+
icon = Image.open('assets/logo0.png')
|
43 |
+
st.set_page_config(
|
44 |
+
page_title="ChemCrow",
|
45 |
+
page_icon = icon
|
46 |
+
)
|
47 |
+
|
48 |
+
# Set width of sidebar
|
49 |
+
st.markdown(
|
50 |
+
"""
|
51 |
+
<style>
|
52 |
+
[data-testid="stSidebar"][aria-expanded="true"]{
|
53 |
+
min-width: 450px;
|
54 |
+
max-width: 450px;
|
55 |
+
}
|
56 |
+
""",
|
57 |
+
unsafe_allow_html=True,
|
58 |
+
)
|
59 |
+
|
60 |
+
# Session state
|
61 |
+
# st.session_state['molecule'] = "CCO"
|
62 |
+
def on_api_key_change():
|
63 |
+
api_key = ss.get('api_key') or os.getenv('OPENAI_API_KEY')
|
64 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
65 |
+
|
66 |
+
|
67 |
+
# sidebar
|
68 |
+
with st.sidebar:
|
69 |
+
chemcrow_logo = Image.open('assets/chemcrow-logo-bold-new.png')
|
70 |
+
st.image(chemcrow_logo)
|
71 |
+
|
72 |
+
# Input OpenAI api key
|
73 |
+
st.markdown('Input your OpenAI API key.')
|
74 |
+
st.text_input('OpenAI API key', type='password', key='api_key', on_change=on_api_key_change, label_visibility="collapsed")
|
75 |
+
|
76 |
+
# Display available tools
|
77 |
+
#st.markdown(f"# Available tools: {len(tools)}")
|
78 |
+
#st.dataframe(
|
79 |
+
# tool_list,
|
80 |
+
# use_container_width=True,
|
81 |
+
# hide_index=True,
|
82 |
+
# height=300
|
83 |
+
|
84 |
+
#)
|
85 |
+
|
86 |
+
|
87 |
+
print(st.session_state)
|
88 |
+
# Agent execution
|
89 |
+
if prompt := st.chat_input():
|
90 |
+
st.chat_message("user").write(prompt)
|
91 |
+
with st.chat_message("assistant"):
|
92 |
+
st_callback = StreamlitCallbackHandlerChem(
|
93 |
+
st.container(),
|
94 |
+
max_thought_containers = 4,
|
95 |
+
collapse_completed_thoughts = False,
|
96 |
+
output_placeholder=st.session_state
|
97 |
+
)
|
98 |
+
#try:
|
99 |
+
# TODO Modify this, not taking callbacks
|
100 |
+
response = agent.run(prompt, callbacks=[st_callback])
|
101 |
+
st.write(response)
|
102 |
+
#except:
|
103 |
+
# st.write("Please input a valid OpenAI API key.")
|
104 |
+
|
105 |
+
|