felix commited on
Commit
1fd05df
1 Parent(s): 1f4ef7b

make chat mode

Browse files
Files changed (1) hide show
  1. app.py +76 -4
app.py CHANGED
@@ -1,7 +1,79 @@
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
 
4
 
5
+ # xl size run out of memory on 16GB vm
6
+ # All the models have input length of 512 tokens and outputs of 512 tokens
7
+ # small 250M param
8
+ # base
9
+ # large
10
+ # xl
11
+ # xxl
12
+ model_name = "large"
13
+ tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-" + model_name)
14
+ model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-" + model_name)
15
+
16
+ title = ""
17
+
18
+ def get_examples ():
19
+ return [
20
+ ["Peter goes to the store to buy a soda. The soda costs $.25 an ounce. \
21
+ He brought $2 with him and leaves with $.50. How many ounces of soda did he buy?",
22
+ "How much did Peter spend on soda? ** He spend $1.5 on soda because 2 - .5 = <<2-.5=1.5>>1.5 \
23
+ How many ounces of soda did Peter buy? ** He bought 6 ounces of soda because 1.5 / .25 = <<6=6>>6 #### 6"
24
+ ],
25
+ ["Krystian works in the library. He borrows an average of 40 books every day. \
26
+ 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?"
27
+ ,"How many books does Krystian borrow on Friday? ** The number of books borrowed \
28
+ 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."]
29
+ , ["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"],
30
+ ["Cat is a friend of a Dog. Are cat and Dog friends?", "Yes"]
31
+ ]
32
+
33
+
34
+ def text2text(input_text):
35
+ input_ids = tokenizer(input_text, return_tensors="pt").input_ids
36
+
37
+ input_num_tokens = input_ids.shape[1]
38
+
39
+ print( "Number of input tokens: " + str(input_num_tokens))
40
+ print("Length of input: " + str(len(input_text)))
41
+
42
+ # Does not seem to care if it goes over 512... humm...
43
+ outputs = model.generate(input_ids, max_new_tokens=512)
44
+
45
+ # Remove <pad> and </s> eof sequence tokens
46
+ model_output_text = tokenizer.decode(outputs[0],skip_special_tokens=True)
47
+
48
+ print("Number of output tokens: " + str(outputs.shape[1]))
49
+ print("length of output: " + str(len(model_output_text)))
50
+ print("Output: " + model_output_text)
51
+ output_text = input_text + model_output_text
52
+ return output_text
53
+
54
+
55
+ with gr.Blocks() as demo:
56
+ gr.Markdown(
57
+ """
58
+ # Flan T5 Large Demo
59
+ 780M parameter Large language model fine tuned on diverse tasks.
60
+ Prompt the model in the Input box. Models output is appended to input. To get additional generation hit submit again.
61
+ """)
62
+ txt_in = gr.Textbox(label="Input", lines=8)
63
+ correct_label = gr.Label(label="Correct")
64
+ # txt_out = gr.Textbox(value="", label="Output", lines=4)
65
+
66
+
67
+ btn = gr.Button(value="Submit")
68
+ # Send back to inputs
69
+ btn.click(text2text, inputs=[txt_in], outputs=[txt_in])
70
+
71
+
72
+ gr.Examples(
73
+ examples=get_examples(),
74
+ inputs=[txt_in,correct_label]
75
+ )
76
+
77
+
78
+ if __name__ == "__main__":
79
+ demo.launch()