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