dipta007 commited on
Commit
ebab1a2
β€’
1 Parent(s): 464c719

added chat, langchat

Browse files
Files changed (5) hide show
  1. app.py +34 -16
  2. explore_1.py +37 -0
  3. explore_2.py +43 -0
  4. langchat.py +141 -0
  5. utils.py +0 -2
app.py CHANGED
@@ -1,20 +1,30 @@
 
 
 
 
 
1
  # import torch
2
  import pickle
3
- import streamlit as st
4
- from transformers import Conversation, pipeline
5
  from upload import get_file, upload_file
6
  from utils import clear_uploader, undo, restart
7
 
8
 
 
 
9
  share_keys = ["messages", "model_name"]
10
  MODELS = [
11
- "google/flan-t5-small",
12
- "google/flan-t5-base",
13
- "google/flan-t5-large",
14
- "google/flan-t5-xl",
15
- "google/flan-t5-xxl",
 
 
16
  ]
17
- default_model = "google/flan-t5-small"
 
18
 
19
  st.set_page_config(
20
  page_title="LLM",
@@ -25,10 +35,18 @@ if "model_name" not in st.session_state:
25
  st.session_state.model_name = default_model
26
 
27
 
 
28
  def get_pipeline(model_name):
29
- # device = 0 if torch.cuda.is_available() else -1
30
- device = -1
31
- chatbot = pipeline(model=model_name, task="conversational", device=device)
 
 
 
 
 
 
 
32
  return chatbot
33
 
34
  chatbot = get_pipeline(st.session_state.model_name)
@@ -60,7 +78,7 @@ with st.sidebar:
60
  st.title(":blue[LLM Only]")
61
 
62
  st.subheader("Model")
63
- model_name = st.selectbox("Model", MODELS, index=MODELS.index(st.session_state.model_name))
64
 
65
  if st.button("Share", use_container_width=True):
66
  share()
@@ -94,12 +112,12 @@ if prompt := st.chat_input("Type a message", key="chat_input"):
94
 
95
  if not append:
96
  with st.chat_message("assistant"):
97
- conversation = Conversation()
98
  for m in st.session_state.messages:
99
- conversation.add_message(m)
100
- print(conversation)
101
  with st.spinner("Generating response..."):
102
- response = chatbot(conversation)
103
  response = response[-1]["content"]
104
  st.write(response)
105
 
 
1
+
2
+ import streamlit as st
3
+ import os
4
+ os.environ['HF_HOME'] = '/scratch/sroydip1/cache/hf/'
5
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
6
  # import torch
7
  import pickle
8
+ import torch
9
+ from transformers import Conversation, pipeline, AutoTokenizer, AutoModelForCausalLM
10
  from upload import get_file, upload_file
11
  from utils import clear_uploader, undo, restart
12
 
13
 
14
+ TOKEN = st.secrets["HF_TOKEN"]
15
+
16
  share_keys = ["messages", "model_name"]
17
  MODELS = [
18
+ "meta-llama/Llama-2-7b-chat-hf",
19
+ "mistralai/Mistral-7B-Instruct-v0.2",
20
+ # "google/flan-t5-small",
21
+ # "google/flan-t5-base",
22
+ # "google/flan-t5-large",
23
+ # "google/flan-t5-xl",
24
+ # "google/flan-t5-xxl",
25
  ]
26
+ default_model = MODELS[0]
27
+ # default_model = "meta-llama/Llama-2-7b-chat-hf"
28
 
29
  st.set_page_config(
30
  page_title="LLM",
 
35
  st.session_state.model_name = default_model
36
 
37
 
38
+ @st.cache_resource
39
  def get_pipeline(model_name):
40
+ device = 0 if torch.cuda.is_available() else -1
41
+ # if True or model_name == "meta-llama/Llama-2-7b-chat-hf" or model_name == "mistralai/Mistral-7B-Instruct-v0.2":
42
+ # chatbot = pipeline(model=model_name, task="conversational", device=device)#, model_kwargs=model_kwargs)
43
+ # else:
44
+ # chatbot = pipeline(model=model_name, task="text-generation", device=device)
45
+
46
+ tokenizer = AutoTokenizer.from_pretrained(model_name, token=TOKEN)
47
+ model = AutoModelForCausalLM.from_pretrained(model_name, token=TOKEN, load_in_8bit=True)
48
+ # chatbot = pipeline("conversational", model=model, tokenizer=tokenizer, device=device)
49
+ chatbot = pipeline("conversational", model=model, tokenizer=tokenizer)
50
  return chatbot
51
 
52
  chatbot = get_pipeline(st.session_state.model_name)
 
78
  st.title(":blue[LLM Only]")
79
 
80
  st.subheader("Model")
81
+ model_name = st.selectbox("Model", MODELS, key="model_name")
82
 
83
  if st.button("Share", use_container_width=True):
84
  share()
 
112
 
113
  if not append:
114
  with st.chat_message("assistant"):
115
+ chat = Conversation()
116
  for m in st.session_state.messages:
117
+ chat.add_message(m)
118
+ print(chat)
119
  with st.spinner("Generating response..."):
120
+ response = chatbot(chat)
121
  response = response[-1]["content"]
122
  st.write(response)
123
 
explore_1.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = ""
3
+
4
+ from langchain_community.llms import HuggingFaceHub
5
+
6
+ llm = HuggingFaceHub(
7
+ repo_id="meta-llama/Llama-2-7b-chat-hf",
8
+ task="text-generation",
9
+ model_kwargs={
10
+ "max_new_tokens": 512,
11
+ "temperature": 0.1,
12
+ "seed": 42,
13
+ },
14
+ )
15
+
16
+ from langchain.schema import (
17
+ HumanMessage,
18
+ SystemMessage,
19
+ AIMessage,
20
+ )
21
+ from langchain_community.chat_models.huggingface import ChatHuggingFace
22
+
23
+ messages = [
24
+ SystemMessage(content="You're a helpful assistant"),
25
+ ]
26
+
27
+ chat_model = ChatHuggingFace(llm=llm)
28
+
29
+
30
+ while True:
31
+ question = input("You: ")
32
+ messages.append(HumanMessage(content=question))
33
+ response = chat_model.invoke(messages)
34
+ print(response)
35
+ response = response.content
36
+ messages.append(AIMessage(content=response))
37
+ print(f"Bot: {response}")
explore_2.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = ""
3
+
4
+ from langchain.prompts import PromptTemplate
5
+ from langchain.chains import LLMChain
6
+ from langchain.memory import ConversationBufferMemory
7
+ from langchain_community.llms import HuggingFaceHub
8
+
9
+ template = """You are a friendly chatbot engaging in a conversation with a human.
10
+
11
+ Previous conversation:
12
+ {chat_history}
13
+
14
+ New human question: {question}
15
+ Response:"""
16
+
17
+ def get_pipeline(model_name):
18
+ llm = HuggingFaceHub(
19
+ repo_id=model_name,
20
+ task="text-generation",
21
+ model_kwargs={
22
+ "max_new_tokens": 250,
23
+ "top_k": 30,
24
+ "temperature": 0.1,
25
+ "repetition_penalty": 1.03,
26
+ },
27
+ )
28
+ return llm
29
+
30
+
31
+ chatbot = get_pipeline("mistralai/Mistral-7B-Instruct-v0.2")
32
+ memory = ConversationBufferMemory(memory_key="chat_history")
33
+ prompt_template = PromptTemplate.from_template(template)
34
+ conversation = LLMChain(llm=chatbot, prompt=prompt_template, verbose=True, memory=memory)
35
+
36
+ while True:
37
+ question = input("You: ")
38
+ response = conversation({"question": question})
39
+ print("-" * 50)
40
+ print(response)
41
+ print(response["text"])
42
+ print("-" * 50)
43
+ print()
langchat.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ os.environ["HF_HOME"] = "/scratch/sroydip1/cache/hf/"
4
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = ""
5
+ # import torch
6
+ import pickle
7
+ import torch
8
+ import streamlit as st
9
+ from transformers import Conversation, pipeline
10
+ from upload import get_file, upload_file
11
+ from utils import clear_uploader, undo, restart
12
+
13
+ from langchain.prompts import PromptTemplate
14
+ from langchain.chains import LLMChain
15
+ from langchain.memory import ConversationBufferMemory
16
+ from langchain_community.llms import HuggingFaceHub
17
+
18
+
19
+ share_keys = ["messages", "model_name"]
20
+ MODELS = [
21
+ "mistralai/Mistral-7B-Instruct-v0.2",
22
+ "google/flan-t5-small",
23
+ "google/flan-t5-base",
24
+ "google/flan-t5-large",
25
+ "google/flan-t5-xl",
26
+ "google/flan-t5-xxl",
27
+ ]
28
+ default_model = "mistralai/Mistral-7B-Instruct-v0.2"
29
+ # default_model = "meta-llama/Llama-2-7b-chat-hf"
30
+
31
+ st.set_page_config(
32
+ page_title="LLM",
33
+ page_icon="πŸ“š",
34
+ )
35
+
36
+ if "model_name" not in st.session_state:
37
+ st.session_state.model_name = default_model
38
+
39
+ template = """You are a friendly chatbot engaging in a conversation with a human.
40
+
41
+ Previous conversation:
42
+ {chat_history}
43
+
44
+ New human question: {question}
45
+ Response:"""
46
+
47
+
48
+ def get_pipeline(model_name):
49
+ llm = HuggingFaceHub(
50
+ repo_id=model_name,
51
+ task="text-generation",
52
+ model_kwargs={
53
+ "max_new_tokens": 512,
54
+ "top_k": 30,
55
+ "temperature": 0.1,
56
+ "repetition_penalty": 1.03,
57
+ },
58
+ )
59
+ return llm
60
+
61
+
62
+ chatbot = get_pipeline(st.session_state.model_name)
63
+ memory = ConversationBufferMemory(memory_key="chat_history")
64
+ prompt_template = PromptTemplate.from_template(template)
65
+ conversation = LLMChain(llm=chatbot, prompt=prompt_template, verbose=True, memory=memory)
66
+
67
+
68
+ if "messages" not in st.session_state:
69
+ st.session_state.messages = []
70
+
71
+ if len(st.session_state.messages) == 0 and "id" in st.query_params:
72
+ with st.spinner("Loading chat..."):
73
+ id = st.query_params["id"]
74
+ data = get_file(id)
75
+ obj = pickle.loads(data)
76
+ for k, v in obj.items():
77
+ st.session_state[k] = v
78
+
79
+
80
+ def share():
81
+ obj = {}
82
+ for k in share_keys:
83
+ if k in st.session_state:
84
+ obj[k] = st.session_state[k]
85
+ data = pickle.dumps(obj)
86
+ id = upload_file(data)
87
+ url = f"https://umbc-nlp-chat-llm.hf.space/?id={id}"
88
+ st.markdown(f"[share](/?id={id})")
89
+ st.success(f"Share URL: {url}")
90
+
91
+
92
+ with st.sidebar:
93
+ st.title(":blue[LLM Only]")
94
+
95
+ st.subheader("Model")
96
+ model_name = st.selectbox(
97
+ "Model", MODELS, index=MODELS.index(st.session_state.model_name)
98
+ )
99
+
100
+ if st.button("Share", use_container_width=True):
101
+ share()
102
+
103
+ cols = st.columns(2)
104
+ with cols[0]:
105
+ if st.button("Restart", type="primary", use_container_width=True):
106
+ restart()
107
+
108
+ with cols[1]:
109
+ if st.button("Undo", use_container_width=True):
110
+ undo()
111
+
112
+ append = st.checkbox("Append to previous message", value=False)
113
+
114
+
115
+ for message in st.session_state.messages:
116
+ with st.chat_message(message["role"]):
117
+ st.markdown(message["content"])
118
+
119
+
120
+ def push_message(role, content):
121
+ message = {"role": role, "content": content}
122
+ st.session_state.messages.append(message)
123
+ return message
124
+
125
+
126
+ if prompt := st.chat_input("Type a message", key="chat_input"):
127
+ push_message("user", prompt)
128
+ with st.chat_message("user"):
129
+ st.markdown(prompt)
130
+
131
+ if not append:
132
+ with st.chat_message("assistant"):
133
+ print(conversation)
134
+ with st.spinner("Generating response..."):
135
+ response = conversation({"question": prompt})
136
+ print(response)
137
+ response = response["text"]
138
+ st.write(response)
139
+
140
+ push_message("assistant", response)
141
+ clear_uploader()
utils.py CHANGED
@@ -9,8 +9,6 @@ def undo():
9
  if len(st.session_state.messages) > 0:
10
  st.query_params.clear()
11
  msg = st.session_state.messages.pop()
12
- if msg["role"] == "assistant" and "cost" in st.session_state:
13
- st.session_state.cost.pop()
14
  time.sleep(0.1)
15
  st.rerun()
16
 
 
9
  if len(st.session_state.messages) > 0:
10
  st.query_params.clear()
11
  msg = st.session_state.messages.pop()
 
 
12
  time.sleep(0.1)
13
  st.rerun()
14