KennethTM commited on
Commit
2c38237
·
1 Parent(s): 5e2ce9e

added app file

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from threading import Thread
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
4
+
5
+ model_id = "KennethTM/gpt-neo-1.3B-danish"
6
+ peft_model_id = "KennethTM/gpt-neo-1.3B-danish-lora-r64-a128"
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
9
+ model = AutoModelForCausalLM.from_pretrained(model_id)
10
+ model.load_adapter(peft_model_id)
11
+ model.eval()
12
+
13
+ def run_generation(user_text):
14
+ model_inputs = tokenizer(user_text, return_tensors="pt")
15
+
16
+ streamer = TextIteratorStreamer(tokenizer, skip_prompt=False, skip_special_tokens=True)
17
+
18
+ generate_kwargs = dict(
19
+ inputs = model_inputs.input_ids,
20
+ attention_mask = model_inputs.attention_mask,
21
+ streamer = streamer,
22
+ max_new_tokens = 20,
23
+ penalty_alpha = 0.6,
24
+ top_k = 4,
25
+ )
26
+
27
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
28
+ t.start()
29
+
30
+ model_output = ""
31
+ for new_text in streamer:
32
+ model_output += new_text
33
+ yield model_output
34
+
35
+ return model_output
36
+
37
+ with gr.Blocks() as demo:
38
+
39
+ gr.Markdown("# Auto-completion med dansk sprogmodel")
40
+ gr.Markdown("## Tryk 'Kør' eller 'SHIFT+ENTER' for auto-completion!")
41
+ textbox = gr.Textbox(placeholder="Skriv her...", lines=10)
42
+ btn = gr.Button("Kør")
43
+ btn.click(run_generation, textbox, textbox)
44
+ textbox.submit(run_generation, textbox, textbox)
45
+ gr.Markdown("## Om")
46
+ gr.Markdown("*Dansk sprogmodel (KennethTM/gpt-neo-1.3B-danish) fin-tunet yderligere med PEFT på udvalgte tekster om natur og miljø*")
47
+
48
+ demo.queue().launch()