lewtun HF staff philschmid HF staff commited on
Commit
a9e7d31
0 Parent(s):

Duplicate from philschmid/igel-playground

Browse files

Co-authored-by: Philipp Schmid <[email protected]>

Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +191 -0
  4. requirements.txt +5 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Igel Playground
3
+ emoji: 🧠
4
+ colorFrom: red
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 3.23.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: philschmid/igel-playground
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig, TextIteratorStreamer
4
+ import torch
5
+ from threading import Thread
6
+ from huggingface_hub import Repository
7
+ import json
8
+
9
+ theme = gr.themes.Monochrome(
10
+ primary_hue="indigo",
11
+ secondary_hue="blue",
12
+ neutral_hue="slate",
13
+ radius_size=gr.themes.sizes.radius_sm,
14
+ font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
15
+ )
16
+ # filesystem to save input and outputs
17
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
18
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
19
+ if HF_TOKEN:
20
+ repo = Repository(
21
+ local_dir="data", clone_from="philschmid/playground-prompts", use_auth_token=HF_TOKEN, repo_type="dataset"
22
+ )
23
+
24
+
25
+ # Load peft config for pre-trained checkpoint etc.
26
+ device = "cuda" if torch.cuda.is_available() else "cpu"
27
+ model_id = "philschmid/instruct-igel-001"
28
+ if device == "cpu":
29
+ model = AutoModelForCausalLM.from_pretrained(model_id, low_cpu_mem_usage=True)
30
+ else:
31
+ # torch_dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] == 8 else torch.float16
32
+ # model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch_dtype, device_map="auto")
33
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", load_in_8bit=True)
34
+
35
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
36
+
37
+ prompt_template = f"### Anweisung:\n{{input}}\n\n### Antwort:"
38
+
39
+
40
+ def generate(instruction, temperature, max_new_tokens, top_p, length_penalty):
41
+ formatted_instruction = prompt_template.format(input=instruction)
42
+ # COMMENT IN FOR NON STREAMING
43
+ # generation_config = GenerationConfig(
44
+ # do_sample=True,
45
+ # top_p=top_p,
46
+ # temperature=temperature,
47
+ # max_new_tokens=max_new_tokens,
48
+ # early_stopping=True,
49
+ # length_penalty=length_penalty,
50
+ # eos_token_id=tokenizer.eos_token_id,
51
+ # pad_token_id=tokenizer.pad_token_id,
52
+ # )
53
+
54
+ # input_ids = tokenizer(
55
+ # formatted_instruction, return_tensors="pt", truncation=True, max_length=2048
56
+ # ).input_ids.cuda()
57
+
58
+ # with torch.inference_mode(), torch.autocast("cuda"):
59
+ # outputs = model.generate(input_ids=input_ids, generation_config=generation_config)[0]
60
+
61
+ # output = tokenizer.decode(outputs.detach().cpu().numpy(), skip_special_tokens=True)
62
+ # return output.split("### Antwort:\n")[1]
63
+
64
+ # STREAMING BASED ON git+https://github.com/gante/transformers.git@streamer_iterator
65
+
66
+ # streaming
67
+ streamer = TextIteratorStreamer(tokenizer)
68
+ model_inputs = tokenizer(formatted_instruction, return_tensors="pt", truncation=True, max_length=2048)
69
+ # move to gpu
70
+ model_inputs = {k: v.to(device) for k, v in model_inputs.items()}
71
+
72
+ generate_kwargs = dict(
73
+ top_p=top_p,
74
+ temperature=temperature,
75
+ max_new_tokens=max_new_tokens,
76
+ early_stopping=True,
77
+ length_penalty=length_penalty,
78
+ eos_token_id=tokenizer.eos_token_id,
79
+ pad_token_id=tokenizer.eos_token_id,
80
+ )
81
+ t = Thread(target=model.generate, kwargs={**dict(model_inputs, streamer=streamer), **generate_kwargs})
82
+ t.start()
83
+
84
+ output = ""
85
+ hidden_output = ""
86
+ for new_text in streamer:
87
+ # skip streaming until new text is available
88
+ if len(hidden_output) <= len(formatted_instruction):
89
+ hidden_output += new_text
90
+ continue
91
+ # replace eos token
92
+ if tokenizer.eos_token in new_text:
93
+ new_text = new_text.replace(tokenizer.eos_token, "")
94
+ output += new_text
95
+ yield output
96
+ if HF_TOKEN:
97
+ save_inputs_and_outputs(formatted_instruction, output, generate_kwargs)
98
+ return output
99
+
100
+
101
+ def save_inputs_and_outputs(inputs, outputs, generate_kwargs):
102
+ with open(os.path.join("data", "prompts.jsonl"), "a") as f:
103
+ json.dump({"inputs": inputs, "outputs": outputs, "generate_kwargs": generate_kwargs}, f, ensure_ascii=False)
104
+ f.write("\n")
105
+ commit_url = repo.push_to_hub()
106
+
107
+
108
+ examples = [
109
+ """Beantworten Sie die Frage am Ende des Textes anhand der folgenden Zusammenhänge. Wenn Sie die Antwort nicht wissen, sagen Sie, dass Sie es nicht wissen, versuchen Sie nicht, eine Antwort zu erfinden.
110
+ "Das Unternehmen wurde 2016 von den französischen Unternehmern Clément Delangue, Julien Chaumond und Thomas Wolf gegründet und entwickelte ursprünglich eine Chatbot-App, die sich an Teenager richtete.[2] Nachdem das Modell hinter dem Chatbot offengelegt wurde, konzentrierte sich das Unternehmen auf eine Plattform für maschinelles Lernen.
111
+
112
+ Im März 2021 sammelte Hugging Face in einer Serie-B-Finanzierungsrunde 40 Millionen US-Dollar ein[3].
113
+
114
+ Am 28. April 2021 rief das Unternehmen in Zusammenarbeit mit mehreren anderen Forschungsgruppen den BigScience Research Workshop ins Leben, um ein offenes großes Sprachmodell zu veröffentlichen.[4] Im Jahr 2022 wurde der Workshop mit der Ankündigung von BLOOM abgeschlossen, einem mehrsprachigen großen Sprachmodell mit 176 Milliarden Parametern.[5]"
115
+
116
+ Frage: Wann wurde Hugging Face gegründet?""",
117
+ "Erklären Sie, was eine API ist.",
118
+ "Bitte beantworten Sie die folgende Frage. Wer wird der nächste Ballon d'or sein?",
119
+ "Beantworten Sie die folgende Ja/Nein-Frage, indem Sie Schritt für Schritt argumentieren. Kannst du ein ganzes Haiku in einem einzigen Tweet schreiben?",
120
+ "Schreibe eine Produktbeschreibung für einen LG 43UQ75009LF 109 cm (43 Zoll) UHD Fernseher (Active HDR, 60 Hz, Smart TV) [Modelljahr 2022]",
121
+ ]
122
+
123
+
124
+ with gr.Blocks(theme=theme) as demo:
125
+ with gr.Column():
126
+ gr.Markdown(
127
+ """<h1><center>IGEL - Instruction-tuned German large Language Model for Text</center></h1>
128
+ <p>
129
+ IGEL is a LLM model family developed for the German language. The first version of IGEL is built on top <a href="https://bigscience.huggingface.co/blog/bloom" target="_blank">BigScience BLOOM</a> adapted to the <a href="https://huggingface.co/malteos/bloom-6b4-clp-german">German language by Malte Ostendorff</a>. IGEL designed to provide accurate and reliable language understanding capabilities for a wide range of natural language understanding tasks, including sentiment analysis, language translation, and question answering.
130
+
131
+ The IGEL family includes instruction [instruct-igel-001](https://huggingface.co/philschmid/instruct-igel-001) and `chat-igel-001` _coming soon_.
132
+ </p>
133
+ """
134
+ )
135
+ with gr.Row():
136
+ with gr.Column(scale=3):
137
+ instruction = gr.Textbox(placeholder="Hier Anweisung eingeben...", label="Anweisung")
138
+ output = gr.Textbox(
139
+ interactive=False,
140
+ lines=8,
141
+ label="Antwort",
142
+ placeholder="Hier Antwort erscheint...",
143
+ )
144
+ submit = gr.Button("Generate", variant="primary")
145
+ gr.Examples(examples=examples, inputs=[instruction])
146
+
147
+ with gr.Column(scale=1):
148
+ temperature = gr.Slider(
149
+ label="Temperature",
150
+ value=1.0,
151
+ minimum=0.0,
152
+ maximum=1.0,
153
+ step=0.1,
154
+ interactive=True,
155
+ info="The higher more random",
156
+ )
157
+ max_new_tokens = gr.Slider(
158
+ label="Max new tokens",
159
+ value=256,
160
+ minimum=0,
161
+ maximum=2048,
162
+ step=5,
163
+ interactive=True,
164
+ info="The maximum numbers of new tokens",
165
+ )
166
+ top_p = gr.Slider(
167
+ label="Top p",
168
+ value=0.9,
169
+ minimum=0.0,
170
+ maximum=1,
171
+ step=0.05,
172
+ interactive=True,
173
+ info="probabilities that add up are kept",
174
+ )
175
+ length_penalty = gr.Slider(
176
+ label="Length penalty",
177
+ value=1.0,
178
+ minimum=-10.0,
179
+ maximum=10.0,
180
+ step=0.1,
181
+ interactive=True,
182
+ info="> 0.0 longer, < 0.0 shorter",
183
+ )
184
+
185
+ submit.click(generate, inputs=[instruction, temperature, max_new_tokens, top_p, length_penalty], outputs=[output])
186
+ instruction.submit(
187
+ generate, inputs=[instruction, temperature, max_new_tokens, top_p, length_penalty], outputs=[output]
188
+ )
189
+
190
+ demo.queue()
191
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ git+https://github.com/huggingface/peft.git
2
+ git+https://github.com/huggingface/transformers.git
3
+ huggingface_hub
4
+ accelerate==0.17.1
5
+ bitsandbytes