felixz commited on
Commit
3d7c5d1
·
1 Parent(s): dbaff7c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import ctranslate2
3
+ from transformers import AutoModel
4
+
5
+ # xl size run out of memory on 16GB VM
6
+ model_name = 'google/flan-t5-large'
7
+ #model_name = 'jncraton/fastchat-t5-3b-v1.0-ct2-int8'
8
+
9
+ # Load model directly
10
+ #from transformers import AutoModel
11
+ #model = AutoModel.from_pretrained(model_name)
12
+
13
+ translator = ctranslate2.Translator("t5-small-ct2")
14
+ tokenizer = T5Tokenizer.from_pretrained(model_name)
15
+ #model = T5ForConditionalGeneration.from_pretrained(model_name)
16
+
17
+ title = ""
18
+
19
+ def get_examples ():
20
+ return [
21
+ ["Peter goes to the store to buy a soda. The soda costs $.25 an ounce. \
22
+ He brought $2 with him and leaves with $.50. How many ounces of soda did he buy?",
23
+ "How much did Peter spend on soda? ** He spend $1.5 on soda because 2 - .5 = <<2-.5=1.5>>1.5 \
24
+ How many ounces of soda did Peter buy? ** He bought 6 ounces of soda because 1.5 / .25 = <<6=6>>6 #### 6"
25
+ ],
26
+ ["Krystian works in the library. He borrows an average of 40 books every day. \
27
+ Every Friday, his number of borrowed books is about 40% higher than the daily average. How many books does he borrow in a week if the library is open from Monday to Friday?"
28
+ ,"How many books does Krystian borrow on Friday? ** The number of books borrowed \
29
+ on Friday is higher by 40 * 40/100 = <<40*40/100=16>>16 books. How many books does Krystian borrow in a week? ** There are 5 days from Monday to Friday inclusive, so Krystian borrows an average of 5 * 40 = <<5*40=200>>200 books during that time. How many books does Krystian borrow in a week? ** With Friday's increase in borrowings, during one week Krystian borrows 200 + 16 = <<200+16=216>>216 books."]
30
+ , ["Jane had $60 but gave $30 to dave and went to movies and spend $2. How much money does Jane has left? Answer by reasoning step by step:", "$28"]
31
+ ]
32
+
33
+
34
+ def text2text(input_text):
35
+ input_tokens = tokenizer.convert_ids_to_tokens(tokenizer.encode(input_text))
36
+ results = translator.translate_batch([input_tokens])
37
+
38
+ output_tokens = results[0].hypotheses[0]
39
+ output_text = tokenizer.decode(tokenizer.convert_tokens_to_ids(output_tokens))
40
+
41
+ return output_text
42
+
43
+
44
+ with gr.Blocks() as demo:
45
+ gr.Markdown(
46
+ """
47
+ # Fast Chat T5 Demo
48
+ Fast inference with quantized LLM
49
+ Prompt the model in the Input box.
50
+ """)
51
+ txt_in = gr.Textbox(label="Input", lines=3)
52
+ correct_label = gr.Label(label="Correct")
53
+ txt_out = gr.Textbox(value="", label="Output", lines=4)
54
+
55
+
56
+ btn = gr.Button(value="Submit")
57
+ btn.click(text2text, inputs=[txt_in], outputs=[txt_out])
58
+
59
+
60
+ gr.Examples(
61
+ examples=get_examples(),
62
+ inputs=[txt_in,correct_label]
63
+ )
64
+
65
+
66
+ if __name__ == "__main__":
67
+ demo.launch()