Spaces:
Runtime error
Runtime error
push to hfspace
Browse files- .DS_Store +0 -0
- .gitignore +2 -0
- .streamlit/config.toml +2 -0
- install_env.sh +5 -0
- main.py +58 -0
- requirements.txt +6 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# environment
|
| 2 |
+
bloom_demo
|
.streamlit/config.toml
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[browser]
|
| 2 |
+
gatherUsageStats = false
|
install_env.sh
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!sh
|
| 2 |
+
conda create -p bloom_demo python=3.8
|
| 3 |
+
source activate ./bloom_demo
|
| 4 |
+
pip install -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cu111
|
| 5 |
+
# streamlit run main.py
|
main.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ------------------- LIBRARIES -------------------- #
|
| 2 |
+
import os, logging, torch, streamlit as st
|
| 3 |
+
from transformers import (
|
| 4 |
+
AutoTokenizer, AutoModelForCausalLM)
|
| 5 |
+
|
| 6 |
+
# --------------------- HELPER --------------------- #
|
| 7 |
+
def C(text, color="yellow"):
|
| 8 |
+
color_dict: dict = dict(
|
| 9 |
+
red="\033[01;31m",
|
| 10 |
+
green="\033[01;32m",
|
| 11 |
+
yellow="\033[01;33m",
|
| 12 |
+
blue="\033[01;34m",
|
| 13 |
+
magenta="\033[01;35m",
|
| 14 |
+
cyan="\033[01;36m",
|
| 15 |
+
)
|
| 16 |
+
color_dict[None] = "\033[0m"
|
| 17 |
+
return (
|
| 18 |
+
f"{color_dict.get(color, None)}"
|
| 19 |
+
f"{text}{color_dict[None]}")
|
| 20 |
+
|
| 21 |
+
# ------------------ ENVIORNMENT ------------------- #
|
| 22 |
+
os.environ["HF_ENDPOINT"] = "https://huggingface.co"
|
| 23 |
+
device = ("cuda"
|
| 24 |
+
if torch.cuda.is_available() else "cpu")
|
| 25 |
+
logging.info(C("[INFO] "f"device = {device}"))
|
| 26 |
+
|
| 27 |
+
# ------------------ INITITALIZE ------------------- #
|
| 28 |
+
@st.cache_resource
|
| 29 |
+
def model_init():
|
| 30 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 31 |
+
"ckip-joint/bloom-1b1-zh")
|
| 32 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 33 |
+
"ckip-joint/bloom-1b1-zh",
|
| 34 |
+
# Ref.: Eric, Thanks!
|
| 35 |
+
# torch_dtype="auto",
|
| 36 |
+
# device_map="auto",
|
| 37 |
+
# Ref. for `half`: Chan-Jan, Thanks!
|
| 38 |
+
).eval().to(device)
|
| 39 |
+
st.balloons()
|
| 40 |
+
logging.info(C("[INFO] "f"Model init success!"))
|
| 41 |
+
return tokenizer, model
|
| 42 |
+
|
| 43 |
+
tokenizer, model = model_init()
|
| 44 |
+
|
| 45 |
+
# ===================== INPUT ====================== #
|
| 46 |
+
# prompt = "\u554F\uFF1A\u53F0\u7063\u6700\u9AD8\u7684\u5EFA\u7BC9\u7269\u662F\uFF1F\u7B54\uFF1A" #@param {type:"string"}
|
| 47 |
+
prompt = st.text_input("Prompt: ")
|
| 48 |
+
|
| 49 |
+
# =================== INFERENCE ==================== #
|
| 50 |
+
if prompt:
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
[texts_out] = model.generate(
|
| 53 |
+
**tokenizer(
|
| 54 |
+
prompt, return_tensors="pt"
|
| 55 |
+
).to(device))
|
| 56 |
+
output_text = tokenizer.decode(texts_out)
|
| 57 |
+
st.markdown(output_text)
|
| 58 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==1.10.2+cu111
|
| 2 |
+
transformers
|
| 3 |
+
streamlit
|
| 4 |
+
|
| 5 |
+
# not really
|
| 6 |
+
ipython
|