Spaces:
Running
on
Zero
Running
on
Zero
add demo
Browse files- README.md +1 -1
- app.py +112 -0
- requirements.txt +5 -0
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Salamandra 7B
|
3 |
-
emoji:
|
4 |
colorFrom: pink
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
|
|
1 |
---
|
2 |
title: Salamandra 7B
|
3 |
+
emoji: 🦎
|
4 |
colorFrom: pink
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
app.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
description = """
|
7 |
+
[🦎Salamandra-7b-instruct](https://huggingface.co/BSC-LT/salamandra-7b-instruct) is a Transformer-based decoder-only language model that has been pre-trained on 7.8 trillion tokens of highly curated data.
|
8 |
+
The pre-training corpus contains text in 35 European languages and code. This model has been instruction-tuned and can be used as a general-purpose assistant.
|
9 |
+
"""
|
10 |
+
|
11 |
+
join_us = """
|
12 |
+
## Join us:
|
13 |
+
🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻
|
14 |
+
[![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/qdfnvSPcqP)
|
15 |
+
On 🤗Huggingface: [MultiTransformer](https://huggingface.co/MultiTransformer)
|
16 |
+
On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [Build Tonic](https://git.tonic-ai.com/contribute)
|
17 |
+
🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
|
18 |
+
"""
|
19 |
+
|
20 |
+
model_id = "BSC-LT/salamandra-7b-instruct"
|
21 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
22 |
+
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
24 |
+
model = AutoModelForCausalLM.from_pretrained(
|
25 |
+
model_id,
|
26 |
+
device_map="auto",
|
27 |
+
torch_dtype=torch.bfloat16
|
28 |
+
)
|
29 |
+
|
30 |
+
def generate_text(system_prompt, prompt, temperature, max_new_tokens, top_p, repetition_penalty):
|
31 |
+
date_string = datetime.today().strftime('%Y-%m-%d')
|
32 |
+
messages = [
|
33 |
+
{"role": "system", "content": system_prompt},
|
34 |
+
{"role": "user", "content": prompt}
|
35 |
+
]
|
36 |
+
|
37 |
+
chat_prompt = tokenizer.apply_chat_template(
|
38 |
+
messages,
|
39 |
+
tokenize=False,
|
40 |
+
add_generation_prompt=True,
|
41 |
+
date_string=date_string
|
42 |
+
)
|
43 |
+
|
44 |
+
inputs = tokenizer(chat_prompt, return_tensors="pt").to(device)
|
45 |
+
|
46 |
+
outputs = model.generate(
|
47 |
+
**inputs,
|
48 |
+
max_new_tokens=max_new_tokens,
|
49 |
+
temperature=temperature,
|
50 |
+
top_p=top_p,
|
51 |
+
repetition_penalty=repetition_penalty,
|
52 |
+
do_sample=True,
|
53 |
+
pad_token_id=tokenizer.eos_token_id
|
54 |
+
)
|
55 |
+
|
56 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
57 |
+
return generated_text.split("assistant\n")[-1].strip()
|
58 |
+
|
59 |
+
def update_output(system_prompt, prompt, temperature, max_new_tokens, top_p, repetition_penalty):
|
60 |
+
return generate_text(system_prompt, prompt, temperature, max_new_tokens, top_p, repetition_penalty)
|
61 |
+
|
62 |
+
with gr.Blocks() as demo:
|
63 |
+
gr.Markdown("# 🦎 Welcome to Tonic's Salamandra-7b-instruct Demo")
|
64 |
+
|
65 |
+
with gr.Row():
|
66 |
+
with gr.Column(scale=1):
|
67 |
+
with gr.Group():
|
68 |
+
gr.Markdown(description)
|
69 |
+
with gr.Column(scale=1):
|
70 |
+
with gr.Group():
|
71 |
+
gr.Markdown(join_us)
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
with gr.Column(scale=1):
|
75 |
+
system_prompt = gr.Textbox(
|
76 |
+
lines=3,
|
77 |
+
label="🖥️ System Prompt",
|
78 |
+
value="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.\n\nIf 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."
|
79 |
+
)
|
80 |
+
prompt = gr.Textbox(lines=5, label="🙋♂️ User Prompt")
|
81 |
+
generate_button = gr.Button("Generate with 🦎 Salamandra-7b-instruct")
|
82 |
+
|
83 |
+
with gr.Accordion("🧪 Parameters", open=False):
|
84 |
+
temperature = gr.Slider(0.0, 1.0, value=0.7, label="🌡️ Temperature")
|
85 |
+
max_new_tokens = gr.Slider(1, 1000, value=200, step=1, label="🔢 Max New Tokens")
|
86 |
+
top_p = gr.Slider(0.0, 1.0, value=0.95, label="⚛️ Top P")
|
87 |
+
repetition_penalty = gr.Slider(1.0, 2.0, value=1.2, label="🔁 Repetition Penalty")
|
88 |
+
|
89 |
+
with gr.Column(scale=1):
|
90 |
+
output = gr.Textbox(lines=10, label="🦎 Salamandra-7b-instruct Output")
|
91 |
+
|
92 |
+
generate_button.click(
|
93 |
+
update_output,
|
94 |
+
inputs=[system_prompt, prompt, temperature, max_new_tokens, top_p, repetition_penalty],
|
95 |
+
outputs=output
|
96 |
+
)
|
97 |
+
|
98 |
+
gr.Examples(
|
99 |
+
examples=[
|
100 |
+
["At what temperature does water boil?"],
|
101 |
+
["Explain the concept of artificial intelligence in simple terms."],
|
102 |
+
["Write a short poem about the beauty of nature."],
|
103 |
+
["What are the main differences between Python and JavaScript?"],
|
104 |
+
["Describe the process of photosynthesis in plants."]
|
105 |
+
],
|
106 |
+
inputs=prompt,
|
107 |
+
outputs=prompt,
|
108 |
+
label="Example Prompts"
|
109 |
+
)
|
110 |
+
|
111 |
+
if __name__ == "__main__":
|
112 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
accelerate
|
4 |
+
sentencepiece
|
5 |
+
protobuf
|