Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def respond(
|
| 11 |
message,
|
| 12 |
history: list[tuple[str, str]],
|
| 13 |
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
):
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
|
@@ -24,20 +31,26 @@ def respond(
|
|
| 24 |
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
"""
|
| 43 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
|
@@ -45,7 +58,7 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
|
|
| 45 |
demo = gr.ChatInterface(
|
| 46 |
respond,
|
| 47 |
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a
|
| 49 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
gr.Slider(
|
|
@@ -60,4 +73,4 @@ demo = gr.ChatInterface(
|
|
| 60 |
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import spaces
|
| 4 |
|
| 5 |
+
# Load model and tokenizer
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
+
device = "cuda" # the device to load the model onto
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained("yuchenlin/Rex-v0.1-1.5B", trust_remote_code=True, rex_size=3)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
"yuchenlin/Rex-v0.1-1.5B",
|
| 12 |
+
torch_dtype="auto"
|
| 13 |
+
)
|
| 14 |
+
model.to(device)
|
| 15 |
+
|
| 16 |
+
@spaces.GPU(enable_queue=True)
|
| 17 |
def respond(
|
| 18 |
message,
|
| 19 |
history: list[tuple[str, str]],
|
| 20 |
system_message,
|
| 21 |
+
max_tokens=512,
|
| 22 |
+
temperature=0.5,
|
| 23 |
+
top_p=1.0,
|
| 24 |
):
|
| 25 |
messages = [{"role": "system", "content": system_message}]
|
| 26 |
|
|
|
|
| 31 |
messages.append({"role": "assistant", "content": val[1]})
|
| 32 |
|
| 33 |
messages.append({"role": "user", "content": message})
|
| 34 |
+
|
| 35 |
+
text = tokenizer.apply_chat_template(
|
|
|
|
|
|
|
| 36 |
messages,
|
| 37 |
+
tokenize=False,
|
| 38 |
+
add_generation_prompt=True
|
| 39 |
+
)
|
| 40 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(device)
|
| 41 |
+
|
| 42 |
+
generated_ids = model.generate(
|
| 43 |
+
model_inputs.input_ids,
|
| 44 |
+
max_new_tokens = max_tokens,
|
| 45 |
+
temperature = temperature,
|
| 46 |
+
top_p = top_p,
|
| 47 |
+
)
|
| 48 |
+
generated_ids = [
|
| 49 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
| 50 |
+
]
|
| 51 |
|
| 52 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 53 |
+
return response
|
| 54 |
|
| 55 |
"""
|
| 56 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
|
|
|
| 58 |
demo = gr.ChatInterface(
|
| 59 |
respond,
|
| 60 |
additional_inputs=[
|
| 61 |
+
gr.Textbox(value="You are a helpful AI assistant and your name is RexLM.", label="System message"),
|
| 62 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 63 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 64 |
gr.Slider(
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
+
demo.launch(share=False)
|