-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added streamlit app with ability to upload files (#74)
- Loading branch information
Showing
9 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import os | ||
from typing import List | ||
|
||
import streamlit as st | ||
from dotenv import load_dotenv | ||
from langchain.callbacks import StreamlitCallbackHandler | ||
from langchain.callbacks.base import BaseCallbackHandler | ||
|
||
from mdagent import MDAgent | ||
|
||
load_dotenv() | ||
|
||
|
||
st_callback = StreamlitCallbackHandler(st.container()) | ||
|
||
|
||
# Streamlit app | ||
st.title("MDAgent") | ||
|
||
# option = st.selectbox("Choose an option:", ["Explore & Learn", "Use Learned Skills"]) | ||
# if option == "Explore & Learn": | ||
# explore = True | ||
# else: | ||
# explore = False | ||
|
||
resume_op = st.selectbox("Resume:", ["False", "True"]) | ||
if resume_op == "True": | ||
resume = True | ||
else: | ||
resume = False | ||
|
||
# for now I'm just going to allow pdb and cif files - we can add more later | ||
uploaded_files = st.file_uploader( | ||
"Upload a .pdb or .cif file", type=["pdb", "cif"], accept_multiple_files=True | ||
) | ||
files: List[str] = [] | ||
# write file to disk | ||
if uploaded_files: | ||
for file in uploaded_files: | ||
with open(file.name, "wb") as f: | ||
f.write(file.getbuffer()) | ||
|
||
st.write("Files successfully uploaded!") | ||
uploaded_file = [os.path.join(os.getcwd(), file.name) for file in uploaded_files] | ||
else: | ||
uploaded_file = [] | ||
|
||
mdagent = MDAgent(resume=resume, uploaded_files=uploaded_file) | ||
|
||
|
||
def generate_response(prompt): | ||
result = mdagent.run(prompt) | ||
return result | ||
|
||
|
||
# make new container to store scratch | ||
scratch = st.empty() | ||
scratch.write( | ||
"""Hi! I am MDAgent, your MD automation assistant. | ||
How can I help you today?""" | ||
) | ||
|
||
|
||
# This allows streaming of llm tokens | ||
class TokenStreamlitCallbackHandler(BaseCallbackHandler): | ||
def __init__(self, container): | ||
self.container = container | ||
|
||
def on_llm_new_token(self, token, **kwargs): | ||
self.container.write("".join(token)) | ||
|
||
|
||
token_st_callback = TokenStreamlitCallbackHandler(scratch) | ||
|
||
if prompt := st.chat_input(): | ||
st.chat_message("user").write(prompt) | ||
with st.chat_message("assistant"): | ||
st_callback = StreamlitCallbackHandler(st.container()) | ||
response = mdagent.run(prompt, callbacks=[st_callback, token_st_callback]) | ||
st.write(response) |