Samuel L Meyers commited on
Commit
f96f74e
·
1 Parent(s): ec15998

Running test1

Browse files
Files changed (6) hide show
  1. .gitignore +1 -0
  2. Dockerfile +27 -0
  3. code/app.py +61 -0
  4. code/requirements.txt +10 -0
  5. conversation.py +223 -0
  6. setup.cfg +12 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ WORKDIR /code
7
+
8
+ RUN useradd -m -u 1000 user
9
+
10
+ USER user
11
+
12
+ ENV HOME=/home/user
13
+ ENV PATH=/home/user/.local/bin:$PATH
14
+
15
+ WORKDIR $HOME/app
16
+
17
+ COPY --chown=user /code $HOME/app
18
+
19
+ ENV VIRTUAL_ENV $HOME/app/.venv
20
+
21
+ RUN python -m venv $VIRTUAL_ENV
22
+
23
+ RUN pip install --no-cache-dir --upgrade -r $HOME/app/requirements.txt
24
+
25
+ COPY . .
26
+
27
+ CMD ["python", "app.py"]
code/app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ from typing import cast
3
+ from threading import Lock
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
5
+
6
+ import torch
7
+
8
+ from conversation import get_default_conv_template
9
+ import gradio as gr
10
+ from llama_cpp import Llama
11
+ import json
12
+
13
+ from huggingface_hub import hf_hub_download
14
+
15
+ model_path = "starling-lm-7b-alpha.Q6_K.gguf"
16
+
17
+ #mdlpath = hf_hub_download(repo_id="afrideva/MiniChat-3B-GGUF", filename=model_path)
18
+
19
+ lcpp_model = Llama(model_path=model_path)
20
+
21
+ global otxt
22
+ otxt = ""
23
+
24
+ def m3b_talk(text):
25
+ global otxt
26
+ resp = ""
27
+ formattedQuery = "GPT4 User: " + text + "<|end_of_text|>GPT4 Assistant:"
28
+ r = lcpp_model(formattedQuery, stop=["GPT4 User:", "\n\n\n"], echo=True, stream=True)
29
+ rfq = False
30
+ for c in r:
31
+ otxt += c["choices"][0]["text"]
32
+ if formattedQuery in otxt and not rfq:
33
+ otxt.replace(formattedQuery, "")
34
+ rfq = True
35
+ else:
36
+ yield otxt
37
+ print(resp)
38
+ return otxt
39
+ #return resp.replace(formattedQuery, "")
40
+
41
+ def main():
42
+ global otxt
43
+ logging.basicConfig(level=logging.INFO)
44
+
45
+ with gr.Blocks() as demo:
46
+ with gr.Row(variant="panel"):
47
+ gr.Markdown("## Talk to MiniChat-3B\n\nTalk to MiniChat-3B.")
48
+ with gr.Row(variant="panel"):
49
+ with gr.Column(variant="panel"):
50
+ m3b_talk_input = gr.Textbox(label="Message", placeholder="Type something here...")
51
+ with gr.Column(variant="panel"):
52
+ m3b_talk_output = gr.Textbox()
53
+ m3b_talk_btn = gr.Button("Send")
54
+
55
+ m3b_talk_btn.click(m3b_talk, inputs=m3b_talk_input, outputs=m3b_talk_output, api_name="talk_m3b")
56
+
57
+ demo.queue().launch()
58
+
59
+
60
+ if __name__ == "__main__":
61
+ main()
code/requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://pypi.org/simple/
2
+ huggingface_hub
3
+ numpy
4
+ httpx==0.24.1
5
+ git+https://github.com/huggingface/transformers
6
+ torch
7
+ sentencepiece
8
+ accelerate
9
+ llama-cpp-python>=0.2.18
10
+ wget
conversation.py ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Conversation prompt templates.
3
+ """
4
+
5
+ import dataclasses
6
+ from enum import auto, Enum
7
+ from typing import List, Tuple, Any
8
+
9
+
10
+ class SeparatorStyle(Enum):
11
+ """Different separator style."""
12
+
13
+ ADD_COLON_SINGLE = auto()
14
+ ADD_COLON_TWO = auto()
15
+ NO_COLON_SINGLE = auto()
16
+ BAIZE = auto()
17
+ PHOENIX = auto()
18
+ MINICHAT = auto()
19
+
20
+
21
+ @dataclasses.dataclass
22
+ class Conversation:
23
+ """A class that keeps all conversation history."""
24
+
25
+ # System prompts
26
+ system: str
27
+ # Two roles
28
+ roles: List[str]
29
+ # All messages
30
+ messages: List[List[str]]
31
+ # Offset of few shot examples
32
+ offset: int
33
+ # Separator
34
+ sep_style: SeparatorStyle
35
+ sep: str
36
+ sep2: str = None
37
+ # Stop criteria (the default one is EOS token)
38
+ stop_str: str = None
39
+ # Stops generation if meeting any token in this list
40
+ stop_token_ids: List[int] = None
41
+
42
+ # Used for the state in the gradio servers.
43
+ # TODO(lmzheng): refactor this
44
+ conv_id: Any = None
45
+ skip_next: bool = False
46
+ model_name: str = None
47
+
48
+ def get_prompt(self):
49
+ if self.sep_style == SeparatorStyle.ADD_COLON_SINGLE:
50
+ ret = self.system + self.sep
51
+ for role, message in self.messages:
52
+ if message:
53
+ ret += role + ": " + message + self.sep
54
+ else:
55
+ ret += role + ": "
56
+ return ret
57
+ elif self.sep_style == SeparatorStyle.ADD_COLON_TWO:
58
+ seps = [self.sep, self.sep2]
59
+ ret = self.system + seps[0]
60
+ for i, (role, message) in enumerate(self.messages):
61
+ if message:
62
+ ret += role + ": " + message + seps[i % 2]
63
+ else:
64
+ ret += role + ": "
65
+ return ret
66
+ elif self.sep_style == SeparatorStyle.NO_COLON_SINGLE:
67
+ ret = self.system
68
+ for role, message in self.messages:
69
+ if message:
70
+ ret += role + message + self.sep
71
+ else:
72
+ ret += role
73
+ return ret
74
+ elif self.sep_style == SeparatorStyle.BAIZE:
75
+ ret = self.system + "\n"
76
+ for role, message in self.messages:
77
+ if message:
78
+ ret += role + message + "\n"
79
+ else:
80
+ ret += role
81
+ return ret
82
+ elif self.sep_style == SeparatorStyle.PHOENIX:
83
+ ret = self.system
84
+ for role, message in self.messages:
85
+ if message:
86
+ ret += role + ": " + "<s>" + message + "</s>"
87
+ else:
88
+ ret += role + ": " + "<s>"
89
+ return ret
90
+ elif self.sep_style == SeparatorStyle.MINICHAT:
91
+ ret = self.system
92
+ for role, message in self.messages:
93
+ if message:
94
+ ret += role + " " + message + "</s>"
95
+ else:
96
+ ret += role # No space is needed.
97
+ return ret
98
+ else:
99
+ raise ValueError(f"Invalid style: {self.sep_style}")
100
+
101
+ def append_message(self, role, message):
102
+ self.messages.append([role, message])
103
+
104
+ def to_gradio_chatbot(self):
105
+ ret = []
106
+ for i, (role, msg) in enumerate(self.messages[self.offset:]):
107
+ if i % 2 == 0:
108
+ ret.append([msg, None])
109
+ else:
110
+ ret[-1][-1] = msg
111
+ return ret
112
+
113
+ def to_openai_api_messages(self):
114
+ ret = [{"role": "system", "content": self.system}]
115
+
116
+ for i, (_, msg) in enumerate(self.messages[self.offset:]):
117
+ if i % 2 == 0:
118
+ ret.append({"role": "user", "content": msg})
119
+ else:
120
+ if msg is not None:
121
+ ret.append({"role": "assistant", "content": msg})
122
+ return ret
123
+
124
+ def copy(self):
125
+ return Conversation(
126
+ system=self.system,
127
+ roles=self.roles,
128
+ messages=[[x, y] for x, y in self.messages],
129
+ offset=self.offset,
130
+ sep_style=self.sep_style,
131
+ sep=self.sep,
132
+ sep2=self.sep2,
133
+ stop_str=self.stop_str,
134
+ stop_token_ids=self.stop_token_ids,
135
+ conv_id=self.conv_id,
136
+ model_name=self.model_name,
137
+ )
138
+
139
+ def dict(self):
140
+ return {
141
+ "system": self.system,
142
+ "roles": self.roles,
143
+ "messages": self.messages,
144
+ "offset": self.offset,
145
+ "conv_id": self.conv_id,
146
+ "model_name": self.model_name,
147
+ }
148
+
149
+
150
+ conv_vicuna = Conversation(
151
+ system="A chat between a curious user and an artificial intelligence assistant. "
152
+ "The assistant gives helpful, detailed, and polite answers to the user's questions.",
153
+ roles=("USER", "ASSISTANT"),
154
+ messages=(),
155
+ offset=0,
156
+ sep_style=SeparatorStyle.ADD_COLON_TWO,
157
+ sep=" ",
158
+ sep2="</s>",
159
+ )
160
+
161
+ conv_baize = Conversation(
162
+ system="The following is a conversation between a human and an AI assistant named Baize (named after a mythical creature in Chinese folklore). Baize is an open-source AI assistant developed by UCSD and Sun Yat-Sen University. The human and the AI assistant take turns chatting. Human statements start with [|Human|] and AI assistant statements start with [|AI|]. The AI assistant always provides responses in as much detail as possible, and in Markdown format. The AI assistant always declines to engage with topics, questions and instructions related to unethical, controversial, or sensitive issues. Complete the transcript in exactly that format.\n",
163
+ roles=("[|Human|]", "[|AI|]"),
164
+ messages=(
165
+ ("[|Human|]", "Hello!"),
166
+ ("[|AI|]", "Hi!"),
167
+ ),
168
+ offset=2,
169
+ sep_style=SeparatorStyle.BAIZE,
170
+ sep="\n",
171
+ stop_str="[|Human|]",
172
+ )
173
+
174
+ conv_phoenix = Conversation(
175
+ system="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.\n\n",
176
+ roles=("Human", "Assistant"),
177
+ messages=(),
178
+ offset=0,
179
+ sep_style=SeparatorStyle.PHOENIX,
180
+ sep="</s>",
181
+ )
182
+
183
+ conv_chatgpt = Conversation(
184
+ system="You are a helpful assistant.",
185
+ roles=("user", "assistant"),
186
+ messages=(),
187
+ offset=0,
188
+ sep_style=None,
189
+ sep=None,
190
+ )
191
+
192
+ conv_minichat = Conversation(
193
+ system="‘MiniChat’是一个由‘Beccurio’开发的AI语言模型。下面是人类和MiniChat之间的一段对话。MiniChat的回复应当尽可能详细,并且以Markdown的形式输出。MiniChat应当拒绝参与违背伦理的讨论。</s>",
194
+ roles=("[|User|]", "[|Assistant|]"),
195
+ messages=(),
196
+ offset=0,
197
+ sep_style=SeparatorStyle.MINICHAT,
198
+ sep="</s>",
199
+ )
200
+
201
+
202
+ conv_templates = {
203
+ "vicuna": conv_vicuna,
204
+ "baize": conv_baize,
205
+ "phoenix": conv_phoenix,
206
+ "chatgpt": conv_chatgpt,
207
+ "minichat": conv_minichat,
208
+ }
209
+
210
+ def get_default_conv_template(model_name):
211
+ model_name = model_name.lower()
212
+ try:
213
+ ret = conv_templates[model_name]
214
+ return ret.copy()
215
+ except:
216
+ raise NotImplementedError(f"No support for model {model_name}.")
217
+
218
+
219
+ if __name__ == "__main__":
220
+ conv = conv_templates["minichat"].copy()
221
+ conv.append_message(conv.roles[0], "Write a Python function that checks if a given number is even or odd.")
222
+ conv.append_message(conv.roles[1], None)
223
+ print([conv.get_prompt()])
setup.cfg ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [flake8]
2
+ max_complexity=10
3
+ per-file-ignores=__init__.py:F401,F403
4
+ ignore = E203,W503
5
+ max-line-length=119
6
+
7
+ [isort]
8
+ profile=black
9
+ line_length=119
10
+
11
+ [mypy]
12
+ ignore_missing_imports = True