T.Masuda commited on
Commit
3ec8592
1 Parent(s): 635f0e7

create app

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +49 -0
  3. requirements.txt +6 -0
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Text Generation Ja
3
  emoji: 🐨
4
  colorFrom: purple
5
  colorTo: red
 
1
  ---
2
+ title: Text Generation JA
3
  emoji: 🐨
4
  colorFrom: purple
5
  colorTo: red
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
4
+ from datetime import datetime
5
+
6
+ print('{}:loading...'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained('line-corporation/japanese-large-lm-1.7b', use_fast=False)
9
+ model = AutoModelForCausalLM.from_pretrained('line-corporation/japanese-large-lm-1.7b')
10
+ #tokenizer = AutoTokenizer.from_pretrained('line-corporation/japanese-large-lm-3.6b', use_fast=False)
11
+ #model = AutoModelForCausalLM.from_pretrained('line-corporation/japanese-large-lm-3.6b')
12
+
13
+ if torch.cuda.is_available():
14
+ model.half()
15
+ model = model.to('cuda')
16
+
17
+ generator = pipeline('text-generation', model=model, tokenizer=tokenizer, device=model.device)
18
+ print('{}:done.'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
19
+
20
+ def generate(input_text):
21
+ output = generator(
22
+ input_text,
23
+ max_length=128,
24
+ do_sample=True,
25
+ num_return_sequences=1,
26
+ pad_token_id=tokenizer.pad_token_id,
27
+ bos_token_id=tokenizer.bos_token_id,
28
+ eos_token_id=tokenizer.eos_token_id
29
+ )
30
+ generated_text = output[0]['generated_text']
31
+ return generated_text
32
+
33
+ with gr.Blocks(title='text generation ja') as chatbox:
34
+ gr.Markdown('# Text Generation JA')
35
+
36
+ chatbot = gr.Chatbot(label='generated text')
37
+ msg = gr.Textbox(label='text')
38
+ clear = gr.ClearButton([msg, chatbot])
39
+
40
+ def respond(message, chat_history):
41
+ if message == '':
42
+ return '', chat_history
43
+ bot_message = generate(message)
44
+ chat_history.append((message, bot_message))
45
+ return '', chat_history
46
+
47
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
48
+
49
+ chatbox.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ torchvision
4
+ torchaudio
5
+ transformers
6
+ sentencepiece