Spaces:
Runtime error
Runtime error
add StarChat demo
Browse files- .gitattributes +1 -0
- .gitignore +160 -0
- README.md +2 -2
- StarCoderBanner.png +3 -0
- app.py +405 -0
- requirements.txt +2 -0
- share_btn.py +111 -0
.gitattributes
CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
StarCoderBanner.png filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
.env
|
124 |
+
.venv
|
125 |
+
env/
|
126 |
+
venv/
|
127 |
+
ENV/
|
128 |
+
env.bak/
|
129 |
+
venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
#.idea/
|
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
colorFrom: pink
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
|
|
1 |
---
|
2 |
+
title: StarChat Demo
|
3 |
+
emoji: 💬
|
4 |
colorFrom: pink
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
StarCoderBanner.png
ADDED
Git LFS Details
|
app.py
ADDED
@@ -0,0 +1,405 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import shutil
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import requests
|
7 |
+
from huggingface_hub import Repository
|
8 |
+
from share_btn import community_icon_html, loading_icon_html, share_btn_css, share_js
|
9 |
+
|
10 |
+
|
11 |
+
HF_TOKEN = os.environ.get("H4_TOKEN", None)
|
12 |
+
API_TOKEN = os.environ.get("API_TOKEN", None)
|
13 |
+
STAR_CHAT_API_URL = os.environ.get("STAR_CHAT_API_URL", None)
|
14 |
+
STAR_CHAT_GPT_API_URL = os.environ.get("STAR_CHAT_GPT_API_URL", None)
|
15 |
+
|
16 |
+
model_to_api = {
|
17 |
+
"StarChat": STAR_CHAT_API_URL,
|
18 |
+
"StarChatGPT": STAR_CHAT_GPT_API_URL,
|
19 |
+
}
|
20 |
+
PROMPT_TEMPLATE = "<|system|>\n{system}<|end|>\n<|user|>\n{prompt}<|end|>\n<|assistant|>"
|
21 |
+
|
22 |
+
theme = gr.themes.Monochrome(
|
23 |
+
primary_hue="indigo",
|
24 |
+
secondary_hue="blue",
|
25 |
+
neutral_hue="slate",
|
26 |
+
radius_size=gr.themes.sizes.radius_sm,
|
27 |
+
font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
|
28 |
+
)
|
29 |
+
|
30 |
+
if HF_TOKEN:
|
31 |
+
try:
|
32 |
+
shutil.rmtree("./data/")
|
33 |
+
except:
|
34 |
+
pass
|
35 |
+
|
36 |
+
repo = Repository(
|
37 |
+
local_dir="./data/", clone_from="trl-lib/star-chat-prompts", use_auth_token=HF_TOKEN, repo_type="dataset"
|
38 |
+
)
|
39 |
+
repo.git_pull()
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
def save_inputs_and_outputs(inputs, outputs, generate_kwargs):
|
44 |
+
with open(os.path.join("data", "prompts.jsonl"), "a") as f:
|
45 |
+
json.dump({"inputs": inputs, "outputs": outputs,
|
46 |
+
"generate_kwargs": generate_kwargs}, f, ensure_ascii=False)
|
47 |
+
f.write("\n")
|
48 |
+
repo.push_to_hub()
|
49 |
+
|
50 |
+
|
51 |
+
def inference(
|
52 |
+
model, prompt, system_message, user_message, temperature, top_p, top_k, max_new_tokens, do_sample, eos_token_id
|
53 |
+
):
|
54 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
55 |
+
api_url = model_to_api[model]
|
56 |
+
print(f"CUSTOM_LOG {model} - {api_url}")
|
57 |
+
response = requests.post(
|
58 |
+
api_url,
|
59 |
+
headers=headers,
|
60 |
+
json={
|
61 |
+
"inputs": prompt,
|
62 |
+
"parameters": {
|
63 |
+
"do_sample": do_sample,
|
64 |
+
"temperature": temperature,
|
65 |
+
"top_p": top_p,
|
66 |
+
"top_k": top_k,
|
67 |
+
"max_new_tokens": max_new_tokens,
|
68 |
+
"eos_token_id": eos_token_id,
|
69 |
+
},
|
70 |
+
},
|
71 |
+
)
|
72 |
+
|
73 |
+
if response.status_code != 200:
|
74 |
+
return None
|
75 |
+
completion = response.json()[0]["generated_text"]
|
76 |
+
if user_message in completion:
|
77 |
+
completion = completion.lstrip()[len(f"{system_message}\n{user_message}\n"):]
|
78 |
+
return completion
|
79 |
+
|
80 |
+
|
81 |
+
def get_total_inputs(inputs, chatbot, preprompt, user_name, assistant_name, sep):
|
82 |
+
past = []
|
83 |
+
for data in chatbot:
|
84 |
+
user_data, model_data = data
|
85 |
+
|
86 |
+
if not user_data.startswith(user_name):
|
87 |
+
user_data = user_name + user_data
|
88 |
+
if not model_data.startswith(sep + assistant_name):
|
89 |
+
model_data = sep + assistant_name + model_data
|
90 |
+
|
91 |
+
past.append(user_data + model_data.rstrip() + sep)
|
92 |
+
|
93 |
+
if not inputs.startswith(user_name):
|
94 |
+
inputs = user_name + inputs
|
95 |
+
|
96 |
+
total_inputs = preprompt + "".join(past) + inputs + sep + assistant_name.rstrip()
|
97 |
+
|
98 |
+
return total_inputs
|
99 |
+
|
100 |
+
|
101 |
+
def has_no_history(chatbot, history):
|
102 |
+
return not chatbot and not history
|
103 |
+
|
104 |
+
|
105 |
+
def generate(
|
106 |
+
model,
|
107 |
+
system_message,
|
108 |
+
user_message,
|
109 |
+
chatbot,
|
110 |
+
history,
|
111 |
+
temperature=0.5,
|
112 |
+
top_p=0.25,
|
113 |
+
top_k=50,
|
114 |
+
max_new_tokens=512,
|
115 |
+
do_save=True,
|
116 |
+
):
|
117 |
+
# Don't return meaningless message when the input is empty
|
118 |
+
if not user_message:
|
119 |
+
return chatbot, history, user_message, ""
|
120 |
+
|
121 |
+
prompt = PROMPT_TEMPLATE.format(system=system_message, prompt=user_message)
|
122 |
+
|
123 |
+
history.append(user_message)
|
124 |
+
|
125 |
+
generate_kwargs = {
|
126 |
+
"temperature": temperature,
|
127 |
+
"top_p": top_p,
|
128 |
+
"top_k": top_k,
|
129 |
+
"max_new_tokens": max_new_tokens,
|
130 |
+
"do_sample": True,
|
131 |
+
"eos_token_id": [49155, 32003],
|
132 |
+
}
|
133 |
+
|
134 |
+
response = inference(model, prompt, system_message, user_message, **generate_kwargs)
|
135 |
+
|
136 |
+
history.append(response)
|
137 |
+
chat = [(history[i].strip(), history[i + 1].strip()) for i in range(0, len(history) - 1, 2)]
|
138 |
+
|
139 |
+
if HF_TOKEN and do_save:
|
140 |
+
try:
|
141 |
+
print("Pushing prompt and completion to the Hub")
|
142 |
+
save_inputs_and_outputs(prompt, output, generate_kwargs)
|
143 |
+
except Exception as e:
|
144 |
+
print(e)
|
145 |
+
|
146 |
+
return chat, history, user_message, ""
|
147 |
+
|
148 |
+
|
149 |
+
examples = [
|
150 |
+
"What's the capital city of Brunei?",
|
151 |
+
"How can I sort a list in Python?",
|
152 |
+
"What date is it today? Use Python to answer the question.",
|
153 |
+
"What's the meaning of life?",
|
154 |
+
"How can I write a Java function to generate the nth Fibonacci number?",
|
155 |
+
]
|
156 |
+
|
157 |
+
|
158 |
+
def regenerate(
|
159 |
+
model,
|
160 |
+
system_message,
|
161 |
+
user_message,
|
162 |
+
chatbot,
|
163 |
+
history,
|
164 |
+
temperature=0.5,
|
165 |
+
top_p=0.25,
|
166 |
+
top_k=50,
|
167 |
+
max_new_tokens=512,
|
168 |
+
do_save=True,
|
169 |
+
):
|
170 |
+
# Do nothing if there's no history
|
171 |
+
if has_no_history(chatbot, history):
|
172 |
+
return (
|
173 |
+
chatbot,
|
174 |
+
history,
|
175 |
+
user_message,
|
176 |
+
"",
|
177 |
+
)
|
178 |
+
|
179 |
+
chatbot = chatbot[:-1]
|
180 |
+
history = history[:-2]
|
181 |
+
|
182 |
+
return generate(
|
183 |
+
model, system_message, user_message, chatbot, history, temperature, top_p, top_k, max_new_tokens, do_save
|
184 |
+
)
|
185 |
+
|
186 |
+
|
187 |
+
def clear_chat():
|
188 |
+
return [], []
|
189 |
+
|
190 |
+
|
191 |
+
def radio_on_change():
|
192 |
+
return [], []
|
193 |
+
|
194 |
+
# def radio_on_change(
|
195 |
+
# model, system_message, user_message, chatbot, history, temperature, top_p, top_k, max_new_tokens, do_save
|
196 |
+
# ):
|
197 |
+
# return generate(
|
198 |
+
# model, system_message, user_message, chatbot, history, temperature, top_p, top_k, max_new_tokens, do_save
|
199 |
+
# )
|
200 |
+
|
201 |
+
|
202 |
+
def process_example(args):
|
203 |
+
for [x, y] in generate(args):
|
204 |
+
pass
|
205 |
+
return [x, y]
|
206 |
+
|
207 |
+
|
208 |
+
title = """<h1 align="center">⭐ StarChat Demo 💬</h1>"""
|
209 |
+
custom_css = """
|
210 |
+
#banner-image {
|
211 |
+
display: block;
|
212 |
+
margin-left: auto;
|
213 |
+
margin-right: auto;
|
214 |
+
width: 40%;
|
215 |
+
}
|
216 |
+
|
217 |
+
#chat-message .message {
|
218 |
+
padding: 15px;
|
219 |
+
border-color: #a5b4fc;
|
220 |
+
background-color: #eef2ff;
|
221 |
+
}
|
222 |
+
|
223 |
+
#chat-message .message.bot {
|
224 |
+
padding: 15px;
|
225 |
+
border-color: #e2e8f0;
|
226 |
+
background-color: #f8fafc;
|
227 |
+
}
|
228 |
+
|
229 |
+
#system-message {
|
230 |
+
min-height: 622px;
|
231 |
+
}
|
232 |
+
|
233 |
+
#system-message textarea {
|
234 |
+
min-height: 562px;
|
235 |
+
}
|
236 |
+
|
237 |
+
#chat-message {
|
238 |
+
font-size: 14px;
|
239 |
+
min-height: 500px;
|
240 |
+
}
|
241 |
+
|
242 |
+
message pending
|
243 |
+
|
244 |
+
"""
|
245 |
+
|
246 |
+
css = share_btn_css + custom_css
|
247 |
+
|
248 |
+
with gr.Blocks(theme=theme, analytics_enabled=False, css=css) as demo:
|
249 |
+
gr.HTML(title)
|
250 |
+
gr.Image("StarCoderBanner.png", elem_id="banner-image", show_label=False)
|
251 |
+
gr.Markdown(
|
252 |
+
"""
|
253 |
+
StarChat is an instruction fine-tuned model based on [StarCoder](https://huggingface.co/bigcode/starcoder), a 16B parameter model trained on one trillion tokens sourced from 80+ programming languages, GitHub issues, Git commits, and Jupyter notebooks (all permissively licensed). With an enterprise-friendly license, 8,192 token context length, and fast large-batch inference via [multi-query attention](https://arxiv.org/abs/1911.02150), StarCoder is currently the best open-source choice for code-based applications. For more details, check out our [blog post]().
|
254 |
+
|
255 |
+
⚠️ **Intended Use**: this app and its supporting models ([StarChat](https://huggingface.co/HuggingFaceH4/starchat) and [StarChatGPT](https://huggingface.co/HuggingFaceH4/starchatgpt)) are provided as educational tools to explain instruction fine-tuning; not to serve as replacement for human expertise. For more details on the model's limitations in terms of factuality and biases, see the model cards: [StarChat](https://huggingface.co/HuggingFaceH4/starchat#bias-risks-and-limitations) and [StarChatGPT](https://huggingface.co/HuggingFaceH4/starchatgpt#bias-risks-and-limitations).
|
256 |
+
|
257 |
+
⚠️ **Data Collection**: by default, we are collecting the prompts entered in this app to further improve and evaluate the model. Do not share any personal or sensitive information while using the app! You can opt out of this data collection by removing the checkbox below.
|
258 |
+
"""
|
259 |
+
)
|
260 |
+
|
261 |
+
with gr.Row():
|
262 |
+
with gr.Column(scale=1):
|
263 |
+
system_message = gr.Textbox(elem_id="system-message", label="System prompt")
|
264 |
+
|
265 |
+
with gr.Column(scale=2):
|
266 |
+
with gr.Box():
|
267 |
+
model = gr.Radio(
|
268 |
+
value="StarChat",
|
269 |
+
choices=[
|
270 |
+
"StarChat",
|
271 |
+
"StarChatGPT",
|
272 |
+
],
|
273 |
+
label="Model",
|
274 |
+
interactive=True,
|
275 |
+
)
|
276 |
+
output = gr.Markdown()
|
277 |
+
chatbot = gr.Chatbot(elem_id="chat-message", label="Chat")
|
278 |
+
|
279 |
+
with gr.Row():
|
280 |
+
with gr.Column(scale=3):
|
281 |
+
do_save = gr.Checkbox(
|
282 |
+
value=True,
|
283 |
+
label="Store data",
|
284 |
+
info="You agree to the storage of your prompt and generated text for research and development purposes:",
|
285 |
+
)
|
286 |
+
user_message = gr.Textbox(placeholder="Enter your message here",
|
287 |
+
show_label=False, elem_id="q-input")
|
288 |
+
with gr.Row():
|
289 |
+
send_button = gr.Button("Send", elem_id="send-btn", visible=True)
|
290 |
+
regenerate_button = gr.Button("Regenerate", elem_id="send-btn", visible=True)
|
291 |
+
|
292 |
+
clear_chat_button = gr.Button("Clear chat", elem_id="clear-btn", visible=True)
|
293 |
+
|
294 |
+
with gr.Group(elem_id="share-btn-container"):
|
295 |
+
community_icon = gr.HTML(community_icon_html, visible=True)
|
296 |
+
loading_icon = gr.HTML(loading_icon_html, visible=True)
|
297 |
+
share_button = gr.Button("Share to community", elem_id="share-btn", visible=True)
|
298 |
+
with gr.Row():
|
299 |
+
gr.Examples(
|
300 |
+
examples=examples,
|
301 |
+
inputs=[user_message],
|
302 |
+
cache_examples=False,
|
303 |
+
fn=process_example,
|
304 |
+
outputs=[output],
|
305 |
+
)
|
306 |
+
|
307 |
+
with gr.Column(scale=1):
|
308 |
+
temperature = gr.Slider(
|
309 |
+
label="Temperature",
|
310 |
+
value=0.8,
|
311 |
+
minimum=0.0,
|
312 |
+
maximum=2.0,
|
313 |
+
step=0.1,
|
314 |
+
interactive=True,
|
315 |
+
info="Higher values produce more diverse outputs",
|
316 |
+
)
|
317 |
+
top_k = gr.Slider(
|
318 |
+
label="Top-k",
|
319 |
+
value=50,
|
320 |
+
minimum=0.0,
|
321 |
+
maximum=100,
|
322 |
+
step=1,
|
323 |
+
interactive=True,
|
324 |
+
info="Sample from a shortlist of top-k tokens",
|
325 |
+
)
|
326 |
+
top_p = gr.Slider(
|
327 |
+
label="Top-p (nucleus sampling)",
|
328 |
+
value=0.25,
|
329 |
+
minimum=0.0,
|
330 |
+
maximum=1,
|
331 |
+
step=0.05,
|
332 |
+
interactive=True,
|
333 |
+
info="Higher values sample more low-probability tokens",
|
334 |
+
)
|
335 |
+
max_new_tokens = gr.Slider(
|
336 |
+
label="Max new tokens",
|
337 |
+
value=512,
|
338 |
+
minimum=0,
|
339 |
+
maximum=2048,
|
340 |
+
step=4,
|
341 |
+
interactive=True,
|
342 |
+
info="The maximum numbers of new tokens",
|
343 |
+
)
|
344 |
+
|
345 |
+
history = gr.State([])
|
346 |
+
# To clear out "message" input textbox and use this to regenerate message
|
347 |
+
last_user_message = gr.State("")
|
348 |
+
|
349 |
+
user_message.submit(
|
350 |
+
generate,
|
351 |
+
inputs=[
|
352 |
+
model,
|
353 |
+
system_message,
|
354 |
+
user_message,
|
355 |
+
chatbot,
|
356 |
+
history,
|
357 |
+
temperature,
|
358 |
+
top_p,
|
359 |
+
top_k,
|
360 |
+
max_new_tokens,
|
361 |
+
do_save,
|
362 |
+
],
|
363 |
+
outputs=[chatbot, history, last_user_message, user_message],
|
364 |
+
)
|
365 |
+
|
366 |
+
send_button.click(
|
367 |
+
generate,
|
368 |
+
inputs=[
|
369 |
+
model,
|
370 |
+
system_message,
|
371 |
+
user_message,
|
372 |
+
chatbot,
|
373 |
+
history,
|
374 |
+
temperature,
|
375 |
+
top_p,
|
376 |
+
top_k,
|
377 |
+
max_new_tokens,
|
378 |
+
do_save,
|
379 |
+
],
|
380 |
+
outputs=[chatbot, history, last_user_message, user_message],
|
381 |
+
)
|
382 |
+
|
383 |
+
regenerate_button.click(
|
384 |
+
regenerate,
|
385 |
+
inputs=[
|
386 |
+
model,
|
387 |
+
system_message,
|
388 |
+
last_user_message,
|
389 |
+
chatbot,
|
390 |
+
history,
|
391 |
+
temperature,
|
392 |
+
top_p,
|
393 |
+
top_k,
|
394 |
+
max_new_tokens,
|
395 |
+
do_save,
|
396 |
+
],
|
397 |
+
outputs=[chatbot, history, last_user_message, user_message],
|
398 |
+
)
|
399 |
+
|
400 |
+
clear_chat_button.click(clear_chat, outputs=[chatbot, history])
|
401 |
+
|
402 |
+
model.change(radio_on_change, outputs=[chatbot, history])
|
403 |
+
share_button.click(None, [], [], _js=share_js)
|
404 |
+
|
405 |
+
demo.queue(concurrency_count=16).launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
huggingface_hub
|
2 |
+
text-generation
|
share_btn.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
community_icon_html = """<svg id="share-btn-share-icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32">
|
2 |
+
<path d="M20.6081 3C21.7684 3 22.8053 3.49196 23.5284 4.38415C23.9756 4.93678 24.4428 5.82749 24.4808 7.16133C24.9674 7.01707 25.4353 6.93643 25.8725 6.93643C26.9833 6.93643 27.9865 7.37587 28.696 8.17411C29.6075 9.19872 30.0124 10.4579 29.8361 11.7177C29.7523 12.3177 29.5581 12.8555 29.2678 13.3534C29.8798 13.8646 30.3306 14.5763 30.5485 15.4322C30.719 16.1032 30.8939 17.5006 29.9808 18.9403C30.0389 19.0342 30.0934 19.1319 30.1442 19.2318C30.6932 20.3074 30.7283 21.5229 30.2439 22.6548C29.5093 24.3704 27.6841 25.7219 24.1397 27.1727C21.9347 28.0753 19.9174 28.6523 19.8994 28.6575C16.9842 29.4379 14.3477 29.8345 12.0653 29.8345C7.87017 29.8345 4.8668 28.508 3.13831 25.8921C0.356375 21.6797 0.754104 17.8269 4.35369 14.1131C6.34591 12.058 7.67023 9.02782 7.94613 8.36275C8.50224 6.39343 9.97271 4.20438 12.4172 4.20438H12.4179C12.6236 4.20438 12.8314 4.2214 13.0364 4.25468C14.107 4.42854 15.0428 5.06476 15.7115 6.02205C16.4331 5.09583 17.134 4.359 17.7682 3.94323C18.7242 3.31737 19.6794 3 20.6081 3ZM20.6081 5.95917C20.2427 5.95917 19.7963 6.1197 19.3039 6.44225C17.7754 7.44319 14.8258 12.6772 13.7458 14.7131C13.3839 15.3952 12.7655 15.6837 12.2086 15.6837C11.1036 15.6837 10.2408 14.5497 12.1076 13.1085C14.9146 10.9402 13.9299 7.39584 12.5898 7.1776C12.5311 7.16799 12.4731 7.16355 12.4172 7.16355C11.1989 7.16355 10.6615 9.33114 10.6615 9.33114C10.6615 9.33114 9.0863 13.4148 6.38031 16.206C3.67434 18.998 3.5346 21.2388 5.50675 24.2246C6.85185 26.2606 9.42666 26.8753 12.0653 26.8753C14.8021 26.8753 17.6077 26.2139 19.1799 25.793C19.2574 25.7723 28.8193 22.984 27.6081 20.6107C27.4046 20.212 27.0693 20.0522 26.6471 20.0522C24.9416 20.0522 21.8393 22.6726 20.5057 22.6726C20.2076 22.6726 19.9976 22.5416 19.9116 22.222C19.3433 20.1173 28.552 19.2325 27.7758 16.1839C27.639 15.6445 27.2677 15.4256 26.746 15.4263C24.4923 15.4263 19.4358 19.5181 18.3759 19.5181C18.2949 19.5181 18.2368 19.4937 18.2053 19.4419C17.6743 18.557 17.9653 17.9394 21.7082 15.6009C25.4511 13.2617 28.0783 11.8545 26.5841 10.1752C26.4121 9.98141 26.1684 9.8956 25.8725 9.8956C23.6001 9.89634 18.2311 14.9403 18.2311 14.9403C18.2311 14.9403 16.7821 16.496 15.9057 16.496C15.7043 16.496 15.533 16.4139 15.4169 16.2112C14.7956 15.1296 21.1879 10.1286 21.5484 8.06535C21.7928 6.66715 21.3771 5.95917 20.6081 5.95917Z" fill="#FF9D00"></path>
|
3 |
+
<path d="M5.50686 24.2246C3.53472 21.2387 3.67446 18.9979 6.38043 16.206C9.08641 13.4147 10.6615 9.33111 10.6615 9.33111C10.6615 9.33111 11.2499 6.95933 12.59 7.17757C13.93 7.39581 14.9139 10.9401 12.1069 13.1084C9.29997 15.276 12.6659 16.7489 13.7459 14.713C14.8258 12.6772 17.7747 7.44316 19.304 6.44221C20.8326 5.44128 21.9089 6.00204 21.5484 8.06532C21.188 10.1286 14.795 15.1295 15.4171 16.2118C16.0391 17.2934 18.2312 14.9402 18.2312 14.9402C18.2312 14.9402 25.0907 8.49588 26.5842 10.1752C28.0776 11.8545 25.4512 13.2616 21.7082 15.6008C17.9646 17.9393 17.6744 18.557 18.2054 19.4418C18.7372 20.3266 26.9998 13.1351 27.7759 16.1838C28.5513 19.2324 19.3434 20.1173 19.9117 22.2219C20.48 24.3274 26.3979 18.2382 27.6082 20.6107C28.8193 22.9839 19.2574 25.7722 19.18 25.7929C16.0914 26.62 8.24723 28.3726 5.50686 24.2246Z" fill="#FFD21E"></path>
|
4 |
+
</svg>"""
|
5 |
+
|
6 |
+
loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" class="animate-spin"
|
7 |
+
style="color: #ffffff;
|
8 |
+
"
|
9 |
+
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
|
10 |
+
|
11 |
+
share_js = """async () => {
|
12 |
+
async function uploadFile(file){
|
13 |
+
const UPLOAD_URL = 'https://huggingface.co/uploads';
|
14 |
+
const response = await fetch(UPLOAD_URL, {
|
15 |
+
method: 'POST',
|
16 |
+
headers: {
|
17 |
+
'Content-Type': file.type,
|
18 |
+
'X-Requested-With': 'XMLHttpRequest',
|
19 |
+
},
|
20 |
+
body: file, /// <- File inherits from Blob
|
21 |
+
});
|
22 |
+
const url = await response.text();
|
23 |
+
return url;
|
24 |
+
}
|
25 |
+
|
26 |
+
async function getInputImgFile(imgEl){
|
27 |
+
const res = await fetch(imgEl.src);
|
28 |
+
const blob = await res.blob();
|
29 |
+
const imgId = Date.now() % 200;
|
30 |
+
const isPng = imgEl.src.startsWith(`data:image/png`);
|
31 |
+
if(isPng){
|
32 |
+
const fileName = `sd-perception-${{imgId}}.png`;
|
33 |
+
return new File([blob], fileName, { type: 'image/png' });
|
34 |
+
}else{
|
35 |
+
const fileName = `sd-perception-${{imgId}}.jpg`;
|
36 |
+
return new File([blob], fileName, { type: 'image/jpeg' });
|
37 |
+
}
|
38 |
+
}
|
39 |
+
|
40 |
+
// const gradioEl = document.querySelector('body > gradio-app');
|
41 |
+
const gradioEl = document.querySelector("gradio-app");
|
42 |
+
const inputTxt = gradioEl.querySelector('#q-input textarea').value;
|
43 |
+
const outputTxt = gradioEl.querySelector('#q-output').outerHTML;
|
44 |
+
|
45 |
+
const titleLength = 150;
|
46 |
+
let titleTxt = inputTxt;
|
47 |
+
if(titleTxt.length > titleLength){
|
48 |
+
titleTxt = titleTxt.slice(0, titleLength) + ' ...';
|
49 |
+
}
|
50 |
+
|
51 |
+
const shareBtnEl = gradioEl.querySelector('#share-btn');
|
52 |
+
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
|
53 |
+
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
|
54 |
+
|
55 |
+
if(!inputTxt || !outputTxt){
|
56 |
+
return;
|
57 |
+
};
|
58 |
+
|
59 |
+
shareBtnEl.style.pointerEvents = 'none';
|
60 |
+
shareIconEl.style.display = 'none';
|
61 |
+
loadingIconEl.style.removeProperty('display');
|
62 |
+
|
63 |
+
const descriptionMd = `### Question:
|
64 |
+
${inputTxt}
|
65 |
+
|
66 |
+
### Answer:
|
67 |
+
|
68 |
+
${outputTxt}`;
|
69 |
+
|
70 |
+
const params = {
|
71 |
+
title: titleTxt,
|
72 |
+
description: descriptionMd,
|
73 |
+
};
|
74 |
+
|
75 |
+
const paramsStr = Object.entries(params)
|
76 |
+
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
77 |
+
.join('&');
|
78 |
+
|
79 |
+
window.open(`https://huggingface.co/spaces/trl-lib/stack-llama/discussions/new?${paramsStr}`, '_blank');
|
80 |
+
|
81 |
+
shareBtnEl.style.removeProperty('pointer-events');
|
82 |
+
shareIconEl.style.removeProperty('display');
|
83 |
+
loadingIconEl.style.display = 'none';
|
84 |
+
}"""
|
85 |
+
|
86 |
+
share_btn_css = """
|
87 |
+
a {text-decoration-line: underline; font-weight: 600;}
|
88 |
+
.animate-spin {
|
89 |
+
animation: spin 1s linear infinite;
|
90 |
+
}
|
91 |
+
@keyframes spin {
|
92 |
+
from { transform: rotate(0deg); }
|
93 |
+
to { transform: rotate(360deg); }
|
94 |
+
}
|
95 |
+
#share-btn-container {
|
96 |
+
display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
|
97 |
+
}
|
98 |
+
#share-btn {
|
99 |
+
all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;
|
100 |
+
}
|
101 |
+
#share-btn * {
|
102 |
+
all: unset;
|
103 |
+
}
|
104 |
+
#share-btn-container div:nth-child(-n+2){
|
105 |
+
width: auto !important;
|
106 |
+
min-height: 0px !important;
|
107 |
+
}
|
108 |
+
#share-btn-container .wrap {
|
109 |
+
display: none !important;
|
110 |
+
}
|
111 |
+
"""
|