Spaces:
Paused
Paused
ranamhamoud
commited on
Commit
โข
0f4b183
1
Parent(s):
0432f6c
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,8 @@ from typing import Iterator
|
|
5 |
import gradio as gr
|
6 |
import spaces
|
7 |
import torch
|
8 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
|
|
9 |
|
10 |
MAX_MAX_NEW_TOKENS = 2048
|
11 |
DEFAULT_MAX_NEW_TOKENS = 1024
|
@@ -13,17 +14,13 @@ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
|
13 |
|
14 |
DESCRIPTION = """\
|
15 |
# Llama-2 7B Chat
|
16 |
-
|
17 |
This Space demonstrates model [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat) by Meta, a Llama 2 model with 7B parameters fine-tuned for chat instructions. Feel free to play with it, or duplicate to run generations without a queue! If you want to run your own service, you can also [deploy the model on Inference Endpoints](https://huggingface.co/inference-endpoints).
|
18 |
-
|
19 |
๐ For more details about the Llama 2 family of models and how to use them with `transformers`, take a look [at our blog post](https://huggingface.co/blog/llama2).
|
20 |
-
|
21 |
๐จ Looking for an even more powerful model? Check out the [13B version](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat) or the large [70B model demo](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI).
|
22 |
"""
|
23 |
|
24 |
LICENSE = """
|
25 |
<p/>
|
26 |
-
|
27 |
---
|
28 |
As a derivate work of [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat) by Meta,
|
29 |
this demo is governed by the original [license](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/USE_POLICY.md).
|
@@ -34,8 +31,14 @@ if not torch.cuda.is_available():
|
|
34 |
|
35 |
|
36 |
if torch.cuda.is_available():
|
|
|
|
|
|
|
|
|
37 |
model_id = "meta-llama/Llama-2-7b-chat-hf"
|
38 |
-
|
|
|
|
|
39 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
40 |
tokenizer.use_default_system_prompt = False
|
41 |
|
@@ -44,7 +47,6 @@ if torch.cuda.is_available():
|
|
44 |
def generate(
|
45 |
message: str,
|
46 |
chat_history: list[tuple[str, str]],
|
47 |
-
system_prompt: str,
|
48 |
max_new_tokens: int = 1024,
|
49 |
temperature: float = 0.6,
|
50 |
top_p: float = 0.9,
|
@@ -52,8 +54,6 @@ def generate(
|
|
52 |
repetition_penalty: float = 1.2,
|
53 |
) -> Iterator[str]:
|
54 |
conversation = []
|
55 |
-
if system_prompt:
|
56 |
-
conversation.append({"role": "system", "content": system_prompt})
|
57 |
for user, assistant in chat_history:
|
58 |
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
59 |
conversation.append({"role": "user", "content": message})
|
@@ -85,61 +85,25 @@ def generate(
|
|
85 |
yield "".join(outputs)
|
86 |
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
# gr.Slider(
|
100 |
-
# label="Temperature",
|
101 |
-
# minimum=0.1,
|
102 |
-
# maximum=4.0,
|
103 |
-
# step=0.1,
|
104 |
-
# value=0.6,
|
105 |
-
# ),
|
106 |
-
# gr.Slider(
|
107 |
-
# label="Top-p (nucleus sampling)",
|
108 |
-
# minimum=0.05,
|
109 |
-
# maximum=1.0,
|
110 |
-
# step=0.05,
|
111 |
-
# value=0.9,
|
112 |
-
# ),
|
113 |
-
# gr.Slider(
|
114 |
-
# label="Top-k",
|
115 |
-
# minimum=1,
|
116 |
-
# maximum=1000,
|
117 |
-
# step=1,
|
118 |
-
# value=50,
|
119 |
-
# ),
|
120 |
-
# gr.Slider(
|
121 |
-
# label="Repetition penalty",
|
122 |
-
# minimum=1.0,
|
123 |
-
# maximum=2.0,
|
124 |
-
# step=0.05,
|
125 |
-
# value=1.2,
|
126 |
-
# ),
|
127 |
-
# ],
|
128 |
-
# stop_btn=None,
|
129 |
-
# examples=[
|
130 |
-
# ["Hello there! How are you doing?"],
|
131 |
-
# ["Can you explain briefly to me what is the Python programming language?"],
|
132 |
-
# ["Explain the plot of Cinderella in a sentence."],
|
133 |
-
# ["How many hours does it take a man to eat a Helicopter?"],
|
134 |
-
# ["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
|
135 |
-
# ],
|
136 |
-
# )
|
137 |
|
138 |
with gr.Blocks(css="style.css") as demo:
|
139 |
gr.Markdown(DESCRIPTION)
|
140 |
gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
141 |
-
|
142 |
gr.Markdown(LICENSE)
|
143 |
|
144 |
if __name__ == "__main__":
|
145 |
demo.queue(max_size=20).launch()
|
|
|
|
|
|
5 |
import gradio as gr
|
6 |
import spaces
|
7 |
import torch
|
8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
|
9 |
+
from peft import PeftModel
|
10 |
|
11 |
MAX_MAX_NEW_TOKENS = 2048
|
12 |
DEFAULT_MAX_NEW_TOKENS = 1024
|
|
|
14 |
|
15 |
DESCRIPTION = """\
|
16 |
# Llama-2 7B Chat
|
|
|
17 |
This Space demonstrates model [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat) by Meta, a Llama 2 model with 7B parameters fine-tuned for chat instructions. Feel free to play with it, or duplicate to run generations without a queue! If you want to run your own service, you can also [deploy the model on Inference Endpoints](https://huggingface.co/inference-endpoints).
|
|
|
18 |
๐ For more details about the Llama 2 family of models and how to use them with `transformers`, take a look [at our blog post](https://huggingface.co/blog/llama2).
|
|
|
19 |
๐จ Looking for an even more powerful model? Check out the [13B version](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat) or the large [70B model demo](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI).
|
20 |
"""
|
21 |
|
22 |
LICENSE = """
|
23 |
<p/>
|
|
|
24 |
---
|
25 |
As a derivate work of [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat) by Meta,
|
26 |
this demo is governed by the original [license](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/USE_POLICY.md).
|
|
|
31 |
|
32 |
|
33 |
if torch.cuda.is_available():
|
34 |
+
bnb_config = BitsAndBytesConfig(
|
35 |
+
load_in_8bit=True,
|
36 |
+
bnb_4bit_compute_dtype=torch.float16,
|
37 |
+
)
|
38 |
model_id = "meta-llama/Llama-2-7b-chat-hf"
|
39 |
+
base_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto",quantization_config=bnb_config)
|
40 |
+
model = PeftModel.from_pretrained(
|
41 |
+
base_model,"ranamhamoud/storytell")
|
42 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
43 |
tokenizer.use_default_system_prompt = False
|
44 |
|
|
|
47 |
def generate(
|
48 |
message: str,
|
49 |
chat_history: list[tuple[str, str]],
|
|
|
50 |
max_new_tokens: int = 1024,
|
51 |
temperature: float = 0.6,
|
52 |
top_p: float = 0.9,
|
|
|
54 |
repetition_penalty: float = 1.2,
|
55 |
) -> Iterator[str]:
|
56 |
conversation = []
|
|
|
|
|
57 |
for user, assistant in chat_history:
|
58 |
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
59 |
conversation.append({"role": "user", "content": message})
|
|
|
85 |
yield "".join(outputs)
|
86 |
|
87 |
|
88 |
+
chat_interface = gr.ChatInterface(
|
89 |
+
fn=generate,
|
90 |
+
stop_btn=None,
|
91 |
+
examples=[
|
92 |
+
["Hello there! How are you doing?"],
|
93 |
+
["Can you explain briefly to me what is the Python programming language?"],
|
94 |
+
["Explain the plot of Cinderella in a sentence."],
|
95 |
+
["How many hours does it take a man to eat a Helicopter?"],
|
96 |
+
["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
|
97 |
+
],
|
98 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
with gr.Blocks(css="style.css") as demo:
|
101 |
gr.Markdown(DESCRIPTION)
|
102 |
gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
103 |
+
chat_interface.render()
|
104 |
gr.Markdown(LICENSE)
|
105 |
|
106 |
if __name__ == "__main__":
|
107 |
demo.queue(max_size=20).launch()
|
108 |
+
|
109 |
+
|