Update app.py
Browse files
app.py
CHANGED
|
@@ -1,219 +1,216 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import asyncio
|
| 3 |
-
# Init with fake key
|
| 4 |
-
if 'OPENAI_API_KEY' not in os.environ:
|
| 5 |
-
os.environ['OPENAI_API_KEY'] = 'none'
|
| 6 |
-
if os.name == 'nt':
|
| 7 |
-
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
from
|
| 13 |
-
from
|
| 14 |
-
from
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
import
|
| 18 |
-
|
| 19 |
-
from
|
| 20 |
-
|
| 21 |
-
import
|
| 22 |
-
from
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
buffered =
|
| 29 |
-
|
| 30 |
-
img_str
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
ss =
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
st.chat_message("
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
st.
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
(
|
| 112 |
-
|
| 113 |
-
),
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
)
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
if
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
#
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
# st.markdown("Please input correct files or query ")
|
| 218 |
-
else:
|
| 219 |
-
run_prompt(prompt)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import asyncio
|
| 3 |
+
# Init with fake key
|
| 4 |
+
if 'OPENAI_API_KEY' not in os.environ:
|
| 5 |
+
os.environ['OPENAI_API_KEY'] = 'none'
|
| 6 |
+
if os.name == 'nt':
|
| 7 |
+
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
|
| 8 |
+
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import streamlit as st
|
| 11 |
+
from IPython.core.display import HTML
|
| 12 |
+
from PIL import Image
|
| 13 |
+
from agent import TeLLAgent, make_tools
|
| 14 |
+
from streamlit_callback_handler import \
|
| 15 |
+
StreamlitCallbackHandlerChem
|
| 16 |
+
import base64
|
| 17 |
+
import pandas as pd
|
| 18 |
+
from dotenv import load_dotenv
|
| 19 |
+
from langchain_openai import ChatOpenAI , OpenAI
|
| 20 |
+
import base64
|
| 21 |
+
from io import BytesIO
|
| 22 |
+
from PIL import Image
|
| 23 |
+
import tempfile
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def convert_to_base64(pil_image):
|
| 27 |
+
buffered = BytesIO()
|
| 28 |
+
pil_image.save(buffered, format="PNG")
|
| 29 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 30 |
+
return img_str
|
| 31 |
+
|
| 32 |
+
def oai_key_isvalid(api_key):
|
| 33 |
+
"""Check if a given OpenAI key is valid"""
|
| 34 |
+
try:
|
| 35 |
+
llm = ChatOpenAI(openai_api_key = api_key, base_url="https://www.dmxapi.com/v1/")
|
| 36 |
+
out = llm.invoke("This is a test")
|
| 37 |
+
return True
|
| 38 |
+
except:
|
| 39 |
+
return False
|
| 40 |
+
|
| 41 |
+
load_dotenv()
|
| 42 |
+
ss = st.session_state
|
| 43 |
+
ss.prompt = None
|
| 44 |
+
|
| 45 |
+
# Set width of sidebar
|
| 46 |
+
st.markdown(
|
| 47 |
+
"""
|
| 48 |
+
<style>
|
| 49 |
+
[data-testid="stSidebar"][aria-expanded="true"]{
|
| 50 |
+
min-width: 450px;
|
| 51 |
+
max-width: 450px;
|
| 52 |
+
}
|
| 53 |
+
""",
|
| 54 |
+
unsafe_allow_html=True,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def instantiate_agent(model,file_path = '...',
|
| 59 |
+
image_path ='...'):
|
| 60 |
+
ss.agent = TeLLAgent(
|
| 61 |
+
model=model,
|
| 62 |
+
tools_model=model,
|
| 63 |
+
temp=0.1,
|
| 64 |
+
openai_api_key=ss.get('api_key') , file_path = file_path,
|
| 65 |
+
image_path =image_path
|
| 66 |
+
|
| 67 |
+
)
|
| 68 |
+
return ss.agent
|
| 69 |
+
|
| 70 |
+
instantiate_agent('gpt-4o-2024-11-20')
|
| 71 |
+
tools = ss.agent.agent_executor.tools
|
| 72 |
+
|
| 73 |
+
tool_list = pd.Series(
|
| 74 |
+
{f"✅ {t.name}":t.description for t in tools}
|
| 75 |
+
).reset_index()
|
| 76 |
+
tool_list.columns = ['Tool', 'Description']
|
| 77 |
+
|
| 78 |
+
def on_api_key_change():
|
| 79 |
+
api_key = ss.get('api_key') or os.getenv('OPENAI_API_KEY')
|
| 80 |
+
|
| 81 |
+
# Check if key is valid
|
| 82 |
+
if not oai_key_isvalid(api_key):
|
| 83 |
+
st.write("Please input a valid OpenAI API key.")
|
| 84 |
+
|
| 85 |
+
def run_prompt(prompt, file_path = '...', image_path = '...'):
|
| 86 |
+
agent = instantiate_agent(ss.get('model_select'),file_path = file_path, image_path =image_path)
|
| 87 |
+
st.chat_message("user").write(prompt)
|
| 88 |
+
with st.chat_message("assistant") :
|
| 89 |
+
try:
|
| 90 |
+
|
| 91 |
+
response = agent.run(prompt)
|
| 92 |
+
if ss.get('file_type') == 'CSV (.csv)':
|
| 93 |
+
try:
|
| 94 |
+
fx = pd.DataFrame(list(response))
|
| 95 |
+
st.markdown(":red[Prediction finished! ]")
|
| 96 |
+
st.download_button( "⬇️Download the predicted files as .csv", fx.to_csv(), "predict results.csv", use_container_width=True)
|
| 97 |
+
except:
|
| 98 |
+
st.write(response)
|
| 99 |
+
else:
|
| 100 |
+
st.write(response)
|
| 101 |
+
except:
|
| 102 |
+
st.write("Please input a valid OpenAI API key")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
pre_prompts = [
|
| 107 |
+
'Who are you?',
|
| 108 |
+
('The history and development of Y6'
|
| 109 |
+
|
| 110 |
+
),
|
| 111 |
+
(
|
| 112 |
+
'Predict the LogP of Y6'
|
| 113 |
+
),
|
| 114 |
+
'Generate a donor material with PCE = 10'
|
| 115 |
+
]
|
| 116 |
+
|
| 117 |
+
# sidebar
|
| 118 |
+
with st.sidebar:
|
| 119 |
+
|
| 120 |
+
st.header("🤖 :blue[TeLLAgent] ")
|
| 121 |
+
# Input OpenAI api key
|
| 122 |
+
st.text_input(
|
| 123 |
+
'Input your OpenAI API key.',
|
| 124 |
+
placeholder = 'Input your OpenAI API key.',
|
| 125 |
+
type='password',
|
| 126 |
+
key='api_key',
|
| 127 |
+
on_change=on_api_key_change,
|
| 128 |
+
label_visibility="collapsed"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# Input model to use
|
| 132 |
+
st.selectbox(
|
| 133 |
+
'Select model to use',
|
| 134 |
+
['gpt-4o-2024-11-20', 'deepseek-v3', 'gpt-4o-mini'],
|
| 135 |
+
key='model_select',
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# Display prompt examples
|
| 139 |
+
st.markdown('# What can I ask?')
|
| 140 |
+
cols = st.columns(2)
|
| 141 |
+
with cols[0]:
|
| 142 |
+
st.button(
|
| 143 |
+
r'👑 Who are you ? 🧨 ',
|
| 144 |
+
on_click=lambda: run_prompt(pre_prompts[0]),
|
| 145 |
+
)
|
| 146 |
+
st.button(
|
| 147 |
+
r'📚 The history and development of Y6 ',
|
| 148 |
+
on_click=lambda: run_prompt(pre_prompts[1]),
|
| 149 |
+
)
|
| 150 |
+
with cols[1]:
|
| 151 |
+
st.button(
|
| 152 |
+
r"🎄Predict the LogP of Y6 ",
|
| 153 |
+
on_click=lambda: run_prompt(pre_prompts[2]),
|
| 154 |
+
)
|
| 155 |
+
st.button(
|
| 156 |
+
r'💎 Generate a donor material with PCE = 10',
|
| 157 |
+
on_click=lambda: run_prompt(pre_prompts[3]),
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
st.selectbox(
|
| 161 |
+
'Select the file type ',
|
| 162 |
+
['None', 'CSV (.csv)', 'Figure (.jpg, .png, .jpeg)', 'PDF (.pdf)'],
|
| 163 |
+
key='file_type',
|
| 164 |
+
)
|
| 165 |
+
uploaded_file = None
|
| 166 |
+
if ss.get('file_type') == 'Figure (.jpg, .png, .jpeg)':
|
| 167 |
+
uploaded_file = st.file_uploader("Choose a Figure", type = ["jpg", "jpeg", "png"])
|
| 168 |
+
if ss.get('file_type') == 'PDF (.pdf)':
|
| 169 |
+
uploaded_file = st.file_uploader("Choose a PDF file")
|
| 170 |
+
if ss.get('file_type') == 'CSV (.csv)':
|
| 171 |
+
uploaded_file = st.file_uploader("Choose a csv file", type = 'csv')
|
| 172 |
+
|
| 173 |
+
# Display available tools
|
| 174 |
+
st.markdown(f"# {len(tool_list)} available tools")
|
| 175 |
+
st.dataframe(
|
| 176 |
+
tool_list,
|
| 177 |
+
use_container_width=True,
|
| 178 |
+
hide_index=True,
|
| 179 |
+
height=200
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
# Execute agent on user input
|
| 183 |
+
if prompt := st.chat_input("Say something and/or attach files"):
|
| 184 |
+
|
| 185 |
+
if uploaded_file is not None:
|
| 186 |
+
if ss.get('file_type') == 'CSV (.csv)':
|
| 187 |
+
with tempfile.NamedTemporaryFile( dir = 'j:/', suffix ='.csv' ,delete=False) as f:
|
| 188 |
+
f.write(uploaded_file.read())
|
| 189 |
+
run_prompt(prompt + str(' ') + str(f.name), file_path = f.name)
|
| 190 |
+
f.close()
|
| 191 |
+
|
| 192 |
+
if ss.get('file_type') == 'Figure (.jpg, .png, .jpeg)':
|
| 193 |
+
|
| 194 |
+
st.image(uploaded_file, width = 500)
|
| 195 |
+
with tempfile.NamedTemporaryFile(dir = 'j:/',delete=False, suffix=".png") as temp:
|
| 196 |
+
|
| 197 |
+
mg_str = base64.b64encode(uploaded_file.getvalue()).decode("utf-8")
|
| 198 |
+
temp.write(base64.b64decode(mg_str))
|
| 199 |
+
|
| 200 |
+
run_prompt(prompt+ str(' ') + str(temp.name), image_path = temp.name )
|
| 201 |
+
|
| 202 |
+
if ss.get('file_type') == 'PDF (.pdf)':
|
| 203 |
+
with tempfile.NamedTemporaryFile( dir = 'j:/', suffix ='.pdf' ,delete=False) as f:
|
| 204 |
+
f.write(uploaded_file.read())
|
| 205 |
+
run_prompt(prompt, file_path = f.name)
|
| 206 |
+
f.close()
|
| 207 |
+
|
| 208 |
+
# with open("input.png","wb") as af:
|
| 209 |
+
# mg_str = base64.b64encode(files.getvalue()).decode("utf-8")
|
| 210 |
+
# af.write(base64.b64decode(mg_str))
|
| 211 |
+
|
| 212 |
+
# run_prompt(prompt.text+str(f.name), image_path =f.name )
|
| 213 |
+
# except:
|
| 214 |
+
# st.markdown("Please input correct files or query ")
|
| 215 |
+
else:
|
| 216 |
+
run_prompt(prompt)
|
|
|
|
|
|
|
|
|