Duplicate from mikeee/chinese-llama-2-7b-ggml-q4
Browse filesCo-authored-by: mikeee <mikeee@users.noreply.huggingface.co>
- .gitattributes +35 -0
- .gitignore +12 -0
- .ruff.toml +17 -0
- .stignore +103 -0
- README.md +12 -0
- app.py +406 -0
- requirements.txt +8 -0
- run-app.sh +1 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
call-activate.bat
|
2 |
+
okteto.yml
|
3 |
+
okteto-up.bat
|
4 |
+
install-sw.sh
|
5 |
+
install-sw1.sh
|
6 |
+
start-sshd.sh
|
7 |
+
pyproject.toml
|
8 |
+
models
|
9 |
+
.ruff_cache
|
10 |
+
run-nodemon.sh
|
11 |
+
app-.py
|
12 |
+
nodemon.json
|
.ruff.toml
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Assume Python 3.10.
|
2 |
+
target-version = "py310"
|
3 |
+
# Decrease the maximum line length to 79 characters.
|
4 |
+
line-length = 300
|
5 |
+
|
6 |
+
# pyflakes, pycodestyle, isort
|
7 |
+
# flake8 YTT, pydocstyle D, pylint PLC
|
8 |
+
select = ["F", "E", "W", "I001", "YTT", "D", "PLC"]
|
9 |
+
# select = ["ALL"]
|
10 |
+
|
11 |
+
# D103 Missing docstring in public function
|
12 |
+
# D101 Missing docstring in public class
|
13 |
+
# `multi-line-summary-first-line` (D212)
|
14 |
+
# `one-blank-line-before-class` (D203)
|
15 |
+
extend-ignore = ["D103", "D101", "D212", "D203"]
|
16 |
+
|
17 |
+
exclude = [".venv"]
|
.stignore
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
models
|
2 |
+
*.bin
|
3 |
+
.git
|
4 |
+
# Byte-compiled / optimized / DLL files
|
5 |
+
__pycache__
|
6 |
+
*.py[cod]
|
7 |
+
*$py.class
|
8 |
+
|
9 |
+
# C extensions
|
10 |
+
*.so
|
11 |
+
|
12 |
+
# Distribution / packaging
|
13 |
+
.Python
|
14 |
+
build
|
15 |
+
develop-eggs
|
16 |
+
dist
|
17 |
+
downloads
|
18 |
+
eggs
|
19 |
+
.eggs
|
20 |
+
lib
|
21 |
+
lib64
|
22 |
+
parts
|
23 |
+
sdist
|
24 |
+
var
|
25 |
+
wheels
|
26 |
+
pip-wheel-metadata
|
27 |
+
share/python-wheels
|
28 |
+
*.egg-info
|
29 |
+
.installed.cfg
|
30 |
+
*.egg
|
31 |
+
MANIFEST
|
32 |
+
|
33 |
+
# PyInstaller
|
34 |
+
# Usually these files are written by a python script from a template
|
35 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
36 |
+
*.manifest
|
37 |
+
*.spec
|
38 |
+
|
39 |
+
# Installer logs
|
40 |
+
pip-log.txt
|
41 |
+
pip-delete-this-directory.txt
|
42 |
+
|
43 |
+
# Translations
|
44 |
+
*.mo
|
45 |
+
*.pot
|
46 |
+
|
47 |
+
# Django stuff:
|
48 |
+
*.log
|
49 |
+
local_settings.py
|
50 |
+
db.sqlite3
|
51 |
+
|
52 |
+
# Flask stuff:
|
53 |
+
instance
|
54 |
+
.webassets-cache
|
55 |
+
|
56 |
+
# Scrapy stuff:
|
57 |
+
.scrapy
|
58 |
+
|
59 |
+
# Sphinx documentation
|
60 |
+
docs/_build
|
61 |
+
|
62 |
+
# PyBuilder
|
63 |
+
target
|
64 |
+
|
65 |
+
# Jupyter Notebook
|
66 |
+
.ipynb_checkpoints
|
67 |
+
|
68 |
+
# IPython
|
69 |
+
profile_default
|
70 |
+
ipython_config.py
|
71 |
+
|
72 |
+
# pyenv
|
73 |
+
.python-version
|
74 |
+
|
75 |
+
# celery beat schedule file
|
76 |
+
celerybeat-schedule
|
77 |
+
|
78 |
+
# SageMath parsed files
|
79 |
+
*.sage.py
|
80 |
+
|
81 |
+
# Environments
|
82 |
+
.env
|
83 |
+
.venv
|
84 |
+
env
|
85 |
+
venv
|
86 |
+
ENV
|
87 |
+
env.bak
|
88 |
+
venv.bak
|
89 |
+
|
90 |
+
# Spyder project settings
|
91 |
+
.spyderproject
|
92 |
+
.spyproject
|
93 |
+
|
94 |
+
# Rope project settings
|
95 |
+
.ropeproject
|
96 |
+
|
97 |
+
# mypy
|
98 |
+
.mypy_cache
|
99 |
+
.dmypy.json
|
100 |
+
dmypy.json
|
101 |
+
|
102 |
+
# Pyre type checker
|
103 |
+
.pyre
|
README.md
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: chinese-llama-2-7b-ggml-q4
|
3 |
+
emoji: 🚀
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: green
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.37.0
|
8 |
+
app_file: app.py
|
9 |
+
duplicated_from: mikeee/chinese-llama-2-7b-ggml-q4
|
10 |
+
---
|
11 |
+
|
12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,406 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Run codes."""
|
2 |
+
# pylint: disable=line-too-long, broad-exception-caught, invalid-name, missing-function-docstring, too-many-instance-attributes, missing-class-docstring
|
3 |
+
# ruff: noqa: E501
|
4 |
+
import os
|
5 |
+
import platform
|
6 |
+
import random
|
7 |
+
import time
|
8 |
+
from dataclasses import asdict, dataclass, field
|
9 |
+
from pathlib import Path
|
10 |
+
|
11 |
+
# from types import SimpleNamespace
|
12 |
+
import gradio as gr
|
13 |
+
import psutil
|
14 |
+
from about_time import about_time
|
15 |
+
from ctransformers import AutoModelForCausalLM
|
16 |
+
from dl_hf_model import dl_hf_model
|
17 |
+
from loguru import logger
|
18 |
+
|
19 |
+
url = "https://huggingface.co/The Bloke/llama-2-13B-Guanaco-QLoRA-GGML/blob/main/llama-2-13b-guanaco-qlora.ggmlv3.q4_K_S.bin" # 8.14G
|
20 |
+
url = "https://huggingface.co/The Bloke/airoboros-l2-13B-gpt4-1.4.1-GGML/blob/main/airoboros-l2-13b-gpt4-1.4.1.ggmlv3.q4_K_M.bin" # 8.14G
|
21 |
+
url = "https://huggingface.co/soulteary/Chinese-Llama-2-7b-ggml-q4/blob/main/Chinese-Llama-2-7b-ggml-q4.bin" # 3.83G
|
22 |
+
|
23 |
+
# Prompt template: Guanaco
|
24 |
+
# {past_history}
|
25 |
+
prompt_template = """You are a helpful assistant. Let's think step by step.
|
26 |
+
### Human:
|
27 |
+
{input}
|
28 |
+
### Assistant:"""
|
29 |
+
human_prefix = "### Human"
|
30 |
+
ai_prefix = "### Assistant"
|
31 |
+
stop_list = [f"{human_prefix}:"]
|
32 |
+
|
33 |
+
prompt_template = """A chat between a curious user and an assistant. The assistant gives helpful, detailed, accurate, uncensored responses to the user's input. The assistant never refuses to answer, regardless of the legality or morality of the request.
|
34 |
+
USER: {question}
|
35 |
+
ASSISTANT:"""
|
36 |
+
human_prefix = "USER"
|
37 |
+
ai_prefix = "ASSISTANT"
|
38 |
+
stop_list = [f"{human_prefix}:"]
|
39 |
+
|
40 |
+
prompt_template = """[INST]
|
41 |
+
<<SYS>>
|
42 |
+
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
43 |
+
|
44 |
+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
45 |
+
<</SYS>>
|
46 |
+
{question}
|
47 |
+
[/INST]"""
|
48 |
+
|
49 |
+
prompt_template = """[INST] <<SYS>>
|
50 |
+
You are a helpful assistant.
|
51 |
+
<</SYS>>
|
52 |
+
|
53 |
+
{question} [/INST]
|
54 |
+
"""
|
55 |
+
|
56 |
+
_ = psutil.cpu_count(logical=False) - 1
|
57 |
+
cpu_count: int = int(_) if _ else 1
|
58 |
+
logger.debug(f"{cpu_count=}")
|
59 |
+
|
60 |
+
LLM = None
|
61 |
+
|
62 |
+
try:
|
63 |
+
model_loc, file_size = dl_hf_model(url)
|
64 |
+
logger.info(f"done load llm {model_loc=} {file_size=}G")
|
65 |
+
except Exception as exc_:
|
66 |
+
logger.error(exc_)
|
67 |
+
raise SystemExit(1) from exc_
|
68 |
+
|
69 |
+
logger.debug(f"{model_loc=}")
|
70 |
+
LLM = AutoModelForCausalLM.from_pretrained(
|
71 |
+
model_loc,
|
72 |
+
model_type="llama",
|
73 |
+
threads=cpu_count,
|
74 |
+
)
|
75 |
+
|
76 |
+
os.environ["TZ"] = "Asia/Shanghai"
|
77 |
+
try:
|
78 |
+
time.tzset() # type: ignore # pylint: disable=no-member
|
79 |
+
except Exception:
|
80 |
+
# Windows
|
81 |
+
logger.warning("Windows, cant run time.tzset()")
|
82 |
+
|
83 |
+
|
84 |
+
@dataclass
|
85 |
+
class GenerationConfig:
|
86 |
+
temperature: float = 0.7
|
87 |
+
top_k: int = 50
|
88 |
+
top_p: float = 0.9
|
89 |
+
repetition_penalty: float = 1.0
|
90 |
+
max_new_tokens: int = 512
|
91 |
+
seed: int = 42
|
92 |
+
reset: bool = False
|
93 |
+
stream: bool = True
|
94 |
+
threads: int = cpu_count
|
95 |
+
# stop: list[str] = field(default_factory=lambda: stop_list)
|
96 |
+
|
97 |
+
|
98 |
+
def generate(
|
99 |
+
question: str,
|
100 |
+
llm=LLM,
|
101 |
+
config: GenerationConfig = GenerationConfig(),
|
102 |
+
):
|
103 |
+
"""Run model inference, will return a Generator if streaming is true."""
|
104 |
+
# _ = prompt_template.format(question=question)
|
105 |
+
# print(_)
|
106 |
+
|
107 |
+
prompt = prompt_template.format(question=question)
|
108 |
+
|
109 |
+
return llm(
|
110 |
+
prompt,
|
111 |
+
**asdict(config),
|
112 |
+
)
|
113 |
+
|
114 |
+
|
115 |
+
logger.debug(f"{asdict(GenerationConfig())=}")
|
116 |
+
|
117 |
+
|
118 |
+
def user(user_message, history):
|
119 |
+
# return user_message, history + [[user_message, None]]
|
120 |
+
history.append([user_message, None])
|
121 |
+
return user_message, history # keep user_message
|
122 |
+
|
123 |
+
|
124 |
+
def user1(user_message, history):
|
125 |
+
# return user_message, history + [[user_message, None]]
|
126 |
+
history.append([user_message, None])
|
127 |
+
return "", history # clear user_message
|
128 |
+
|
129 |
+
|
130 |
+
def bot_(history):
|
131 |
+
user_message = history[-1][0]
|
132 |
+
resp = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
133 |
+
bot_message = user_message + ": " + resp
|
134 |
+
history[-1][1] = ""
|
135 |
+
for character in bot_message:
|
136 |
+
history[-1][1] += character
|
137 |
+
time.sleep(0.02)
|
138 |
+
yield history
|
139 |
+
|
140 |
+
history[-1][1] = resp
|
141 |
+
yield history
|
142 |
+
|
143 |
+
|
144 |
+
def bot(history):
|
145 |
+
user_message = history[-1][0]
|
146 |
+
response = []
|
147 |
+
|
148 |
+
logger.debug(f"{user_message=}")
|
149 |
+
|
150 |
+
with about_time() as atime: # type: ignore
|
151 |
+
flag = 1
|
152 |
+
prefix = ""
|
153 |
+
then = time.time()
|
154 |
+
|
155 |
+
logger.debug("about to generate")
|
156 |
+
|
157 |
+
config = GenerationConfig(reset=True)
|
158 |
+
for elm in generate(user_message, config=config):
|
159 |
+
if flag == 1:
|
160 |
+
logger.debug("in the loop")
|
161 |
+
prefix = f"({time.time() - then:.2f}s) "
|
162 |
+
flag = 0
|
163 |
+
print(prefix, end="", flush=True)
|
164 |
+
logger.debug(f"{prefix=}")
|
165 |
+
print(elm, end="", flush=True)
|
166 |
+
# logger.debug(f"{elm}")
|
167 |
+
|
168 |
+
response.append(elm)
|
169 |
+
history[-1][1] = prefix + "".join(response)
|
170 |
+
yield history
|
171 |
+
|
172 |
+
_ = (
|
173 |
+
f"(time elapsed: {atime.duration_human}, " # type: ignore
|
174 |
+
f"{atime.duration/len(''.join(response)):.2f}s/char)" # type: ignore
|
175 |
+
)
|
176 |
+
|
177 |
+
history[-1][1] = "".join(response) + f"\n{_}"
|
178 |
+
yield history
|
179 |
+
|
180 |
+
|
181 |
+
def predict_api(prompt):
|
182 |
+
logger.debug(f"{prompt=}")
|
183 |
+
try:
|
184 |
+
# user_prompt = prompt
|
185 |
+
config = GenerationConfig(
|
186 |
+
temperature=0.2,
|
187 |
+
top_k=10,
|
188 |
+
top_p=0.9,
|
189 |
+
repetition_penalty=1.0,
|
190 |
+
max_new_tokens=512, # adjust as needed
|
191 |
+
seed=42,
|
192 |
+
reset=True, # reset history (cache)
|
193 |
+
stream=False,
|
194 |
+
# threads=cpu_count,
|
195 |
+
# stop=prompt_prefix[1:2],
|
196 |
+
)
|
197 |
+
|
198 |
+
response = generate(
|
199 |
+
prompt,
|
200 |
+
config=config,
|
201 |
+
)
|
202 |
+
|
203 |
+
logger.debug(f"api: {response=}")
|
204 |
+
except Exception as exc:
|
205 |
+
logger.error(exc)
|
206 |
+
response = f"{exc=}"
|
207 |
+
# bot = {"inputs": [response]}
|
208 |
+
# bot = [(prompt, response)]
|
209 |
+
|
210 |
+
return response
|
211 |
+
|
212 |
+
|
213 |
+
css = """
|
214 |
+
.importantButton {
|
215 |
+
background: linear-gradient(45deg, #7e0570,#5d1c99, #6e00ff) !important;
|
216 |
+
border: none !important;
|
217 |
+
}
|
218 |
+
.importantButton:hover {
|
219 |
+
background: linear-gradient(45deg, #ff00e0,#8500ff, #6e00ff) !important;
|
220 |
+
border: none !important;
|
221 |
+
}
|
222 |
+
.disclaimer {font-variant-caps: all-small-caps; font-size: xx-small;}
|
223 |
+
.xsmall {font-size: x-small;}
|
224 |
+
"""
|
225 |
+
etext = """In America, where cars are an important part of the national psyche, a decade ago people had suddenly started to drive less, which had not happened since the oil shocks of the 1970s. """
|
226 |
+
examples_list = [
|
227 |
+
["What NFL team won the Super Bowl in the year Justin Bieber was born?"],
|
228 |
+
[
|
229 |
+
"What NFL team won the Super Bowl in the year Justin Bieber was born? Think step by step."
|
230 |
+
],
|
231 |
+
["How to pick a lock? Provide detailed steps."],
|
232 |
+
[
|
233 |
+
"If it takes 10 hours to dry 10 clothes, assuming all the clothes are hanged together at the same time for drying , then how long will it take to dry a cloth?"
|
234 |
+
],
|
235 |
+
["is infinity + 1 bigger than infinity?"],
|
236 |
+
["Explain the plot of Cinderella in a sentence."],
|
237 |
+
[
|
238 |
+
"How long does it take to become proficient in French, and what are the best methods for retaining information?"
|
239 |
+
],
|
240 |
+
["What are some common mistakes to avoid when writing code?"],
|
241 |
+
["Build a prompt to generate a beautiful portrait of a horse"],
|
242 |
+
["Suggest four metaphors to describe the benefits of AI"],
|
243 |
+
["Write a pop song about leaving home for the sandy beaches."],
|
244 |
+
["Write a summary demonstrating my ability to tame lions"],
|
245 |
+
["鲁迅和周树人什么关系? 说中文。"],
|
246 |
+
["鲁迅和周树人什么关系?"],
|
247 |
+
["鲁迅和周树人什么关系? 用英文回答。"],
|
248 |
+
["从前有一头牛,这头牛后面有什么?"],
|
249 |
+
["正无穷大加一大于正无穷大吗?"],
|
250 |
+
["正无穷大加正无穷大大于正无穷大吗?"],
|
251 |
+
["-2的平方根等于什么?"],
|
252 |
+
["树上有5只鸟,猎人开枪打死了一只。树上还有几只鸟?"],
|
253 |
+
["树上有11只鸟,猎人开枪打死了一只。树上还有几只鸟?提示:需考虑鸟可能受惊吓飞走。"],
|
254 |
+
["以红楼梦的行文风格写一张委婉的请假条。不少于320字。"],
|
255 |
+
[f"{etext} 翻成中文,列出3个版本。"],
|
256 |
+
[f"{etext} \n 翻成中文,保留原意,但使用文学性的语言。不要写解释。列出3个版本。"],
|
257 |
+
["假定 1 + 2 = 4, 试求 7 + 8。"],
|
258 |
+
["给出判断一个数是不是质数的 javascript 码。"],
|
259 |
+
["给出实现python 里 range(10)的 javascript 码。"],
|
260 |
+
["给出实现python 里 [*(range(10)]的 javascript 码。"],
|
261 |
+
["Erkläre die Handlung von Cinderella in einem Satz."],
|
262 |
+
["Erkläre die Handlung von Cinderella in einem Satz. Auf Deutsch."],
|
263 |
+
]
|
264 |
+
|
265 |
+
logger.info("start block")
|
266 |
+
|
267 |
+
with gr.Blocks(
|
268 |
+
title=f"{Path(model_loc).name}",
|
269 |
+
theme=gr.themes.Soft(text_size="sm", spacing_size="sm"),
|
270 |
+
css=css,
|
271 |
+
) as block:
|
272 |
+
# buff_var = gr.State("")
|
273 |
+
with gr.Accordion("🎈 Info", open=False):
|
274 |
+
gr.Markdown(
|
275 |
+
f"""<h5><center>{Path(model_loc).name}</center></h4>
|
276 |
+
Most examples are meant for another model.
|
277 |
+
You probably should try to test
|
278 |
+
some related prompts.""",
|
279 |
+
elem_classes="xsmall",
|
280 |
+
)
|
281 |
+
|
282 |
+
# chatbot = gr.Chatbot().style(height=700) # 500
|
283 |
+
chatbot = gr.Chatbot(height=500)
|
284 |
+
|
285 |
+
# buff = gr.Textbox(show_label=False, visible=True)
|
286 |
+
|
287 |
+
with gr.Row():
|
288 |
+
with gr.Column(scale=5):
|
289 |
+
msg = gr.Textbox(
|
290 |
+
label="Chat Message Box",
|
291 |
+
placeholder="Ask me anything (press Shift+Enter or click Submit to send)",
|
292 |
+
show_label=False,
|
293 |
+
# container=False,
|
294 |
+
lines=6,
|
295 |
+
max_lines=30,
|
296 |
+
show_copy_button=True,
|
297 |
+
# ).style(container=False)
|
298 |
+
)
|
299 |
+
with gr.Column(scale=1, min_width=50):
|
300 |
+
with gr.Row():
|
301 |
+
submit = gr.Button("Submit", elem_classes="xsmall")
|
302 |
+
stop = gr.Button("Stop", visible=True)
|
303 |
+
clear = gr.Button("Clear History", visible=True)
|
304 |
+
with gr.Row(visible=False):
|
305 |
+
with gr.Accordion("Advanced Options:", open=False):
|
306 |
+
with gr.Row():
|
307 |
+
with gr.Column(scale=2):
|
308 |
+
system = gr.Textbox(
|
309 |
+
label="System Prompt",
|
310 |
+
value=prompt_template,
|
311 |
+
show_label=False,
|
312 |
+
container=False,
|
313 |
+
# ).style(container=False)
|
314 |
+
)
|
315 |
+
with gr.Column():
|
316 |
+
with gr.Row():
|
317 |
+
change = gr.Button("Change System Prompt")
|
318 |
+
reset = gr.Button("Reset System Prompt")
|
319 |
+
|
320 |
+
with gr.Accordion("Example Inputs", open=True):
|
321 |
+
examples = gr.Examples(
|
322 |
+
examples=examples_list,
|
323 |
+
inputs=[msg],
|
324 |
+
examples_per_page=40,
|
325 |
+
)
|
326 |
+
|
327 |
+
# with gr.Row():
|
328 |
+
with gr.Accordion("Disclaimer", open=False):
|
329 |
+
_ = Path(model_loc).name
|
330 |
+
gr.Markdown(
|
331 |
+
f"Disclaimer: {_} can produce factually incorrect output, and should not be relied on to produce "
|
332 |
+
"factually accurate information. {_} was trained on various public datasets; while great efforts "
|
333 |
+
"have been taken to clean the pretraining data, it is possible that this model could generate lewd, "
|
334 |
+
"biased, or otherwise offensive outputs.",
|
335 |
+
elem_classes=["disclaimer"],
|
336 |
+
)
|
337 |
+
|
338 |
+
msg_submit_event = msg.submit(
|
339 |
+
# fn=conversation.user_turn,
|
340 |
+
fn=user,
|
341 |
+
inputs=[msg, chatbot],
|
342 |
+
outputs=[msg, chatbot],
|
343 |
+
queue=True,
|
344 |
+
show_progress="full",
|
345 |
+
# api_name=None,
|
346 |
+
).then(bot, chatbot, chatbot, queue=True)
|
347 |
+
submit_click_event = submit.click(
|
348 |
+
# fn=lambda x, y: ("",) + user(x, y)[1:], # clear msg
|
349 |
+
fn=user1, # clear msg
|
350 |
+
inputs=[msg, chatbot],
|
351 |
+
outputs=[msg, chatbot],
|
352 |
+
queue=True,
|
353 |
+
# queue=False,
|
354 |
+
show_progress="full",
|
355 |
+
# api_name=None,
|
356 |
+
).then(bot, chatbot, chatbot, queue=True)
|
357 |
+
stop.click(
|
358 |
+
fn=None,
|
359 |
+
inputs=None,
|
360 |
+
outputs=None,
|
361 |
+
cancels=[msg_submit_event, submit_click_event],
|
362 |
+
queue=False,
|
363 |
+
)
|
364 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
365 |
+
|
366 |
+
with gr.Accordion("For Chat/Translation API", open=False, visible=False):
|
367 |
+
input_text = gr.Text()
|
368 |
+
api_btn = gr.Button("Go", variant="primary")
|
369 |
+
out_text = gr.Text()
|
370 |
+
|
371 |
+
api_btn.click(
|
372 |
+
predict_api,
|
373 |
+
input_text,
|
374 |
+
out_text,
|
375 |
+
api_name="api",
|
376 |
+
)
|
377 |
+
|
378 |
+
# block.load(update_buff, [], buff, every=1)
|
379 |
+
# block.load(update_buff, [buff_var], [buff_var, buff], every=1)
|
380 |
+
|
381 |
+
# concurrency_count=5, max_size=20
|
382 |
+
# max_size=36, concurrency_count=14
|
383 |
+
# CPU cpu_count=2 16G, model 7G
|
384 |
+
# CPU UPGRADE cpu_count=8 32G, model 7G
|
385 |
+
|
386 |
+
# does not work
|
387 |
+
_ = """
|
388 |
+
# _ = int(psutil.virtual_memory().total / 10**9 // file_size - 1)
|
389 |
+
# concurrency_count = max(_, 1)
|
390 |
+
if psutil.cpu_count(logical=False) >= 8:
|
391 |
+
# concurrency_count = max(int(32 / file_size) - 1, 1)
|
392 |
+
else:
|
393 |
+
# concurrency_count = max(int(16 / file_size) - 1, 1)
|
394 |
+
# """
|
395 |
+
|
396 |
+
# default concurrency_count = 1
|
397 |
+
# block.queue(concurrency_count=concurrency_count, max_size=5).launch(debug=True)
|
398 |
+
|
399 |
+
server_port = 7860
|
400 |
+
if "forindo" in platform.node():
|
401 |
+
server_port = 7861
|
402 |
+
block.queue(max_size=5).launch(
|
403 |
+
debug=True, server_name="0.0.0.0", server_port=server_port
|
404 |
+
)
|
405 |
+
|
406 |
+
# block.queue(max_size=5).launch(debug=True, server_name="0.0.0.0")
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
ctransformers # ==0.2.10 0.2.13
|
2 |
+
transformers # ==4.30.2
|
3 |
+
# huggingface_hub
|
4 |
+
gradio
|
5 |
+
loguru
|
6 |
+
about-time
|
7 |
+
psutil
|
8 |
+
dl-hf-model
|
run-app.sh
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
nodemon -w app.py -x python app.py
|