Spaces:
Runtime error
Runtime error
Duplicate from LinkSoul/Chinese-Llama-2-7b
Browse filesCo-authored-by: Yemin <[email protected]>
- .gitattributes +35 -0
- LICENSE.txt +126 -0
- README.md +22 -0
- USE_POLICY.md +50 -0
- app.py +279 -0
- model.py +63 -0
- requirements.txt +9 -0
- style.css +16 -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
|
LICENSE.txt
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
LLAMA 2 COMMUNITY LICENSE AGREEMENT
|
2 |
+
Llama 2 Version Release Date: July 18, 2023
|
3 |
+
|
4 |
+
"Agreement" means the terms and conditions for use, reproduction, distribution and
|
5 |
+
modification of the Llama Materials set forth herein.
|
6 |
+
|
7 |
+
"Documentation" means the specifications, manuals and documentation
|
8 |
+
accompanying Llama 2 distributed by Meta at ai.meta.com/resources/models-and-
|
9 |
+
libraries/llama-downloads/.
|
10 |
+
|
11 |
+
"Licensee" or "you" means you, or your employer or any other person or entity (if
|
12 |
+
you are entering into this Agreement on such person or entity's behalf), of the age
|
13 |
+
required under applicable laws, rules or regulations to provide legal consent and that
|
14 |
+
has legal authority to bind your employer or such other person or entity if you are
|
15 |
+
entering in this Agreement on their behalf.
|
16 |
+
|
17 |
+
"Llama 2" means the foundational large language models and software and
|
18 |
+
algorithms, including machine-learning model code, trained model weights,
|
19 |
+
inference-enabling code, training-enabling code, fine-tuning enabling code and other
|
20 |
+
elements of the foregoing distributed by Meta at ai.meta.com/resources/models-and-
|
21 |
+
libraries/llama-downloads/.
|
22 |
+
|
23 |
+
"Llama Materials" means, collectively, Meta's proprietary Llama 2 and
|
24 |
+
Documentation (and any portion thereof) made available under this Agreement.
|
25 |
+
|
26 |
+
"Meta" or "we" means Meta Platforms Ireland Limited (if you are located in or, if you
|
27 |
+
are an entity, your principal place of business is in the EEA or Switzerland) and Meta
|
28 |
+
Platforms, Inc. (if you are located outside of the EEA or Switzerland).
|
29 |
+
|
30 |
+
By clicking "I Accept" below or by using or distributing any portion or element of the
|
31 |
+
Llama Materials, you agree to be bound by this Agreement.
|
32 |
+
|
33 |
+
1. License Rights and Redistribution.
|
34 |
+
|
35 |
+
a. Grant of Rights. You are granted a non-exclusive, worldwide, non-
|
36 |
+
transferable and royalty-free limited license under Meta's intellectual property or
|
37 |
+
other rights owned by Meta embodied in the Llama Materials to use, reproduce,
|
38 |
+
distribute, copy, create derivative works of, and make modifications to the Llama
|
39 |
+
Materials.
|
40 |
+
|
41 |
+
b. Redistribution and Use.
|
42 |
+
|
43 |
+
i. If you distribute or make the Llama Materials, or any derivative works
|
44 |
+
thereof, available to a third party, you shall provide a copy of this Agreement to such
|
45 |
+
third party.
|
46 |
+
ii. If you receive Llama Materials, or any derivative works thereof, from
|
47 |
+
a Licensee as part of an integrated end user product, then Section 2 of this
|
48 |
+
Agreement will not apply to you.
|
49 |
+
|
50 |
+
iii. You must retain in all copies of the Llama Materials that you
|
51 |
+
distribute the following attribution notice within a "Notice" text file distributed as a
|
52 |
+
part of such copies: "Llama 2 is licensed under the LLAMA 2 Community License,
|
53 |
+
Copyright (c) Meta Platforms, Inc. All Rights Reserved."
|
54 |
+
|
55 |
+
iv. Your use of the Llama Materials must comply with applicable laws
|
56 |
+
and regulations (including trade compliance laws and regulations) and adhere to the
|
57 |
+
Acceptable Use Policy for the Llama Materials (available at
|
58 |
+
https://ai.meta.com/llama/use-policy), which is hereby incorporated by reference into
|
59 |
+
this Agreement.
|
60 |
+
|
61 |
+
v. You will not use the Llama Materials or any output or results of the
|
62 |
+
Llama Materials to improve any other large language model (excluding Llama 2 or
|
63 |
+
derivative works thereof).
|
64 |
+
|
65 |
+
2. Additional Commercial Terms. If, on the Llama 2 version release date, the
|
66 |
+
monthly active users of the products or services made available by or for Licensee,
|
67 |
+
or Licensee's affiliates, is greater than 700 million monthly active users in the
|
68 |
+
preceding calendar month, you must request a license from Meta, which Meta may
|
69 |
+
grant to you in its sole discretion, and you are not authorized to exercise any of the
|
70 |
+
rights under this Agreement unless or until Meta otherwise expressly grants you
|
71 |
+
such rights.
|
72 |
+
|
73 |
+
3. Disclaimer of Warranty. UNLESS REQUIRED BY APPLICABLE LAW, THE
|
74 |
+
LLAMA MATERIALS AND ANY OUTPUT AND RESULTS THEREFROM ARE
|
75 |
+
PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
76 |
+
EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY
|
77 |
+
WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, OR
|
78 |
+
FITNESS FOR A PARTICULAR PURPOSE. YOU ARE SOLELY RESPONSIBLE
|
79 |
+
FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING
|
80 |
+
THE LLAMA MATERIALS AND ASSUME ANY RISKS ASSOCIATED WITH YOUR
|
81 |
+
USE OF THE LLAMA MATERIALS AND ANY OUTPUT AND RESULTS.
|
82 |
+
|
83 |
+
4. Limitation of Liability. IN NO EVENT WILL META OR ITS AFFILIATES BE
|
84 |
+
LIABLE UNDER ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT,
|
85 |
+
NEGLIGENCE, PRODUCTS LIABILITY, OR OTHERWISE, ARISING OUT OF THIS
|
86 |
+
AGREEMENT, FOR ANY LOST PROFITS OR ANY INDIRECT, SPECIAL,
|
87 |
+
CONSEQUENTIAL, INCIDENTAL, EXEMPLARY OR PUNITIVE DAMAGES, EVEN
|
88 |
+
IF META OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF
|
89 |
+
ANY OF THE FOREGOING.
|
90 |
+
|
91 |
+
5. Intellectual Property.
|
92 |
+
|
93 |
+
a. No trademark licenses are granted under this Agreement, and in
|
94 |
+
connection with the Llama Materials, neither Meta nor Licensee may use any name
|
95 |
+
or mark owned by or associated with the other or any of its affiliates, except as
|
96 |
+
required for reasonable and customary use in describing and redistributing the
|
97 |
+
Llama Materials.
|
98 |
+
|
99 |
+
b. Subject to Meta's ownership of Llama Materials and derivatives made by or
|
100 |
+
for Meta, with respect to any derivative works and modifications of the Llama
|
101 |
+
Materials that are made by you, as between you and Meta, you are and will be the
|
102 |
+
owner of such derivative works and modifications.
|
103 |
+
|
104 |
+
c. If you institute litigation or other proceedings against Meta or any entity
|
105 |
+
(including a cross-claim or counterclaim in a lawsuit) alleging that the Llama
|
106 |
+
Materials or Llama 2 outputs or results, or any portion of any of the foregoing,
|
107 |
+
constitutes infringement of intellectual property or other rights owned or licensable
|
108 |
+
by you, then any licenses granted to you under this Agreement shall terminate as of
|
109 |
+
the date such litigation or claim is filed or instituted. You will indemnify and hold
|
110 |
+
harmless Meta from and against any claim by any third party arising out of or related
|
111 |
+
to your use or distribution of the Llama Materials.
|
112 |
+
|
113 |
+
6. Term and Termination. The term of this Agreement will commence upon your
|
114 |
+
acceptance of this Agreement or access to the Llama Materials and will continue in
|
115 |
+
full force and effect until terminated in accordance with the terms and conditions
|
116 |
+
herein. Meta may terminate this Agreement if you are in breach of any term or
|
117 |
+
condition of this Agreement. Upon termination of this Agreement, you shall delete
|
118 |
+
and cease use of the Llama Materials. Sections 3, 4 and 7 shall survive the
|
119 |
+
termination of this Agreement.
|
120 |
+
|
121 |
+
7. Governing Law and Jurisdiction. This Agreement will be governed and
|
122 |
+
construed under the laws of the State of California without regard to choice of law
|
123 |
+
principles, and the UN Convention on Contracts for the International Sale of Goods
|
124 |
+
does not apply to this Agreement. The courts of California shall have exclusive
|
125 |
+
jurisdiction of any dispute arising out of this Agreement.
|
126 |
+
|
README.md
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Llama 2 7B Chat
|
3 |
+
emoji: 🏆
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: gray
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.37.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
suggested_hardware: a10g-small
|
11 |
+
duplicated_from: LinkSoul/Chinese-Llama-2-7b
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
15 |
+
|
16 |
+
# LLAMA v2 Models
|
17 |
+
|
18 |
+
Llama v2 was introduced in [this paper](https://arxiv.org/abs/2307.09288).
|
19 |
+
|
20 |
+
This Space demonstrates [Llama-2-7b-chat-hf](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat/blob/main/meta-llama/Llama-2-7b-chat-hf) from Meta. Please, check the original model card for details.
|
21 |
+
|
22 |
+
|
USE_POLICY.md
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Llama 2 Acceptable Use Policy
|
2 |
+
|
3 |
+
Meta is committed to promoting safe and fair use of its tools and features, including Llama 2. If you access or use Llama 2, you agree to this Acceptable Use Policy (“Policy”). The most recent copy of this policy can be found at [ai.meta.com/llama/use-policy](http://ai.meta.com/llama/use-policy).
|
4 |
+
|
5 |
+
## Prohibited Uses
|
6 |
+
We want everyone to use Llama 2 safely and responsibly. You agree you will not use, or allow others to use, Llama 2 to:
|
7 |
+
|
8 |
+
1. Violate the law or others’ rights, including to:
|
9 |
+
1. Engage in, promote, generate, contribute to, encourage, plan, incite, or further illegal or unlawful activity or content, such as:
|
10 |
+
1. Violence or terrorism
|
11 |
+
2. Exploitation or harm to children, including the solicitation, creation, acquisition, or dissemination of child exploitative content or failure to report Child Sexual Abuse Material
|
12 |
+
3. Human trafficking, exploitation, and sexual violence
|
13 |
+
4. The illegal distribution of information or materials to minors, including obscene materials, or failure to employ legally required age-gating in connection with such information or materials.
|
14 |
+
5. Sexual solicitation
|
15 |
+
6. Any other criminal activity
|
16 |
+
2. Engage in, promote, incite, or facilitate the harassment, abuse, threatening, or bullying of individuals or groups of individuals
|
17 |
+
3. Engage in, promote, incite, or facilitate discrimination or other unlawful or harmful conduct in the provision of employment, employment benefits, credit, housing, other economic benefits, or other essential goods and services
|
18 |
+
4. Engage in the unauthorized or unlicensed practice of any profession including, but not limited to, financial, legal, medical/health, or related professional practices
|
19 |
+
5. Collect, process, disclose, generate, or infer health, demographic, or other sensitive personal or private information about individuals without rights and consents required by applicable laws
|
20 |
+
6. Engage in or facilitate any action or generate any content that infringes, misappropriates, or otherwise violates any third-party rights, including the outputs or results of any products or services using the Llama 2 Materials
|
21 |
+
7. Create, generate, or facilitate the creation of malicious code, malware, computer viruses or do anything else that could disable, overburden, interfere with or impair the proper working, integrity, operation or appearance of a website or computer system
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
2. Engage in, promote, incite, facilitate, or assist in the planning or development of activities that present a risk of death or bodily harm to individuals, including use of Llama 2 related to the following:
|
26 |
+
1. Military, warfare, nuclear industries or applications, espionage, use for materials or activities that are subject to the International Traffic Arms Regulations (ITAR) maintained by the United States Department of State
|
27 |
+
2. Guns and illegal weapons (including weapon development)
|
28 |
+
3. Illegal drugs and regulated/controlled substances
|
29 |
+
4. Operation of critical infrastructure, transportation technologies, or heavy machinery
|
30 |
+
5. Self-harm or harm to others, including suicide, cutting, and eating disorders
|
31 |
+
6. Any content intended to incite or promote violence, abuse, or any infliction of bodily harm to an individual
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
3. Intentionally deceive or mislead others, including use of Llama 2 related to the following:
|
36 |
+
1. Generating, promoting, or furthering fraud or the creation or promotion of disinformation
|
37 |
+
2. Generating, promoting, or furthering defamatory content, including the creation of defamatory statements, images, or other content
|
38 |
+
3. Generating, promoting, or further distributing spam
|
39 |
+
4. Impersonating another individual without consent, authorization, or legal right
|
40 |
+
5. Representing that the use of Llama 2 or outputs are human-generated
|
41 |
+
6. Generating or facilitating false online engagement, including fake reviews and other means of fake online engagement
|
42 |
+
4. Fail to appropriately disclose to end users any known dangers of your AI system
|
43 |
+
|
44 |
+
Please report any violation of this Policy, software “bug,” or other problems that could lead to a violation of this Policy through one of the following means:
|
45 |
+
|
46 |
+
* Reporting issues with the model: [github.com/facebookresearch/llama](http://github.com/facebookresearch/llama)
|
47 |
+
* Reporting risky content generated by the model: [developers.facebook.com/llama_output_feedback](http://developers.facebook.com/llama_output_feedback)
|
48 |
+
* Reporting bugs and security concerns: [facebook.com/whitehat/info](http://facebook.com/whitehat/info)
|
49 |
+
* Reporting violations of the Acceptable Use Policy or unlicensed uses of Llama: [[email protected]](mailto:[email protected])
|
50 |
+
|
app.py
ADDED
@@ -0,0 +1,279 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Iterator
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from model import get_input_token_length, run
|
7 |
+
|
8 |
+
DEFAULT_SYSTEM_PROMPT = """\
|
9 |
+
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.
|
10 |
+
|
11 |
+
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.\
|
12 |
+
"""
|
13 |
+
MAX_MAX_NEW_TOKENS = 2048
|
14 |
+
DEFAULT_MAX_NEW_TOKENS = 1024
|
15 |
+
MAX_INPUT_TOKEN_LENGTH = 4000
|
16 |
+
|
17 |
+
DESCRIPTION = """
|
18 |
+
# Chinese Llama-2 7B Chat
|
19 |
+
|
20 |
+
本空间是对于[Chinese-Llama-2-7b-4bit](https://huggingface.co/LinkSoul/Chinese-Llama-2-7b-4bit)和[Chinese-Llama-2-7b](https://huggingface.co/LinkSoul/Chinese-Llama-2-7b)模型的演示。该模型由LinkSoul开发,基于Llama 2基础模型和自行收集的大规模中英文指令数据集[instruction_merge_set](https://huggingface.co/datasets/LinkSoul/instruction_merge_set)训练得到。
|
21 |
+
"""
|
22 |
+
|
23 |
+
LICENSE = """
|
24 |
+
<p/>
|
25 |
+
|
26 |
+
---
|
27 |
+
As a derivate work of [Chinese-Llama-2-7b](https://huggingface.co/LinkSoul/Chinese-Llama-2-7b) by LinkSoul and [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat) by Meta,
|
28 |
+
this demo is governed by the original [license](https://huggingface.co/spaces/LinkSoul/Chinese-Llama-2-7b/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/LinkSoul/Chinese-Llama-2-7b/blob/main/USE_POLICY.md).
|
29 |
+
"""
|
30 |
+
|
31 |
+
if not torch.cuda.is_available():
|
32 |
+
DESCRIPTION += '\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>'
|
33 |
+
|
34 |
+
|
35 |
+
def clear_and_save_textbox(message: str) -> tuple[str, str]:
|
36 |
+
return '', message
|
37 |
+
|
38 |
+
|
39 |
+
def display_input(message: str,
|
40 |
+
history: list[tuple[str, str]]) -> list[tuple[str, str]]:
|
41 |
+
history.append((message, ''))
|
42 |
+
return history
|
43 |
+
|
44 |
+
|
45 |
+
def delete_prev_fn(
|
46 |
+
history: list[tuple[str, str]]) -> tuple[list[tuple[str, str]], str]:
|
47 |
+
try:
|
48 |
+
message, _ = history.pop()
|
49 |
+
except IndexError:
|
50 |
+
message = ''
|
51 |
+
return history, message or ''
|
52 |
+
|
53 |
+
|
54 |
+
def generate(
|
55 |
+
message: str,
|
56 |
+
history_with_input: list[tuple[str, str]],
|
57 |
+
system_prompt: str,
|
58 |
+
max_new_tokens: int,
|
59 |
+
temperature: float,
|
60 |
+
top_p: float,
|
61 |
+
top_k: int,
|
62 |
+
) -> Iterator[list[tuple[str, str]]]:
|
63 |
+
if max_new_tokens > MAX_MAX_NEW_TOKENS:
|
64 |
+
raise ValueError
|
65 |
+
|
66 |
+
history = history_with_input[:-1]
|
67 |
+
generator = run(message, history, system_prompt, max_new_tokens, temperature, top_p, top_k)
|
68 |
+
try:
|
69 |
+
first_response = next(generator)
|
70 |
+
yield history + [(message, first_response)]
|
71 |
+
except StopIteration:
|
72 |
+
yield history + [(message, '')]
|
73 |
+
for response in generator:
|
74 |
+
yield history + [(message, response)]
|
75 |
+
|
76 |
+
|
77 |
+
def process_example(message: str) -> tuple[str, list[tuple[str, str]]]:
|
78 |
+
generator = generate(message, [], DEFAULT_SYSTEM_PROMPT, 1024, 1, 0.95, 50)
|
79 |
+
for x in generator:
|
80 |
+
pass
|
81 |
+
return '', x
|
82 |
+
|
83 |
+
|
84 |
+
def check_input_token_length(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> None:
|
85 |
+
input_token_length = get_input_token_length(message, chat_history, system_prompt)
|
86 |
+
if input_token_length > MAX_INPUT_TOKEN_LENGTH:
|
87 |
+
raise gr.Error(f'The accumulated input is too long ({input_token_length} > {MAX_INPUT_TOKEN_LENGTH}). Clear your chat history and try again.')
|
88 |
+
|
89 |
+
|
90 |
+
with gr.Blocks(css='style.css') as demo:
|
91 |
+
gr.Markdown(DESCRIPTION)
|
92 |
+
gr.DuplicateButton(value='Duplicate Space for private use',
|
93 |
+
elem_id='duplicate-button')
|
94 |
+
|
95 |
+
with gr.Group():
|
96 |
+
chatbot = gr.Chatbot(label='Chatbot')
|
97 |
+
with gr.Row():
|
98 |
+
textbox = gr.Textbox(
|
99 |
+
container=False,
|
100 |
+
show_label=False,
|
101 |
+
placeholder='Type a message...',
|
102 |
+
scale=10,
|
103 |
+
)
|
104 |
+
submit_button = gr.Button('Submit',
|
105 |
+
variant='primary',
|
106 |
+
scale=1,
|
107 |
+
min_width=0)
|
108 |
+
with gr.Row():
|
109 |
+
retry_button = gr.Button('🔄 Retry', variant='secondary')
|
110 |
+
undo_button = gr.Button('↩️ Undo', variant='secondary')
|
111 |
+
clear_button = gr.Button('🗑️ Clear', variant='secondary')
|
112 |
+
|
113 |
+
saved_input = gr.State()
|
114 |
+
|
115 |
+
with gr.Accordion(label='Advanced options', open=False):
|
116 |
+
system_prompt = gr.Textbox(label='System prompt',
|
117 |
+
value=DEFAULT_SYSTEM_PROMPT,
|
118 |
+
lines=6)
|
119 |
+
max_new_tokens = gr.Slider(
|
120 |
+
label='Max new tokens',
|
121 |
+
minimum=1,
|
122 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
123 |
+
step=1,
|
124 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
125 |
+
)
|
126 |
+
temperature = gr.Slider(
|
127 |
+
label='Temperature',
|
128 |
+
minimum=0.1,
|
129 |
+
maximum=4.0,
|
130 |
+
step=0.1,
|
131 |
+
value=1.0,
|
132 |
+
)
|
133 |
+
top_p = gr.Slider(
|
134 |
+
label='Top-p (nucleus sampling)',
|
135 |
+
minimum=0.05,
|
136 |
+
maximum=1.0,
|
137 |
+
step=0.05,
|
138 |
+
value=0.95,
|
139 |
+
)
|
140 |
+
top_k = gr.Slider(
|
141 |
+
label='Top-k',
|
142 |
+
minimum=1,
|
143 |
+
maximum=1000,
|
144 |
+
step=1,
|
145 |
+
value=50,
|
146 |
+
)
|
147 |
+
|
148 |
+
gr.Examples(
|
149 |
+
examples=[
|
150 |
+
'用中文回答,When is the best time to visit Beijing, and do you have any suggestions for me?',
|
151 |
+
'用英文回答,特朗普是谁?',
|
152 |
+
'Hello there! How are you doing?',
|
153 |
+
'Can you explain briefly to me what is the Python programming language?',
|
154 |
+
'Explain the plot of Cinderella in a sentence.',
|
155 |
+
'How many hours does it take a man to eat a Helicopter?',
|
156 |
+
"Write a 100-word article on 'Benefits of Open-Source in AI research'",
|
157 |
+
],
|
158 |
+
inputs=textbox,
|
159 |
+
outputs=[textbox, chatbot],
|
160 |
+
fn=process_example,
|
161 |
+
cache_examples=True,
|
162 |
+
)
|
163 |
+
|
164 |
+
gr.Markdown(LICENSE)
|
165 |
+
|
166 |
+
textbox.submit(
|
167 |
+
fn=clear_and_save_textbox,
|
168 |
+
inputs=textbox,
|
169 |
+
outputs=[textbox, saved_input],
|
170 |
+
api_name=False,
|
171 |
+
queue=False,
|
172 |
+
).then(
|
173 |
+
fn=display_input,
|
174 |
+
inputs=[saved_input, chatbot],
|
175 |
+
outputs=chatbot,
|
176 |
+
api_name=False,
|
177 |
+
queue=False,
|
178 |
+
).then(
|
179 |
+
fn=check_input_token_length,
|
180 |
+
inputs=[saved_input, chatbot, system_prompt],
|
181 |
+
api_name=False,
|
182 |
+
queue=False,
|
183 |
+
).success(
|
184 |
+
fn=generate,
|
185 |
+
inputs=[
|
186 |
+
saved_input,
|
187 |
+
chatbot,
|
188 |
+
system_prompt,
|
189 |
+
max_new_tokens,
|
190 |
+
temperature,
|
191 |
+
top_p,
|
192 |
+
top_k,
|
193 |
+
],
|
194 |
+
outputs=chatbot,
|
195 |
+
api_name=False,
|
196 |
+
)
|
197 |
+
|
198 |
+
button_event_preprocess = submit_button.click(
|
199 |
+
fn=clear_and_save_textbox,
|
200 |
+
inputs=textbox,
|
201 |
+
outputs=[textbox, saved_input],
|
202 |
+
api_name=False,
|
203 |
+
queue=False,
|
204 |
+
).then(
|
205 |
+
fn=display_input,
|
206 |
+
inputs=[saved_input, chatbot],
|
207 |
+
outputs=chatbot,
|
208 |
+
api_name=False,
|
209 |
+
queue=False,
|
210 |
+
).then(
|
211 |
+
fn=check_input_token_length,
|
212 |
+
inputs=[saved_input, chatbot, system_prompt],
|
213 |
+
api_name=False,
|
214 |
+
queue=False,
|
215 |
+
).success(
|
216 |
+
fn=generate,
|
217 |
+
inputs=[
|
218 |
+
saved_input,
|
219 |
+
chatbot,
|
220 |
+
system_prompt,
|
221 |
+
max_new_tokens,
|
222 |
+
temperature,
|
223 |
+
top_p,
|
224 |
+
top_k,
|
225 |
+
],
|
226 |
+
outputs=chatbot,
|
227 |
+
api_name=False,
|
228 |
+
)
|
229 |
+
|
230 |
+
retry_button.click(
|
231 |
+
fn=delete_prev_fn,
|
232 |
+
inputs=chatbot,
|
233 |
+
outputs=[chatbot, saved_input],
|
234 |
+
api_name=False,
|
235 |
+
queue=False,
|
236 |
+
).then(
|
237 |
+
fn=display_input,
|
238 |
+
inputs=[saved_input, chatbot],
|
239 |
+
outputs=chatbot,
|
240 |
+
api_name=False,
|
241 |
+
queue=False,
|
242 |
+
).then(
|
243 |
+
fn=generate,
|
244 |
+
inputs=[
|
245 |
+
saved_input,
|
246 |
+
chatbot,
|
247 |
+
system_prompt,
|
248 |
+
max_new_tokens,
|
249 |
+
temperature,
|
250 |
+
top_p,
|
251 |
+
top_k,
|
252 |
+
],
|
253 |
+
outputs=chatbot,
|
254 |
+
api_name=False,
|
255 |
+
)
|
256 |
+
|
257 |
+
undo_button.click(
|
258 |
+
|
259 |
+
fn=delete_prev_fn,
|
260 |
+
inputs=chatbot,
|
261 |
+
outputs=[chatbot, saved_input],
|
262 |
+
api_name=False,
|
263 |
+
queue=False,
|
264 |
+
).then(
|
265 |
+
fn=lambda x: x,
|
266 |
+
inputs=[saved_input],
|
267 |
+
outputs=textbox,
|
268 |
+
api_name=False,
|
269 |
+
queue=False,
|
270 |
+
)
|
271 |
+
|
272 |
+
clear_button.click(
|
273 |
+
fn=lambda: ([], ''),
|
274 |
+
outputs=[chatbot, saved_input],
|
275 |
+
queue=False,
|
276 |
+
api_name=False,
|
277 |
+
)
|
278 |
+
|
279 |
+
demo.queue(max_size=20).launch()
|
model.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Iterator
|
2 |
+
from llama_cpp import Llama
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
|
5 |
+
|
6 |
+
def download_model():
|
7 |
+
# See https://github.com/OpenAccess-AI-Collective/ggml-webui/blob/main/tabbed.py
|
8 |
+
# https://huggingface.co/spaces/kat33/llama.cpp/blob/main/app.py
|
9 |
+
print(f"Downloading model: {model_repo}/{model_filename}")
|
10 |
+
file = hf_hub_download(
|
11 |
+
repo_id=model_repo, filename=model_filename
|
12 |
+
)
|
13 |
+
print("Downloaded " + file)
|
14 |
+
return file
|
15 |
+
|
16 |
+
model_repo = "LinkSoul/Chinese-Llama-2-7b-ggml"
|
17 |
+
model_filename = "Chinese-Llama-2-7b.ggmlv3.q4_0.bin"
|
18 |
+
# model_filename = "Chinese-Llama-2-7b.ggmlv3.q8_0.bin"
|
19 |
+
model_path = download_model()
|
20 |
+
|
21 |
+
# load Llama-2
|
22 |
+
llm = Llama(model_path=model_path, n_ctx=4000, verbose=False)
|
23 |
+
|
24 |
+
|
25 |
+
def get_prompt(message: str, chat_history: list[tuple[str, str]],
|
26 |
+
system_prompt: str) -> str:
|
27 |
+
texts = [f'[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n']
|
28 |
+
for user_input, response in chat_history:
|
29 |
+
texts.append(f'{user_input.strip()} [/INST] {response.strip()} </s><s> [INST] ')
|
30 |
+
texts.append(f'{message.strip()} [/INST]')
|
31 |
+
return ''.join(texts)
|
32 |
+
|
33 |
+
def generate(prompt, max_new_tokens, temperature, top_p, top_k):
|
34 |
+
return llm(prompt,
|
35 |
+
max_tokens=max_new_tokens,
|
36 |
+
stop=["</s>"],
|
37 |
+
temperature=temperature,
|
38 |
+
top_p=top_p,
|
39 |
+
top_k=top_k,
|
40 |
+
stream=False)
|
41 |
+
|
42 |
+
|
43 |
+
def get_input_token_length(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> int:
|
44 |
+
prompt = get_prompt(message, chat_history, system_prompt)
|
45 |
+
input_ids = llm.tokenize(prompt.encode('utf-8'))
|
46 |
+
return len(input_ids)
|
47 |
+
|
48 |
+
|
49 |
+
def run(message: str,
|
50 |
+
chat_history: list[tuple[str, str]],
|
51 |
+
system_prompt: str,
|
52 |
+
max_new_tokens: int = 1024,
|
53 |
+
temperature: float = 0.8,
|
54 |
+
top_p: float = 0.95,
|
55 |
+
top_k: int = 50) -> Iterator[str]:
|
56 |
+
prompt = get_prompt(message, chat_history, system_prompt)
|
57 |
+
output = generate(prompt, max_new_tokens, temperature, top_p, top_k)
|
58 |
+
yield output['choices'][0]['text']
|
59 |
+
|
60 |
+
# outputs = []
|
61 |
+
# for resp in streamer:
|
62 |
+
# outputs.append(resp['choices'][0]['text'])
|
63 |
+
# yield ''.join(outputs)
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate==0.21.0
|
2 |
+
bitsandbytes==0.40.2
|
3 |
+
gradio==3.37.0
|
4 |
+
protobuf==3.20.3
|
5 |
+
scipy==1.11.1
|
6 |
+
sentencepiece==0.1.99
|
7 |
+
torch==2.0.1
|
8 |
+
transformers==4.31.0
|
9 |
+
llama-cpp-python
|
style.css
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
h1 {
|
2 |
+
text-align: center;
|
3 |
+
}
|
4 |
+
|
5 |
+
#duplicate-button {
|
6 |
+
margin: auto;
|
7 |
+
color: white;
|
8 |
+
background: #1565c0;
|
9 |
+
border-radius: 100vh;
|
10 |
+
}
|
11 |
+
|
12 |
+
#component-0 {
|
13 |
+
max-width: 900px;
|
14 |
+
margin: auto;
|
15 |
+
padding-top: 1.5rem;
|
16 |
+
}
|