Spaces:
Sleeping
Sleeping
update all
Browse files- README.md +1 -1
- app.py +26 -14
- awesome_prompt.json +12 -0
- model.py +18 -49
- pipline.py +36 -0
- requirements.txt +3 -3
- test.py +1 -1
- tests.sh +2 -1
README.md
CHANGED
@@ -7,4 +7,4 @@ sdk: docker
|
|
7 |
app_port: 7860
|
8 |
pinned: false
|
9 |
---
|
10 |
-
|
|
|
7 |
app_port: 7860
|
8 |
pinned: false
|
9 |
---
|
10 |
+
key = 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
|
app.py
CHANGED
@@ -1,19 +1,31 @@
|
|
1 |
import subprocess
|
2 |
import torch
|
3 |
import streamlit as st
|
|
|
4 |
import streamlit_ace
|
|
|
|
|
5 |
|
6 |
-
if
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import subprocess
|
2 |
import torch
|
3 |
import streamlit as st
|
4 |
+
from streamlit import session_state as state
|
5 |
import streamlit_ace
|
6 |
+
import model
|
7 |
+
import pipline
|
8 |
|
9 |
+
if "app" not in state:
|
10 |
+
state.app = "model"
|
11 |
+
# state.input_text = "This is the input text."
|
12 |
+
# state.word = "input"
|
13 |
+
state.out = ""
|
14 |
+
|
15 |
+
st.title("Streamlit using Huggingface Transformers and langchain")
|
16 |
+
|
17 |
+
def __run_pipline():
|
18 |
+
st.text(f"input_text: {state.input_text}\nword: {state.word}")
|
19 |
+
st.markdown(":green[Running pipline]")
|
20 |
+
st.text(pipline.pipeline(state.input_text, state.word))
|
21 |
+
|
22 |
+
def __run_model():
|
23 |
+
st.text(f"input_text: {state.input_text}")
|
24 |
+
st.markdown(":green[Running model]")
|
25 |
+
st.text(model.model(state.input_text))
|
26 |
+
|
27 |
+
|
28 |
+
st.text_area("input_text", key="input_text")
|
29 |
+
st.text_input("word", key="word")
|
30 |
+
st.button("pip line", on_click=__run_pipline)
|
31 |
+
st.button("model", on_click=__run_model)
|
awesome_prompt.json
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"input_variables": [
|
3 |
+
"adjective",
|
4 |
+
"content"
|
5 |
+
],
|
6 |
+
"output_parser": null,
|
7 |
+
"partial_variables": {},
|
8 |
+
"template": "Tell me a {adjective} joke about {content}.",
|
9 |
+
"template_format": "f-string",
|
10 |
+
"validate_template": true,
|
11 |
+
"_type": "prompt"
|
12 |
+
}
|
model.py
CHANGED
@@ -1,55 +1,24 @@
|
|
1 |
-
import torch
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
-
import
|
4 |
|
5 |
|
|
|
|
|
|
|
6 |
|
7 |
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
use_auth_token=None,
|
24 |
-
)
|
25 |
-
model.eval()
|
26 |
-
model.to(device=device, dtype=torch_dtype)
|
27 |
-
if tokenizer.pad_token_id is None:
|
28 |
-
warnings.warn(
|
29 |
-
"pad_token_id is not set for the tokenizer. Using eos_token_id as pad_token_id."
|
30 |
-
)
|
31 |
-
tokenizer.pad_token = tokenizer.eos_token
|
32 |
-
tokenizer.padding_side = "left"
|
33 |
-
|
34 |
-
gkw = {
|
35 |
-
"temperature": 0.5,
|
36 |
-
"top_p": 0.92,
|
37 |
-
"top_k": 0,
|
38 |
-
"max_new_tokens": 512,
|
39 |
-
"use_cache": True,
|
40 |
-
"do_sample": True,
|
41 |
-
"eos_token_id": tokenizer.eos_token_id,
|
42 |
-
"pad_token_id": tokenizer.pad_token_id,
|
43 |
-
"repetition_penalty": 1.1, # 1.0 means no penalty, > 1.0 means penalty, 1.2 from CTRL paper
|
44 |
-
}
|
45 |
-
|
46 |
-
|
47 |
-
def mpt_7b(s):
|
48 |
-
input_ids = tokenizer(s, return_tensors="pt").input_ids
|
49 |
-
input_ids = input_ids.to(model.device)
|
50 |
-
with torch.no_grad():
|
51 |
-
output_ids = model.generate(input_ids, **gkw)
|
52 |
-
# Slice the output_ids tensor to get only new tokens
|
53 |
-
new_tokens = output_ids[0, len(input_ids[0]) :]
|
54 |
-
output_text = tokenizer.decode(new_tokens, skip_special_tokens=True)
|
55 |
-
return output_text
|
|
|
|
|
1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import torch
|
3 |
|
4 |
|
5 |
+
torch_dtype = torch.bfloat16
|
6 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
7 |
+
model_name = "bigscience/bloomz-1b7"
|
8 |
|
9 |
|
10 |
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto").to(device=device)
|
13 |
+
|
14 |
+
|
15 |
+
def model(text,**kargs):
|
16 |
+
inputs = tokenizer.encode(text=text, return_tensors="pt").to(device=device)
|
17 |
+
outputs = model.generate(inputs,**kargs)
|
18 |
+
return tokenizer.decode(outputs[0])
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
if __name__ == "__main__":
|
23 |
+
print("model test")
|
24 |
+
model("This is the input text.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pipline.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import langchain as lc
|
2 |
+
from langchain import PromptTemplate
|
3 |
+
from langchain.prompts import load_prompt
|
4 |
+
import wikipedia
|
5 |
+
from model import model
|
6 |
+
|
7 |
+
# save templates to a file
|
8 |
+
|
9 |
+
|
10 |
+
# An example prompt with multiple input variables
|
11 |
+
multiple_input_prompt = PromptTemplate(
|
12 |
+
input_variables=["adjective", "content"],
|
13 |
+
template="Tell me a {adjective} joke about {content}.",
|
14 |
+
)
|
15 |
+
multiple_input_prompt.save("awesome_prompt.json") # Save to JSON file
|
16 |
+
# multiple_input_prompt.format(adjective="funny", content="chickens")
|
17 |
+
# -> "Tell me a funny joke about chickens."
|
18 |
+
|
19 |
+
|
20 |
+
prompt = load_prompt("awesome_prompt.json")
|
21 |
+
|
22 |
+
|
23 |
+
def pipeline(text, word):
|
24 |
+
model_output = ""
|
25 |
+
input_text = prompt.format(adjective="funny", content=text)
|
26 |
+
while word not in model_output:
|
27 |
+
model_output = model(input_text)
|
28 |
+
wikipedia_entry = wikipedia.search(word)[1]
|
29 |
+
wiki = wikipedia.summary(wikipedia_entry, auto_suggest=False, redirect=True)
|
30 |
+
input_text += model_output + wiki
|
31 |
+
return model_output
|
32 |
+
|
33 |
+
|
34 |
+
if __name__ == "__main__":
|
35 |
+
print("pipline test")
|
36 |
+
pipeline("This is the input text.", "input")
|
requirements.txt
CHANGED
@@ -3,7 +3,7 @@ requests==2.27.*
|
|
3 |
sentencepiece==0.1.*
|
4 |
torch==2.*
|
5 |
transformers==4.*
|
6 |
-
uvicorn[standard]==0.17.*
|
7 |
streamlit-ace
|
8 |
-
|
9 |
-
|
|
|
|
3 |
sentencepiece==0.1.*
|
4 |
torch==2.*
|
5 |
transformers==4.*
|
|
|
6 |
streamlit-ace
|
7 |
+
langchain
|
8 |
+
openai
|
9 |
+
accelerate
|
test.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
import subprocess
|
2 |
import model
|
3 |
|
4 |
-
print(model.
|
|
|
1 |
import subprocess
|
2 |
import model
|
3 |
|
4 |
+
print(model.model("Hello, world!, please generate some text for me."))
|
tests.sh
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
echo "running tests"
|
2 |
-
python
|
|
|
|
1 |
echo "running tests"
|
2 |
+
# python pipline.py
|
3 |
+
# python model.py
|