gojiteji commited on
Commit
dad54e8
1 Parent(s): 3715289

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -13
app.py CHANGED
@@ -5,8 +5,8 @@ from transformers import AutoTokenizer, AutoModelForMaskedLM, AutoModelForSeq2Se
5
  BERTTokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese")
6
  BERTModel = AutoModelForMaskedLM.from_pretrained("cl-tohoku/bert-base-japanese")
7
 
8
- mT5Tokenizer = AutoTokenizer.from_pretrained("google/mt5-base")
9
- mT5Model = AutoModelForSeq2SeqLM.from_pretrained("google/mt5-base")
10
 
11
  GPT2Tokenizer = AutoTokenizer.from_pretrained("rinna/japanese-gpt2-medium")
12
  GPT2Model = AutoModelForCausalLM.from_pretrained("rinna/japanese-gpt2-medium")
@@ -37,23 +37,23 @@ def BALTHASAR(sue):#mT5
37
  votes.append(1 if probs[-1][allow]>probs[-1][deny] else -1)
38
  return "承認" if probs[-1][allow]>probs[-1][deny] else "否定"
39
 
40
- def CASPER(sue):#GPT2
41
- allow=GPT2Tokenizer("承認").input_ids[1]
42
- deny=GPT2Tokenizer("否定").input_ids[1]
43
- inpt=GPT2Tokenizer('CASPERは女としての人格を持っています。人間とCASPERの対話です。人間「'+sue+'。承認 か 否定 のどちらかで答えてください。」'+"CASPER 「",return_tensors="pt")
44
- probs=GPT2Model(input_ids=inpt.input_ids[:,:-1],attention_mask=inpt.attention_mask[:,:-1]).logits[0]
45
- i=-1
46
- p_answer=probs
47
- id=torch.argmax(probs[i])
48
- votes.append(1 if probs[i][allow]>probs[i][deny] else -1)
49
- return "承認" if probs[i][allow]>probs[i][deny] else "否定"
50
 
51
  def greet(sue):
52
  text1="BERT-1"+MELCHIOR(sue)
53
  text2="GPT-2"+CASPER(sue)
54
- text3="mT5-3"+BALTHASAR(sue)
55
  return text1+" "+text2+" "+text3+"\n___\n\n"+("|可決|" if sum(votes[-3:])>0 else "| 否決 |")+"\n___"
56
 
 
57
  css="@import url('https://fonts.googleapis.com/css2?family=Shippori+Mincho:wght@800&display=swap'); .gradio-container {background-color: black} .gr-button {background-color: blue;color:black; weight:200%;font-family:'Shippori Mincho', serif;}"
58
  css+=".block{color:orange;} ::placeholder {font-size:35%} .gr-box {text-align: center;font-size: 125%;border-color:orange;background-color: #000000;weight:200%;font-family:'Shippori Mincho', serif;}:disabled {color: orange;opacity:1.0;}"
59
  with gr.Blocks(css=css) as demo:
 
5
  BERTTokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese")
6
  BERTModel = AutoModelForMaskedLM.from_pretrained("cl-tohoku/bert-base-japanese")
7
 
8
+ mBERTTokenizer = AutoTokenizer.from_pretrained("bert-base-multilingual-cased")
9
+ mBERTModel = AutoModelForMaskedLM.from_pretrained("bert-base-multilingual-cased")
10
 
11
  GPT2Tokenizer = AutoTokenizer.from_pretrained("rinna/japanese-gpt2-medium")
12
  GPT2Model = AutoModelForCausalLM.from_pretrained("rinna/japanese-gpt2-medium")
 
37
  votes.append(1 if probs[-1][allow]>probs[-1][deny] else -1)
38
  return "承認" if probs[-1][allow]>probs[-1][deny] else "否定"
39
 
40
+ def BALTHASAR(sue):#mBERT
41
+ allow=mBERTTokenizer("Yes").input_ids[1]
42
+ deny=mBERTTokenizer("No").input_ids[1]
43
+ output=mBERTModel(**mBERTTokenizer('MELCHIORは科学者としての人格を持っています。人間とMELCHIORの対話です。人間「'+sue+'。賛成か反対か。」'+"MELCHIOR[MASK]」",return_tensors="pt")).logits
44
+ mask=output[0,-3,:]
45
+ print(mBERTTokenizer.decode(torch.argmax(output[0,-3,:])))
46
+ votes.append(1 if mask[allow]>mask[deny] else -1)
47
+ return "承認" if mask[allow]>mask[deny] else "否定"
48
+
 
49
 
50
  def greet(sue):
51
  text1="BERT-1"+MELCHIOR(sue)
52
  text2="GPT-2"+CASPER(sue)
53
+ text3="mBERT-3"+BALTHASAR(sue)
54
  return text1+" "+text2+" "+text3+"\n___\n\n"+("|可決|" if sum(votes[-3:])>0 else "| 否決 |")+"\n___"
55
 
56
+
57
  css="@import url('https://fonts.googleapis.com/css2?family=Shippori+Mincho:wght@800&display=swap'); .gradio-container {background-color: black} .gr-button {background-color: blue;color:black; weight:200%;font-family:'Shippori Mincho', serif;}"
58
  css+=".block{color:orange;} ::placeholder {font-size:35%} .gr-box {text-align: center;font-size: 125%;border-color:orange;background-color: #000000;weight:200%;font-family:'Shippori Mincho', serif;}:disabled {color: orange;opacity:1.0;}"
59
  with gr.Blocks(css=css) as demo: