avreymi commited on
Commit
27145dd
1 Parent(s): dad185d

change to pipline

Browse files
Files changed (8) hide show
  1. Dockerfile +1 -1
  2. app.py +15 -32
  3. model.py +24 -0
  4. pipline.py +35 -0
  5. requirements.txt +4 -4
  6. t5.py +0 -8
  7. test.py +4 -0
  8. tests.sh +3 -0
Dockerfile CHANGED
@@ -17,5 +17,5 @@ ENV HOME=/home/user \
17
  WORKDIR $HOME/app
18
 
19
  COPY --chown=user . $HOME/app
20
-
21
  CMD ["streamlit", "run", "app.py"]
 
17
  WORKDIR $HOME/app
18
 
19
  COPY --chown=user . $HOME/app
20
+ RUN ./tests.sh
21
  CMD ["streamlit", "run", "app.py"]
app.py CHANGED
@@ -1,40 +1,23 @@
1
- from t5 import t5
2
  import torch
3
  import streamlit as st
4
- from streamlit import session_state as ses
5
- import timeit
6
- import random
7
 
8
- text_list = "Prospecting Academy is a 6 weeks online course designed for business owners, marketing and sales leaders of B2B scaleup companies to help you".split()
 
 
 
 
 
9
 
10
 
11
- def random_sentence():
12
- return " ".join(
13
- list(random.choice(text_list) for i in range(random.randint(4, 20)))
14
- )
15
 
 
 
 
16
 
17
- if "page" not in ses:
18
- ses["page"] = "run"
19
- ses.out = ""
20
- ses.time = ""
21
 
22
-
23
- def __run_t5():
24
- try:
25
- ses.out = t5(ses.input_text)
26
- except:
27
- ses.out = "error"
28
-
29
-
30
- def __test_t5():
31
- try:
32
- ses.time = timeit.timeit("t5(random_sentence())", globals=globals(), number=20)
33
- except:
34
- ses.out = "error"
35
-
36
-
37
- st.text_input("enter text", key="input_text", on_change=__run_t5)
38
- st.write(ses.out)
39
- if st.button("average time", on_click=__test_t5):
40
- st.metric("time", ses.time)
 
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 pipline
7
 
8
+ if "app" not in state:
9
+ state.app = "model"
10
+ state.out = ""
11
+ st.title("Streamlit using Huggingface Transformers and langchain")
12
+ in_area = st.container()
13
+ out_area = st.container()
14
 
15
 
 
 
 
 
16
 
17
+ def __run_pipline():
18
+ out_area.markdown(":green[Running pipline]")
19
+ out_area.text(pipline.chain_TI(state.input_text))
20
 
 
 
 
 
21
 
22
+ in_area.text_area("input_text", key="input_text")
23
+ in_area.button("run", on_click=__run_pipline)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
model.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 run(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,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import langchain as lc
2
+ from langchain import PromptTemplate, OpenAI, LLMChain
3
+ from langchain.prompts import load_prompt
4
+ import wikipedia
5
+ import os
6
+
7
+ llm = OpenAI(openai_api_key=os.environ.get("OPENAI_API_KEY"))
8
+ # save templates to a file
9
+ try_imply_template = """Question:
10
+ The user wrote me the following text, what is he trying to imply to me?
11
+ {user_input}
12
+
13
+ Answer: Let's think step by step."""
14
+ # An example prompt with multiple input variables
15
+ TI_prompt = PromptTemplate(
16
+ input_variables=["user_input"],
17
+ template=try_imply_template,
18
+ )
19
+
20
+
21
+ connection_between_terms_template = PromptTemplate(
22
+ template="""Question:
23
+ What is the connection between {term1} and {term2}?
24
+ Answer: Let's think step by step.""",
25
+ input_variables=["term1", "term2"],
26
+ )
27
+
28
+
29
+ chain_TI = LLMChain(prompt=TI_prompt, llm=llm)
30
+ chain_CC = LLMChain(prompt=connection_between_terms_template, llm=llm)
31
+
32
+
33
+ if __name__ == "__main__":
34
+ print(chain_TI.run("I am happy"))
35
+ print(chain_CC.run(["I am sad", "I am happy"]))
requirements.txt CHANGED
@@ -1,6 +1,6 @@
1
  streamlit==1.20.*
2
  requests==2.27.*
3
- sentencepiece==0.1.*
4
- torch==1.11.*
5
- transformers==4.*
6
- uvicorn[standard]==0.17.*
 
1
  streamlit==1.20.*
2
  requests==2.27.*
3
+ streamlit-ace
4
+ langchain
5
+ openai
6
+ wikipedia
t5.py DELETED
@@ -1,8 +0,0 @@
1
- from transformers import pipeline
2
-
3
- pipe_flan = pipeline(model="bigscience/bloom-560m")
4
-
5
-
6
- def t5(input):
7
- output = pipe_flan(input)
8
- return output[0]["generated_text"]
 
 
 
 
 
 
 
 
 
test.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ import subprocess
2
+ import model
3
+
4
+ print(model.model("Hello, world!, please generate some text for me."))
tests.sh ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ echo "running tests"
2
+ # python pipline.py
3
+ # python model.py