s3nh commited on
Commit
075341e
1 Parent(s): a314c67

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pathlib
2
+ import gradio as gr
3
+ import transformers
4
+ from transformers import AutoTokenizer
5
+ from transformers import ModelForCausalLM
6
+ from transformers import GenerationConfig
7
+ from typing import List, Dict, Union
8
+ from typing import Any, TypeVar
9
+
10
+ Pathable = Union[str, pathlib.Path]
11
+
12
+ def load_model(name: str) -> Any:
13
+ return ModelForCausalLM.from_pretrained(name)
14
+
15
+ def load_tokenizer(name: str) -> Any:
16
+ return AutoTokenizer.from_pretrained(name)
17
+
18
+ def create_generator():
19
+ return GenerationConfig(
20
+ temperature=1.0,
21
+ top_p=0.75,
22
+ num_beams=4,
23
+ )
24
+
25
+ def generate_prompt(instruction, input=None):
26
+ if input:
27
+ return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
28
+
29
+ ### Instruction:
30
+ {instruction}
31
+
32
+ ### Input:
33
+ {input}
34
+
35
+ ### Response:"""
36
+ else:
37
+ return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
38
+
39
+ ### Instruction:
40
+ {instruction}
41
+
42
+ ### Response:"""
43
+
44
+
45
+
46
+
47
+ def evaluate(instruction, input=None):
48
+ prompt = generate_prompt(instruction, input)
49
+ inputs = tokenizer(prompt, return_tensors="pt")
50
+ input_ids = inputs["input_ids"].cuda()
51
+ generation_output = model.generate(
52
+ input_ids=input_ids,
53
+ generation_config=generation_config,
54
+ return_dict_in_generate=True,
55
+ output_scores=True,
56
+ max_new_tokens=256
57
+ )
58
+ for s in generation_output.sequences:
59
+ output = tokenizer.decode(s)
60
+ print("Response:", output.split("### Response:")[1].strip())