Spaces:
Sleeping
Sleeping
Joaoffg
commited on
Commit
·
c12c6cc
1
Parent(s):
26ebcbe
Updated gradio app
Browse files
app.py
CHANGED
@@ -1,3 +1,76 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
def evaluate(instruction):
|
4 |
+
# Generate a response:
|
5 |
+
input = None
|
6 |
+
prompt = prompter.generate_prompt(instruction, input)
|
7 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
8 |
+
#inputs = inputs.to("cuda:0")
|
9 |
+
input_ids = inputs["input_ids"]
|
10 |
+
|
11 |
+
#play around with generation strategies for better/diverse sequences. https://huggingface.co/docs/transformers/generation_strategies
|
12 |
+
temperature=0.2
|
13 |
+
top_p=0.95
|
14 |
+
top_k=25
|
15 |
+
num_beams=1
|
16 |
+
# num_beam_groups=num_beams #see: 'Diverse beam search decoding'
|
17 |
+
max_new_tokens=256
|
18 |
+
repetition_penalty = 2.0
|
19 |
+
do_sample = True # allow 'beam sample': do_sample=True, num_beams > 1
|
20 |
+
num_return_sequences = 1 #generate multiple candidates, takes longer..
|
21 |
+
|
22 |
+
generation_config = transformers.GenerationConfig(
|
23 |
+
temperature=temperature,
|
24 |
+
top_p=top_p,
|
25 |
+
top_k=top_k,
|
26 |
+
num_beams=num_beams,
|
27 |
+
repetition_penalty=repetition_penalty,
|
28 |
+
do_sample=do_sample,
|
29 |
+
min_new_tokens=32,
|
30 |
+
num_return_sequences=num_return_sequences,
|
31 |
+
pad_token_id = 0
|
32 |
+
# num_beam_groups=num_beam_groups
|
33 |
+
)
|
34 |
+
|
35 |
+
generate_params = {
|
36 |
+
"input_ids": input_ids,
|
37 |
+
"generation_config": generation_config,
|
38 |
+
"return_dict_in_generate": True,
|
39 |
+
"output_scores": True,
|
40 |
+
"max_new_tokens": max_new_tokens,
|
41 |
+
}
|
42 |
+
with torch.no_grad():
|
43 |
+
generation_output = model.generate(
|
44 |
+
input_ids=input_ids,
|
45 |
+
generation_config=generation_config,
|
46 |
+
return_dict_in_generate=True,
|
47 |
+
output_scores=True,
|
48 |
+
max_new_tokens=max_new_tokens,
|
49 |
+
)
|
50 |
+
|
51 |
+
|
52 |
+
print(f'Instruction: {instruction}')
|
53 |
+
|
54 |
+
for i,s in enumerate(generation_output.sequences):
|
55 |
+
output = tokenizer.decode(s,skip_special_tokens=True)
|
56 |
+
# print(output)
|
57 |
+
return(f' {prompter.get_response(output)}')
|
58 |
+
|
59 |
+
gr.Interface(
|
60 |
+
fn=evaluate,
|
61 |
+
inputs=[
|
62 |
+
gr.components.Textbox(
|
63 |
+
lines=2,
|
64 |
+
label="Instruction",
|
65 |
+
placeholder="Explain economic growth.",
|
66 |
+
),
|
67 |
+
],
|
68 |
+
outputs=[
|
69 |
+
gr.components.Textbox(
|
70 |
+
lines=5,
|
71 |
+
label="Output",
|
72 |
+
)
|
73 |
+
],
|
74 |
+
title="🌲 ELM - Erasmian Language Model",
|
75 |
+
description="ELM is a 900M parameter language model finetuned to follow instruction. It is trained on Erasmus University academic outputs and the [Stanford Alpaca](https://github.com/tatsu-lab/stanford_alpaca) dataset. For more information, please visit [the GitHub repository](https://github.com/Joaoffg/ELM).", # noqa: E501
|
76 |
+
).queue().gr.load("models/Joaoffg/ELM").launch()
|