Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,21 @@
|
|
1 |
-
import asyncio
|
2 |
import gradio as gr
|
3 |
-
from
|
4 |
|
|
|
5 |
client = AsyncInferenceClient("meta-llama/Llama-2-70b-chat-hf")
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
description = """
|
11 |
-
This Space demonstrates model [Llama-2-70b-chat-hf](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) by Meta, a Llama 2 model with 70B parameters fine-tuned for chat 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/).
|
12 |
-
π 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).
|
13 |
-
π¨ Looking for lighter chat model versions of Llama-v2?
|
14 |
-
- π Check out the [7B Chat model demo](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat).
|
15 |
-
- π¦ Check out the [13B Chat model demo](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat).
|
16 |
-
Note: As a derivate work of [Llama-2-70b-chat](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) by Meta,
|
17 |
-
this demo is governed by the original [license](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/USE_POLICY.md).
|
18 |
-
"""
|
19 |
-
css = """.toast-wrap { display: none !important } """
|
20 |
-
examples = [
|
21 |
-
['Hello there! How are you doing?'],
|
22 |
-
['Can you explain to me briefly what is Python programming language?'],
|
23 |
-
['Explain the plot of Cinderella in a sentence.'],
|
24 |
-
['How many hours does it take a man to eat a Helicopter?'],
|
25 |
-
["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
|
26 |
-
]
|
27 |
-
|
28 |
-
# Note: We have removed default system prompt as requested by the paper authors [Dated: 13/Oct/2023]
|
29 |
-
# Prompting style for Llama2 without using system prompt
|
30 |
-
# <s>[INST] {{ user_msg_1 }} [/INST] {{ model_answer_1 }} </s><s>[INST] {{ user_msg_2 }} [/INST]
|
31 |
-
|
32 |
-
# Stream text - stream tokens with InferenceClient from TGI
|
33 |
-
async def predict(message, chatbot, system_prompt="", temperature=0.9, max_new_tokens=256, top_p=0.6, repetition_penalty=1.0,):
|
34 |
-
|
35 |
-
if system_prompt != "":
|
36 |
-
input_prompt = f"<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n "
|
37 |
-
else:
|
38 |
-
input_prompt = f"<s>[INST] "
|
39 |
-
|
40 |
-
temperature = float(temperature)
|
41 |
-
if temperature < 1e-2:
|
42 |
-
temperature = 1e-2
|
43 |
top_p = float(top_p)
|
44 |
|
45 |
for interaction in chatbot:
|
46 |
-
input_prompt
|
47 |
-
|
48 |
-
input_prompt = input_prompt + str(message) + " [/INST] "
|
49 |
|
|
|
50 |
partial_message = ""
|
51 |
-
|
|
|
52 |
prompt=input_prompt,
|
53 |
max_new_tokens=max_new_tokens,
|
54 |
stream=True,
|
@@ -58,140 +25,22 @@ async def predict(message, chatbot, system_prompt="", temperature=0.9, max_new_t
|
|
58 |
do_sample=True,
|
59 |
repetition_penalty=repetition_penalty,
|
60 |
):
|
61 |
-
partial_message
|
62 |
yield partial_message
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
)
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
temperature = 1e-2
|
78 |
-
top_p = float(top_p)
|
79 |
-
|
80 |
-
for interaction in chatbot:
|
81 |
-
input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s>[INST] "
|
82 |
-
|
83 |
-
input_prompt = input_prompt + str(message) + " [/INST] "
|
84 |
-
print(f"input_prompt - {input_prompt}")
|
85 |
-
|
86 |
-
data = {
|
87 |
-
"inputs": input_prompt,
|
88 |
-
"parameters": {
|
89 |
-
"max_new_tokens": max_new_tokens,
|
90 |
-
"temperature": temperature,
|
91 |
-
"top_p": top_p,
|
92 |
-
"repetition_penalty": repetition_penalty,
|
93 |
-
"do_sample": True,
|
94 |
-
},
|
95 |
-
}
|
96 |
-
|
97 |
-
response = requests.post(api_url, headers=headers, json=data) # auth=('hf', hf_token)) data=json.dumps(data),
|
98 |
-
|
99 |
-
if response.status_code == 200: # check if the request was successful
|
100 |
-
try:
|
101 |
-
json_obj = response.json()
|
102 |
-
if 'generated_text' in json_obj[0] and len(json_obj[0]['generated_text']) > 0:
|
103 |
-
return json_obj[0]['generated_text']
|
104 |
-
elif 'error' in json_obj[0]:
|
105 |
-
return json_obj[0]['error'] + ' Please refresh and try again with smaller input prompt'
|
106 |
-
else:
|
107 |
-
print(f"Unexpected response: {json_obj[0]}")
|
108 |
-
except json.JSONDecodeError:
|
109 |
-
print(f"Failed to decode response as JSON: {response.text}")
|
110 |
-
else:
|
111 |
-
print(f"Request failed with status code {response.status_code}")
|
112 |
-
|
113 |
-
|
114 |
-
def vote(data: gr.LikeData):
|
115 |
-
if data.liked:
|
116 |
-
print("You upvoted this response: " + data.value)
|
117 |
-
else:
|
118 |
-
print("You downvoted this response: " + data.value)
|
119 |
-
|
120 |
-
|
121 |
-
additional_inputs = [
|
122 |
-
gr.Textbox("", label="Optional system prompt"),
|
123 |
-
gr.Slider(
|
124 |
-
label="Temperature",
|
125 |
-
value=0.9,
|
126 |
-
minimum=0.0,
|
127 |
-
maximum=1.0,
|
128 |
-
step=0.05,
|
129 |
-
interactive=True,
|
130 |
-
info="Higher values produce more diverse outputs",
|
131 |
-
),
|
132 |
-
gr.Slider(
|
133 |
-
label="Max new tokens",
|
134 |
-
value=256,
|
135 |
-
minimum=0,
|
136 |
-
maximum=4096,
|
137 |
-
step=64,
|
138 |
-
interactive=True,
|
139 |
-
info="The maximum numbers of new tokens",
|
140 |
-
),
|
141 |
-
gr.Slider(
|
142 |
-
label="Top-p (nucleus sampling)",
|
143 |
-
value=0.6,
|
144 |
-
minimum=0.0,
|
145 |
-
maximum=1,
|
146 |
-
step=0.05,
|
147 |
-
interactive=True,
|
148 |
-
info="Higher values sample more low-probability tokens",
|
149 |
-
),
|
150 |
-
gr.Slider(
|
151 |
-
label="Repetition penalty",
|
152 |
-
value=1.2,
|
153 |
-
minimum=1.0,
|
154 |
-
maximum=2.0,
|
155 |
-
step=0.05,
|
156 |
-
interactive=True,
|
157 |
-
info="Penalize repeated tokens",
|
158 |
-
)
|
159 |
-
]
|
160 |
-
|
161 |
-
chatbot_stream = gr.Chatbot(bubble_full_width=False)
|
162 |
-
chatbot_batch = gr.Chatbot(bubble_full_width=False)
|
163 |
-
chat_interface_stream = gr.ChatInterface(
|
164 |
-
predict,
|
165 |
-
title=title,
|
166 |
-
description=description,
|
167 |
-
textbox=gr.Textbox(),
|
168 |
-
chatbot=chatbot_stream,
|
169 |
-
css=css,
|
170 |
-
examples=examples,
|
171 |
-
additional_inputs=additional_inputs,
|
172 |
-
)
|
173 |
-
chat_interface_batch = gr.ChatInterface(
|
174 |
-
predict_batch,
|
175 |
-
title=title,
|
176 |
-
description=description,
|
177 |
-
textbox=gr.Textbox(),
|
178 |
-
chatbot=chatbot_batch,
|
179 |
-
css=css,
|
180 |
-
examples=examples,
|
181 |
-
additional_inputs=additional_inputs,
|
182 |
)
|
183 |
|
184 |
-
|
185 |
-
with gr.Blocks() as demo:
|
186 |
-
|
187 |
-
with gr.Tab("Streaming"):
|
188 |
-
# streaming chatbot
|
189 |
-
chatbot_stream.like(vote, None, None)
|
190 |
-
chat_interface_stream.render()
|
191 |
-
|
192 |
-
with gr.Tab("Batch"):
|
193 |
-
# non-streaming chatbot
|
194 |
-
chatbot_batch.like(vote, None, None)
|
195 |
-
chat_interface_batch.render()
|
196 |
-
|
197 |
-
demo.queue(max_size=100).launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from openai import AsyncInferenceClient
|
3 |
|
4 |
+
# Assuming client is a global variable
|
5 |
client = AsyncInferenceClient("meta-llama/Llama-2-70b-chat-hf")
|
6 |
|
7 |
+
def predict(message, chatbot, system_prompt="", temperature=0.9, max_new_tokens=256, top_p=0.6, repetition_penalty=1.0):
|
8 |
+
input_prompt = f"<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n " if system_prompt else "<s>[INST] "
|
9 |
+
temperature = max(1e-2, float(temperature))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
top_p = float(top_p)
|
11 |
|
12 |
for interaction in chatbot:
|
13 |
+
input_prompt += f"{interaction[0]} [/INST] {interaction[1]} </s><s>[INST] "
|
|
|
|
|
14 |
|
15 |
+
input_prompt += f"{message} [/INST] "
|
16 |
partial_message = ""
|
17 |
+
|
18 |
+
for token in client.text_generation(
|
19 |
prompt=input_prompt,
|
20 |
max_new_tokens=max_new_tokens,
|
21 |
stream=True,
|
|
|
25 |
do_sample=True,
|
26 |
repetition_penalty=repetition_penalty,
|
27 |
):
|
28 |
+
partial_message += token
|
29 |
yield partial_message
|
30 |
|
31 |
+
# Create a Gradio interface
|
32 |
+
iface = gr.Interface(
|
33 |
+
fn=predict,
|
34 |
+
inputs=[
|
35 |
+
gr.Textbox("text", label="Message"),
|
36 |
+
gr.Textbox("text", label="Chatbot"),
|
37 |
+
gr.Textbox("text", label="System Prompt"),
|
38 |
+
gr.Number("slider", minimum=0.1, maximum=2, default=0.9, label="Temperature"),
|
39 |
+
gr.Number("slider", minimum=1, maximum=1000, default=256, label="Max New Tokens"),
|
40 |
+
gr.Number("slider", minimum=0.1, maximum=1, default=0.6, label="Top P"),
|
41 |
+
gr.Number("slider", minimum=0.1, maximum=2, default=1.0, label="Repetition Penalty"),
|
42 |
+
],
|
43 |
+
outputs=gr.Textbox(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
)
|
45 |
|
46 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|