Spaces:
Sleeping
Sleeping
init
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import (
|
| 3 |
+
AutoModelForCausalLM,
|
| 4 |
+
AutoModelForSeq2SeqLM,
|
| 5 |
+
AutoTokenizer,
|
| 6 |
+
GenerationConfig,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_tokenizer_model(name_or_path: str, model_type: str, model_auth_token: str):
|
| 11 |
+
model_auth_token = None if model_auth_token == "" else model_auth_token
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(*name_or_path.split(","), use_auth_token=model_auth_token)
|
| 13 |
+
if model_type == "seq2seq":
|
| 14 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(*name_or_path.split(","), use_auth_token=model_auth_token)
|
| 15 |
+
elif model_type == "causal":
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(*name_or_path.split(","), use_auth_token=model_auth_token)
|
| 17 |
+
else:
|
| 18 |
+
raise ValueError("model_type must be one of 'seq2seq' or 'causal'")
|
| 19 |
+
|
| 20 |
+
return tokenizer, model
|
| 21 |
+
|
| 22 |
+
def main():
|
| 23 |
+
st.title("Huggingface Transformers Demo")
|
| 24 |
+
with st.form("model_form"):
|
| 25 |
+
model_type = st.selectbox("Select Model Type", ["seq2seq", "causal"])
|
| 26 |
+
model_name_or_path = st.text_input("Model Name or Path")
|
| 27 |
+
model_auth_token = st.text_input("Model Auth Token")
|
| 28 |
+
input_text = st.text_area("Input Text")
|
| 29 |
+
|
| 30 |
+
col1, col2, col3 = st.columns(3)
|
| 31 |
+
user_gen_config = {}
|
| 32 |
+
with col1:
|
| 33 |
+
user_gen_config["min_length"] = st.number_input("Min Length", value=10, min_value=1, max_value=1000, step=1)
|
| 34 |
+
user_gen_config["max_length"] = st.number_input("Max Length", value=50, min_value=1, max_value=1000, step=1)
|
| 35 |
+
user_gen_config["top_k"] = st.number_input("Top K", value=50, min_value=1, max_value=100, step=1)
|
| 36 |
+
|
| 37 |
+
with col2:
|
| 38 |
+
user_gen_config["num_beams"] = st.number_input("Num Beams", value=1, min_value=1, max_value=100, step=1)
|
| 39 |
+
user_gen_config["top_p"] = st.number_input("Top P", value=1.0, min_value=0.0, max_value=100.0, step=0.1)
|
| 40 |
+
user_gen_config["repetition_penalty"] = st.number_input("Repetition Penalty", value=1.0, min_value=0.0,
|
| 41 |
+
max_value=100.0, step=0.1)
|
| 42 |
+
|
| 43 |
+
with col3:
|
| 44 |
+
user_gen_config["temperature"] = st.number_input("Temperature", value=1.0, min_value=0.0, max_value=100.0, step=0.1)
|
| 45 |
+
user_gen_config["do_sample"] = st.checkbox("Do Sample", value=False)
|
| 46 |
+
user_gen_config["early_stopping"] = st.checkbox("Early Stopping", value=True)
|
| 47 |
+
|
| 48 |
+
submitted = st.form_submit_button("Submit")
|
| 49 |
+
|
| 50 |
+
if submitted:
|
| 51 |
+
tokenizer, model = load_tokenizer_model(model_name_or_path, model_type, model_auth_token)
|
| 52 |
+
gen_config = GenerationConfig.from_model_config(model.config)
|
| 53 |
+
for k,v in user_gen_config.items():
|
| 54 |
+
setattr(gen_config, k, v)
|
| 55 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
| 56 |
+
output_ids = model.generate(input_ids, generation_config=gen_config)
|
| 57 |
+
output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 58 |
+
st.write(output_text)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
main()
|