Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#streamlit run app.py
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
import torch
|
5 |
+
import galai as gal
|
6 |
+
|
7 |
+
#https://github.com/paperswithcode/galai/blob/main/notebooks/Introduction%20to%20Galactica%20Models.ipynb
|
8 |
+
|
9 |
+
#@st.cache(suppress_st_warning=True, allow_output_mutation=True)
|
10 |
+
@st.cache_resource
|
11 |
+
def load_model(model_name):
|
12 |
+
model = gal.load_model(model_name, dtype=torch.float16)
|
13 |
+
return model
|
14 |
+
|
15 |
+
if 'text' not in st.session_state:
|
16 |
+
st.session_state['text'] = ""
|
17 |
+
|
18 |
+
def generate_text():
|
19 |
+
|
20 |
+
st.session_state['text'] = model.generate(input_text=st.session_state.editor, penalty_alpha=penalty_alpha, top_k=top_k, max_new_tokens=max_new_tokens, new_doc=new_doc)
|
21 |
+
|
22 |
+
def suggest_reference():
|
23 |
+
|
24 |
+
st.session_state['text'] = st.session_state.editor + " " + model.generate_reference(input_text=st.session_state.editor)
|
25 |
+
|
26 |
+
#Sidebar
|
27 |
+
st.sidebar.markdown("### Select model")
|
28 |
+
|
29 |
+
choose_model = st.sidebar.selectbox("Size", ["mini", "base"])
|
30 |
+
|
31 |
+
model = load_model(choose_model)
|
32 |
+
|
33 |
+
st.sidebar.markdown("### Text generation settings")
|
34 |
+
|
35 |
+
max_new_tokens = st.sidebar.slider("Max new tokens", value=60, min_value = 10, max_value = 200, step=10)
|
36 |
+
penalty_alpha = st.sidebar.slider("Alpha penalty", value = 0.6, min_value = 0.0, max_value=2.0, step=0.1)
|
37 |
+
top_k = st.sidebar.slider("Top-k", min_value = 0, max_value=10, value = 4)
|
38 |
+
new_doc = st.sidebar.checkbox("New document", value=True)
|
39 |
+
|
40 |
+
#Main
|
41 |
+
st.markdown(
|
42 |
+
'''
|
43 |
+
# Scientific writing assistant
|
44 |
+
|
45 |
+
## GALACTICA model
|
46 |
+
|
47 |
+
The [GALACTICA models](https://www.galactica.org) have been training on a large corpus of scientific data (see also the [GitHub repository](https://github.com/paperswithcode/galai)). Try out the two smaller models here and how they can be used to generate scientific text and suggest references.
|
48 |
+
|
49 |
+
Write text in the editor and push **TAB** or **CTRL+ENTER** to generate text.
|
50 |
+
|
51 |
+
Settings for model size (mini = 125 M and base = 1.3 B parameters) and text generation can be managed using from the left margin.
|
52 |
+
|
53 |
+
## Suggest citations
|
54 |
+
|
55 |
+
Use **Add citation** button to suggest and insert a citation into the text editor. The citation format is *Title, First author*.
|
56 |
+
''')
|
57 |
+
|
58 |
+
add_ref = st.button("Add citation", on_click=suggest_reference)
|
59 |
+
|
60 |
+
text_editor = st.text_area("(not shown)", st.session_state['text'], height = 500, key="editor", on_change=generate_text, label_visibility="collapsed")
|