Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,76 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
if
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import streamlit as st
|
3 |
+
from transformers import AutoTokenizer, OPTForCausalLM
|
4 |
+
|
5 |
+
|
6 |
+
@st.cache_resource
|
7 |
+
def load_model():
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/galactica-30b")
|
9 |
+
model = OPTForCausalLM.from_pretrained("facebook/galactica-30b", device_map='auto', low_cpu_mem_usage=True, torch_dtype=torch.float16)
|
10 |
+
model.gradient_checkpointing_enable()
|
11 |
+
return tokenizer, model
|
12 |
+
|
13 |
+
|
14 |
+
st.set_page_config(
|
15 |
+
page_title='BioML-SVM',
|
16 |
+
layout="wide"
|
17 |
+
)
|
18 |
+
|
19 |
+
with st.spinner("Loading Models and Tokens..."):
|
20 |
+
tokenizer, model = load_model()
|
21 |
+
|
22 |
+
with st.form(key='my_form'):
|
23 |
+
col1, col2 = st.columns([10, 1])
|
24 |
+
text_input = col1.text_input(label='Enter the amino sequence')
|
25 |
+
with col2:
|
26 |
+
st.text('')
|
27 |
+
st.text('')
|
28 |
+
submit_button = st.form_submit_button(label='Submit')
|
29 |
+
|
30 |
+
if submit_button:
|
31 |
+
st.session_state['result_done'] = False
|
32 |
+
# input_text = "[START_AMINO]GHMQSITAGQKVISKHKNGRFYQCEVVRLTTETFYEVNFDDGSFSDNLYPEDIVSQDCLQFGPPAEGEVVQVRWTDGQVYGAKFVASHPIQMYQVEFEDGSQLVVKRDDVYTLDEELP[END_AMINO]"
|
33 |
+
with st.spinner('Generating...'):
|
34 |
+
# formatted_text = f"[START_AMINO]{text_input}[END_AMINO]"
|
35 |
+
# formatted_text = f"Here is the sequence: [START_AMINO]{text_input}[END_AMINO]"
|
36 |
+
formatted_text = f"{text_input}"
|
37 |
+
input_ids = tokenizer(formatted_text, return_tensors="pt").input_ids.to("cuda")
|
38 |
+
outputs = model.generate(
|
39 |
+
input_ids=input_ids,
|
40 |
+
max_new_tokens=500
|
41 |
+
)
|
42 |
+
result = tokenizer.decode(outputs[0]).replace(formatted_text, "")
|
43 |
+
st.markdown(result)
|
44 |
+
|
45 |
+
if 'result_done' not in st.session_state or not st.session_state.result_done:
|
46 |
+
st.session_state['result_done'] = True
|
47 |
+
st.session_state['previous_state'] = result
|
48 |
+
else:
|
49 |
+
if 'result_done' in st.session_state and st.session_state.result_done:
|
50 |
+
st.markdown(st.session_state.previous_state)
|
51 |
+
|
52 |
+
if 'result_done' in st.session_state and st.session_state.result_done:
|
53 |
+
with st.form(key='ask_more'):
|
54 |
+
col1, col2 = st.columns([10, 1])
|
55 |
+
text_input = col1.text_input(label='Ask more question')
|
56 |
+
with col2:
|
57 |
+
st.text('')
|
58 |
+
st.text('')
|
59 |
+
submit_button = st.form_submit_button(label='Submit')
|
60 |
+
|
61 |
+
if submit_button:
|
62 |
+
with st.spinner('Generating...'):
|
63 |
+
# formatted_text = f"[START_AMINO]{text_input}[END_AMINO]"
|
64 |
+
formatted_text = f"Q:{text_input}\n\nA:\n\n"
|
65 |
+
input_ids = tokenizer(formatted_text, return_tensors="pt").input_ids.to("cuda")
|
66 |
+
|
67 |
+
outputs = model.generate(
|
68 |
+
input_ids=input_ids,
|
69 |
+
max_length=len(formatted_text) + 500,
|
70 |
+
do_sample=True,
|
71 |
+
top_k=40,
|
72 |
+
num_beams=1,
|
73 |
+
num_return_sequences=1
|
74 |
+
)
|
75 |
+
result = tokenizer.decode(outputs[0]).replace(formatted_text, "")
|
76 |
+
st.markdown(result)
|