Spaces:
Runtime error
Runtime error
ari9dam
commited on
Commit
•
11250e9
1
Parent(s):
2c6e1bb
adding app file
Browse files- .ipynb_checkpoints/app-checkpoint.py +112 -0
- LICENSE +63 -0
- app.py +112 -0
- requirements.txt +82 -0
- style.css +16 -0
.ipynb_checkpoints/app-checkpoint.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from threading import Thread
|
3 |
+
from typing import Iterator
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import torch
|
7 |
+
import transformers
|
8 |
+
from transformers import TextIteratorStreamer
|
9 |
+
|
10 |
+
MAX_MAX_NEW_TOKENS = 2048
|
11 |
+
DEFAULT_MAX_NEW_TOKENS = 1024
|
12 |
+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
13 |
+
|
14 |
+
model_id = "microsoft/Orca-2-13b"
|
15 |
+
model = transformers.AutoModelForCausalLM.from_pretrained(model_id, device_map='auto')
|
16 |
+
|
17 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(model_id, use_fast=False)
|
18 |
+
|
19 |
+
system_message = "You are Orca, an AI language model created by Microsoft. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and you follow ethical guidelines and promote positive behavior."
|
20 |
+
user_message = "How can you determine if a restaurant is popular among locals or mainly attracts tourists, and why might this information be useful?"
|
21 |
+
|
22 |
+
DESCRIPTION = """
|
23 |
+
# Orca-2 13B
|
24 |
+
This Space demonstrates model [Orca-2-13B](https://huggingface.co/microsoft/Orca-2-13B) by Microsoft, a Llama 2 derivate model with 13B parameters fine-tuned for sigle turn instructions. This space is running on Inference Endpoints using text-generation-inference library. If you want to run your own service, you can also [deploy the model on Inference Endpoints](https://ui.endpoints.huggingface.co/).
|
25 |
+
|
26 |
+
The system message is set to be the cautious system message:
|
27 |
+
You are Orca, an AI language model created by Microsoft. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and you follow ethical guidelines and promote positive behavior.
|
28 |
+
Feel free to modify it in the additional input section. The demo uses greedy decoding.
|
29 |
+
|
30 |
+
🔎 For more details about the Orca family of models take a look [at our blog post](https://msft.it/6042iGtzK).
|
31 |
+
🔨 Looking for lighter versions of Orca-2? 🐇 Check out the [7B Chat model](https://huggingface.co/spaces/huggingface-projects/Orca-2-7b). Note: Orca 2 is licensed under the [Microsoft Research License](LICENSE). Llama 2 is licensed under the [LLAMA 2 Community License](https://ai.meta.com/llama/license/).
|
32 |
+
"""
|
33 |
+
|
34 |
+
# Function to combine system message and user
|
35 |
+
def to_prompt(conversations):
|
36 |
+
text = ""
|
37 |
+
for message in conversations:
|
38 |
+
if message['role']!="assistant":
|
39 |
+
text += f"<|im_start|>{message['role']}\n{message['content']}<|im_end|>\n"
|
40 |
+
else:
|
41 |
+
text += f"<|im_start|>{message['role']}\n{message['content']}{tokenizer.eos_token}\n"
|
42 |
+
prompt = text + "<|im_start|>assistant\n"
|
43 |
+
inputs = tokenizer(prompt, return_tensors='pt').input_ids
|
44 |
+
return inputs
|
45 |
+
|
46 |
+
|
47 |
+
def generate(
|
48 |
+
message: str,
|
49 |
+
chat_history: list[tuple[str, str]],
|
50 |
+
system_prompt: str,
|
51 |
+
max_new_tokens: int = 1024,
|
52 |
+
temperature: float = 0.6,
|
53 |
+
top_p: float = 0.9,
|
54 |
+
) -> Iterator[str]:
|
55 |
+
conversation = []
|
56 |
+
if system_prompt:
|
57 |
+
conversation.append({"role": "system", "content": system_prompt.strip()})
|
58 |
+
else:
|
59 |
+
conversation.append({"role": "system", "content": ""})
|
60 |
+
for user, assistant in chat_history:
|
61 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
62 |
+
conversation.append({"role": "user", "content": message})
|
63 |
+
|
64 |
+
input_ids = to_prompt(conversation)
|
65 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
66 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
67 |
+
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
68 |
+
input_ids = input_ids.to(model.device)
|
69 |
+
|
70 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
71 |
+
generate_kwargs = dict(
|
72 |
+
{"input_ids": input_ids},
|
73 |
+
streamer=streamer,
|
74 |
+
max_new_tokens=max_new_tokens,
|
75 |
+
do_sample=False,
|
76 |
+
)
|
77 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
78 |
+
t.start()
|
79 |
+
|
80 |
+
outputs = []
|
81 |
+
for text in streamer:
|
82 |
+
outputs.append(text)
|
83 |
+
yield "".join(outputs)
|
84 |
+
|
85 |
+
|
86 |
+
chat_interface = gr.ChatInterface(
|
87 |
+
fn=generate,
|
88 |
+
additional_inputs=[
|
89 |
+
gr.Textbox(label="System prompt", lines=6, value=system_message),
|
90 |
+
gr.Slider(
|
91 |
+
label="Max new tokens",
|
92 |
+
minimum=1,
|
93 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
94 |
+
step=1,
|
95 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
96 |
+
),
|
97 |
+
],
|
98 |
+
stop_btn=None,
|
99 |
+
examples=[
|
100 |
+
["How can you determine if a restaurant is popular among locals or mainly attracts tourists, and why might this information be useful?"],
|
101 |
+
["The eighth-grade class held a bake-off. Kelsie made two times more cookies than Josh. Josh made one-fourth the number of cookies that Suzanne made. If Suzanne made 36 cookies, how many did Kelsie make?"],
|
102 |
+
["Read the following web search snippets carefully and then answer the question below:\nWashington state remains near the top of the list for the most expensive average. According to the AAA, the current average price for a gallon of gas in Washington state is $5.01.\nToday's average price of gas in the U.S. is $3.82 per gallon, unchanged from yesterday, down $0.01 from last week and down $0.02 from last month.\n\nAnswer the following question:\n\nHow does the gas price in Washington compare to the national average? and what is the exact difference?"],
|
103 |
+
["The ages of New Havens residents are 25.4% under the age of 18, 16.4% from 18 to 24, 31.2% from 25 to 44, 16.7% from 45 to 64, and 10.2% who were 65 years of age or older. The median age is 29 years, which is significantly lower than the national average. There are 91.8 males per 100 females. For every 100 females age 18 and over, there are 87.6 males.\n\nWhich gender group is larger: females or males?"],
|
104 |
+
],
|
105 |
+
)
|
106 |
+
|
107 |
+
with gr.Blocks(css="style.css") as demo:
|
108 |
+
gr.Markdown(DESCRIPTION)
|
109 |
+
chat_interface.render()
|
110 |
+
|
111 |
+
if __name__ == "__main__":
|
112 |
+
demo.queue(max_size=20).launch()
|
LICENSE
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MICROSOFT RESEARCH LICENSE TERMS
|
2 |
+
|
3 |
+
IF YOU LIVE IN THE UNITED STATES, PLEASE READ THE “BINDING ARBITRATION AND CLASS ACTION WAIVER” SECTION BELOW. IT AFFECTS HOW DISPUTES ARE RESOLVED.
|
4 |
+
|
5 |
+
These license terms are an agreement between you and Microsoft Corporation (or one of its affiliates). They apply to the source code, object code, machine learning models, or data (collectively “Materials”) that accompany this license. IF YOU COMPLY WITH THESE LICENSE TERMS, YOU HAVE THE RIGHTS BELOW. BY USING THE MATERIALS, YOU ACCEPT THESE TERMS.
|
6 |
+
|
7 |
+
1) INSTALLATION AND USE RIGHTS TO THE MATERIALS.
|
8 |
+
|
9 |
+
Subject to the terms of this agreement, you have the below rights, if applicable, to use the Materials solely for non-commercial, non-revenue generating, research purposes:
|
10 |
+
|
11 |
+
a) Source Code. If source code is included, you may use and modify the source code, but you may not distribute the source code.
|
12 |
+
b) Object Code. If object code is included, you may use the object code, but you may not distribute the object code.
|
13 |
+
c) Models. If machine learning model(s) are included, you may use the model(s), but you may not distribute the models.
|
14 |
+
d) Data. If data is included, you may use and modify the data, but your use and modification must be consistent with the consent under which the data was provided and/or gathered and you may not distribute the data or your modifications to the data.
|
15 |
+
|
16 |
+
2) SCOPE OF LICENSE. The Materials are licensed, not sold. Microsoft reserves all other rights. Unless applicable law gives you more rights despite this limitation, you will not (and have no right to):
|
17 |
+
|
18 |
+
a) work around any technical limitations in the Materials that only allow you to use it in certain ways;
|
19 |
+
b) reverse engineer, decompile or disassemble the Materials;
|
20 |
+
c) remove, minimize, block, or modify any notices of Microsoft or its suppliers in the Materials;
|
21 |
+
d) use the Materials in any way that is against the law or to create or propagate malware; or
|
22 |
+
e) share, publish, distribute or lend the Materials, provide the Materials as a stand-alone hosted solution for others to use, or transfer the Materials or this agreement to any third party.
|
23 |
+
|
24 |
+
3) PERSONAL DATA. If the data (set forth in Section 1(c) above) includes or is found to include any data that enables any ability to identify an individual (“Personal Data”), you will not use such Personal Data for any purpose other than was authorized and consented to by the data subject/research participant. You will not use Personal Data to contact any person. You will keep Personal Data in strict confidence. You will not share any Personal Data that is collected or in your possession with any third party for any reason and as required under the original consent agreement. Further, you will destroy the Personal Data and any backup or copies, immediately upon the completion of your research.
|
25 |
+
|
26 |
+
4) LICENSE TO MICROSOFT. Notwithstanding the limitations in Section 1, you may distribute your modifications back to Microsoft, and if you do provide Microsoft with modifications of the Materials, you hereby grant Microsoft, without any restrictions or limitations, a non-exclusive, perpetual, irrevocable, royalty-free, assignable and sub-licensable license, to reproduce, publicly perform or display, install, use, modify, post, distribute, make and have made, sell and transfer such modifications and derivatives for any purpose.
|
27 |
+
|
28 |
+
5) PUBLICATION. You may publish (or present papers or articles) on your results from using the Materials provided that no material or substantial portion of the Materials is included in any such publication or presentation.
|
29 |
+
|
30 |
+
6) FEEDBACK. Any feedback about the Materials provided by you to us is voluntarily given, and Microsoft shall be free to use the feedback as it sees fit without obligation or restriction of any kind, even if the feedback is designated by you as confidential. Such feedback shall be considered a contribution and licensed to Microsoft under the terms of Section 4 above.
|
31 |
+
|
32 |
+
7) COMPLIANCE WITH TRADE LAWS. You acknowledge that the Materials may be subject to applicable trade laws in one or more countries. You will comply with all relevant laws and regulations applicable to the import or export of the Materials, including but not limited to, trade laws such as the U.S. Export Administration Regulations or other end-user, end use, and destination restrictions by the U.S. and other governments, as well as sanctions regulations administered by the U.S. Office of Foreign Assets Control. Microsoft may suspend or terminate the agreement immediately to the extent that Microsoft reasonably concludes that continued performance would violate trade laws or put it at risk of becoming subject to sanctions or penalties under trade laws. For additional information, see www.microsoft.com/exporting.
|
33 |
+
|
34 |
+
8) SUPPORT SERVICES. Microsoft is not obligated under this agreement to provide any support services for the Materials. Any support provided is “as is”, “with all faults”, and without warranty of any kind.
|
35 |
+
|
36 |
+
9) BINDING ARBITRATION AND CLASS ACTION WAIVER. This Section applies if you live in (or, if a business, your principal place of business is in) the United States. If you and Microsoft have a dispute, you and Microsoft agree to try for 60 days to resolve it informally. If you and Microsoft can’t, you and Microsoft agree to binding individual arbitration before the American Arbitration Association under the Federal Arbitration Act (“FAA”), and not to sue in court in front of a judge or jury. Instead, a neutral arbitrator will decide. Class action lawsuits, class-wide arbitrations, private attorney-general actions, and any other proceeding where someone acts in a representative capacity are not allowed; nor is combining individual proceedings without the consent of all parties. The complete Arbitration Agreement contains more terms and is at aka.ms/arb-agreement-1. You and Microsoft agree to these terms.
|
37 |
+
|
38 |
+
10) ENTIRE AGREEMENT. This agreement, and any other terms Microsoft may provide for supplements, updates, or third-party applications, is the entire agreement for the Materials.
|
39 |
+
|
40 |
+
11) APPLICABLE LAW AND PLACE TO RESOLVE DISPUTES. If you acquired the Materials in the United States or Canada, the laws of the state or province where you live (or, if a business, where your principal place of business is located) govern the interpretation of this agreement, claims for its breach, and all other claims (including consumer protection, unfair competition, and tort claims), regardless of conflict of laws principles, except that the FAA governs everything related to arbitration. If you acquired the Materials in any other country, its laws apply, except that the FAA governs everything related to arbitration. If U.S. federal jurisdiction exists, you and Microsoft consent to exclusive jurisdiction and venue in the federal court in King County, Washington for all disputes heard in court (excluding arbitration). If not, you and Microsoft consent to exclusive jurisdiction and venue in the Superior Court of King County, Washington for all disputes heard in court (excluding arbitration).
|
41 |
+
|
42 |
+
12) CONSUMER RIGHTS; REGIONAL VARIATIONS. This agreement describes certain legal rights. You may have other rights, including consumer rights, under the laws of your state, province, or country. Separate and apart from your relationship with Microsoft, you may also have rights with respect to the party from which you acquired the Materials. This agreement does not change those other rights if the laws of your state, province, or country do not permit it to do so. For example, if you acquired the Materials in one of the below regions, or mandatory country law applies, then the following provisions apply to you:
|
43 |
+
|
44 |
+
a) Australia. You have statutory guarantees under the Australian Consumer Law and nothing in this agreement is intended to affect those rights.
|
45 |
+
|
46 |
+
b) Canada. If you acquired this software in Canada, you may stop receiving updates by turning off the automatic update feature, disconnecting your device from the Internet (if and when you re-connect to the Internet, however, the Materials will resume checking for and installing updates), or uninstalling the Materials. The product documentation, if any, may also specify how to turn off updates for your specific device or software.
|
47 |
+
|
48 |
+
c) Germany and Austria.
|
49 |
+
|
50 |
+
i. Warranty. The properly licensed software will perform substantially as described in any Microsoft materials that accompany the Materials. However, Microsoft gives no contractual guarantee in relation to the licensed software.
|
51 |
+
|
52 |
+
ii. Limitation of Liability. In case of intentional conduct, gross negligence, claims based on the Product Liability Act, as well as, in case of death or personal or physical injury, Microsoft is liable according to the statutory law.
|
53 |
+
|
54 |
+
Subject to the foregoing clause (ii), Microsoft will only be liable for slight negligence if Microsoft is in breach of such material contractual obligations, the fulfillment of which facilitate the due performance of this agreement, the breach of which would endanger the purpose of this agreement and the compliance with which a party may constantly trust in (so-called "cardinal obligations"). In other cases of slight negligence, Microsoft will not be liable for slight negligence.
|
55 |
+
|
56 |
+
13) DISCLAIMER OF WARRANTY. THE MATERIALS ARE LICENSED “AS IS.” YOU BEAR THE RISK OF USING THEM. MICROSOFT GIVES NO EXPRESS WARRANTIES, GUARANTEES, OR CONDITIONS. TO THE EXTENT PERMITTED UNDER APPLICABLE LAWS, MICROSOFT EXCLUDES ALL IMPLIED WARRANTIES, INCLUDING MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
|
57 |
+
|
58 |
+
14) LIMITATION ON AND EXCLUSION OF DAMAGES. IF YOU HAVE ANY BASIS FOR RECOVERING DAMAGES DESPITE THE PRECEDING DISCLAIMER OF WARRANTY, YOU CAN RECOVER FROM MICROSOFT AND ITS SUPPLIERS ONLY DIRECT DAMAGES UP TO U.S. $5.00. YOU CANNOT RECOVER ANY OTHER DAMAGES, INCLUDING CONSEQUENTIAL, LOST PROFITS, SPECIAL, INDIRECT OR INCIDENTAL DAMAGES.
|
59 |
+
|
60 |
+
This limitation applies to (a) anything related to the Materials, services, content (including code) on third party Internet sites, or third party applications; and (b) claims for breach of contract, warranty, guarantee, or condition; strict liability, negligence, or other tort; or any other claim; in each case to the extent permitted by applicable law.
|
61 |
+
|
62 |
+
It also applies even if Microsoft knew or should have known about the possibility of the damages. The above limitation or exclusion may not apply to you because your state, province, or country may not allow the exclusion or limitation of incidental, consequential, or other damages.
|
63 |
+
|
app.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from threading import Thread
|
3 |
+
from typing import Iterator
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import torch
|
7 |
+
import transformers
|
8 |
+
from transformers import TextIteratorStreamer
|
9 |
+
|
10 |
+
MAX_MAX_NEW_TOKENS = 2048
|
11 |
+
DEFAULT_MAX_NEW_TOKENS = 1024
|
12 |
+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
13 |
+
|
14 |
+
model_id = "microsoft/Orca-2-13b"
|
15 |
+
model = transformers.AutoModelForCausalLM.from_pretrained(model_id, device_map='auto')
|
16 |
+
|
17 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(model_id, use_fast=False)
|
18 |
+
|
19 |
+
system_message = "You are Orca, an AI language model created by Microsoft. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and you follow ethical guidelines and promote positive behavior."
|
20 |
+
user_message = "How can you determine if a restaurant is popular among locals or mainly attracts tourists, and why might this information be useful?"
|
21 |
+
|
22 |
+
DESCRIPTION = """
|
23 |
+
# Orca-2 13B
|
24 |
+
This Space demonstrates model [Orca-2-13B](https://huggingface.co/microsoft/Orca-2-13B) by Microsoft, a Llama 2 derivate model with 13B parameters fine-tuned for sigle turn instructions. This space is running on Inference Endpoints using text-generation-inference library. If you want to run your own service, you can also [deploy the model on Inference Endpoints](https://ui.endpoints.huggingface.co/).
|
25 |
+
|
26 |
+
The system message is set to be the cautious system message:
|
27 |
+
You are Orca, an AI language model created by Microsoft. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and you follow ethical guidelines and promote positive behavior.
|
28 |
+
Feel free to modify it in the additional input section. The demo uses greedy decoding.
|
29 |
+
|
30 |
+
🔎 For more details about the Orca family of models take a look [at our blog post](https://msft.it/6042iGtzK).
|
31 |
+
🔨 Looking for lighter versions of Orca-2? 🐇 Check out the [7B Chat model](https://huggingface.co/spaces/huggingface-projects/Orca-2-7b). Note: Orca 2 is licensed under the [Microsoft Research License](LICENSE). Llama 2 is licensed under the [LLAMA 2 Community License](https://ai.meta.com/llama/license/).
|
32 |
+
"""
|
33 |
+
|
34 |
+
# Function to combine system message and user
|
35 |
+
def to_prompt(conversations):
|
36 |
+
text = ""
|
37 |
+
for message in conversations:
|
38 |
+
if message['role']!="assistant":
|
39 |
+
text += f"<|im_start|>{message['role']}\n{message['content']}<|im_end|>\n"
|
40 |
+
else:
|
41 |
+
text += f"<|im_start|>{message['role']}\n{message['content']}{tokenizer.eos_token}\n"
|
42 |
+
prompt = text + "<|im_start|>assistant\n"
|
43 |
+
inputs = tokenizer(prompt, return_tensors='pt').input_ids
|
44 |
+
return inputs
|
45 |
+
|
46 |
+
|
47 |
+
def generate(
|
48 |
+
message: str,
|
49 |
+
chat_history: list[tuple[str, str]],
|
50 |
+
system_prompt: str,
|
51 |
+
max_new_tokens: int = 1024,
|
52 |
+
temperature: float = 0.6,
|
53 |
+
top_p: float = 0.9,
|
54 |
+
) -> Iterator[str]:
|
55 |
+
conversation = []
|
56 |
+
if system_prompt:
|
57 |
+
conversation.append({"role": "system", "content": system_prompt.strip()})
|
58 |
+
else:
|
59 |
+
conversation.append({"role": "system", "content": ""})
|
60 |
+
for user, assistant in chat_history:
|
61 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
62 |
+
conversation.append({"role": "user", "content": message})
|
63 |
+
|
64 |
+
input_ids = to_prompt(conversation)
|
65 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
66 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
67 |
+
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
68 |
+
input_ids = input_ids.to(model.device)
|
69 |
+
|
70 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
71 |
+
generate_kwargs = dict(
|
72 |
+
{"input_ids": input_ids},
|
73 |
+
streamer=streamer,
|
74 |
+
max_new_tokens=max_new_tokens,
|
75 |
+
do_sample=False,
|
76 |
+
)
|
77 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
78 |
+
t.start()
|
79 |
+
|
80 |
+
outputs = []
|
81 |
+
for text in streamer:
|
82 |
+
outputs.append(text)
|
83 |
+
yield "".join(outputs)
|
84 |
+
|
85 |
+
|
86 |
+
chat_interface = gr.ChatInterface(
|
87 |
+
fn=generate,
|
88 |
+
additional_inputs=[
|
89 |
+
gr.Textbox(label="System prompt", lines=6, value=system_message),
|
90 |
+
gr.Slider(
|
91 |
+
label="Max new tokens",
|
92 |
+
minimum=1,
|
93 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
94 |
+
step=1,
|
95 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
96 |
+
),
|
97 |
+
],
|
98 |
+
stop_btn=None,
|
99 |
+
examples=[
|
100 |
+
["How can you determine if a restaurant is popular among locals or mainly attracts tourists, and why might this information be useful?"],
|
101 |
+
["The eighth-grade class held a bake-off. Kelsie made two times more cookies than Josh. Josh made one-fourth the number of cookies that Suzanne made. If Suzanne made 36 cookies, how many did Kelsie make?"],
|
102 |
+
["Read the following web search snippets carefully and then answer the question below:\nWashington state remains near the top of the list for the most expensive average. According to the AAA, the current average price for a gallon of gas in Washington state is $5.01.\nToday's average price of gas in the U.S. is $3.82 per gallon, unchanged from yesterday, down $0.01 from last week and down $0.02 from last month.\n\nAnswer the following question:\n\nHow does the gas price in Washington compare to the national average? and what is the exact difference?"],
|
103 |
+
["The ages of New Havens residents are 25.4% under the age of 18, 16.4% from 18 to 24, 31.2% from 25 to 44, 16.7% from 45 to 64, and 10.2% who were 65 years of age or older. The median age is 29 years, which is significantly lower than the national average. There are 91.8 males per 100 females. For every 100 females age 18 and over, there are 87.6 males.\n\nWhich gender group is larger: females or males?"],
|
104 |
+
],
|
105 |
+
)
|
106 |
+
|
107 |
+
with gr.Blocks(css="style.css") as demo:
|
108 |
+
gr.Markdown(DESCRIPTION)
|
109 |
+
chat_interface.render()
|
110 |
+
|
111 |
+
if __name__ == "__main__":
|
112 |
+
demo.queue(max_size=20).launch()
|
requirements.txt
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate==0.24.1
|
2 |
+
aiofiles==23.2.1
|
3 |
+
altair==5.1.2
|
4 |
+
annotated-types==0.6.0
|
5 |
+
anyio==3.7.1
|
6 |
+
attrs==23.1.0
|
7 |
+
certifi==2023.11.17
|
8 |
+
charset-normalizer==3.3.2
|
9 |
+
click==8.1.7
|
10 |
+
colorama==0.4.6
|
11 |
+
contourpy==1.2.0
|
12 |
+
cycler==0.12.1
|
13 |
+
exceptiongroup==1.1.3
|
14 |
+
fastapi==0.104.1
|
15 |
+
ffmpy==0.3.1
|
16 |
+
filelock==3.13.1
|
17 |
+
fonttools==4.45.0
|
18 |
+
fsspec==2023.10.0
|
19 |
+
gradio==4.5.0
|
20 |
+
gradio_client==0.7.0
|
21 |
+
h11==0.14.0
|
22 |
+
httpcore==1.0.2
|
23 |
+
httpx==0.25.1
|
24 |
+
huggingface-hub==0.19.4
|
25 |
+
idna==3.4
|
26 |
+
importlib-resources==6.1.1
|
27 |
+
Jinja2==3.1.2
|
28 |
+
jsonschema==4.20.0
|
29 |
+
jsonschema-specifications==2023.11.1
|
30 |
+
kiwisolver==1.4.5
|
31 |
+
markdown-it-py==3.0.0
|
32 |
+
MarkupSafe==2.1.3
|
33 |
+
matplotlib==3.8.2
|
34 |
+
mdurl==0.1.2
|
35 |
+
mpmath==1.3.0
|
36 |
+
networkx==3.0
|
37 |
+
numpy==1.26.2
|
38 |
+
orjson==3.9.10
|
39 |
+
packaging==23.2
|
40 |
+
pandas==2.1.3
|
41 |
+
Pillow==10.1.0
|
42 |
+
protobuf==4.25.1
|
43 |
+
psutil==5.9.6
|
44 |
+
pydantic==2.5.1
|
45 |
+
pydantic_core==2.14.3
|
46 |
+
pydub==0.25.1
|
47 |
+
Pygments==2.17.1
|
48 |
+
pyparsing==3.1.1
|
49 |
+
python-dateutil==2.8.2
|
50 |
+
python-multipart==0.0.6
|
51 |
+
pytz==2023.3.post1
|
52 |
+
PyYAML==6.0.1
|
53 |
+
referencing==0.31.0
|
54 |
+
regex==2023.10.3
|
55 |
+
requests==2.31.0
|
56 |
+
rich==13.7.0
|
57 |
+
rpds-py==0.13.1
|
58 |
+
safetensors==0.4.0
|
59 |
+
semantic-version==2.10.0
|
60 |
+
sentencepiece==0.1.99
|
61 |
+
shellingham==1.5.4
|
62 |
+
six==1.16.0
|
63 |
+
sniffio==1.3.0
|
64 |
+
spaces==0.18.0
|
65 |
+
starlette==0.27.0
|
66 |
+
sympy==1.12
|
67 |
+
tokenizers==0.13.3
|
68 |
+
tomlkit==0.12.0
|
69 |
+
toolz==0.12.0
|
70 |
+
torch==2.1.1+cu118
|
71 |
+
torchaudio==2.1.1+cu118
|
72 |
+
torchvision==0.16.1+cu118
|
73 |
+
tqdm==4.66.1
|
74 |
+
transformers==4.33.1
|
75 |
+
triton==2.1.0
|
76 |
+
typer==0.9.0
|
77 |
+
typing_extensions==4.8.0
|
78 |
+
tzdata==2023.3
|
79 |
+
urllib3==2.1.0
|
80 |
+
uvicorn==0.24.0.post1
|
81 |
+
websockets==11.0.3
|
82 |
+
zipp==3.17.0
|
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 |
+
.contain {
|
13 |
+
max-width: 900px;
|
14 |
+
margin: auto;
|
15 |
+
padding-top: 1.5rem;
|
16 |
+
}
|