Evelyn18 commited on
Commit
02c3f80
1 Parent(s): f8a3f8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -39
app.py CHANGED
@@ -1,44 +1,26 @@
1
- from transformers import AutoModelForCausalLM, AutoTokenizer
2
- import torch
3
-
4
- tokenizer = AutoTokenizer.from_pretrained("Evelyn18/roberta-base-spanish-squades-becasv3")
5
- model = AutoModelForCausalLM.from_pretrained("Evelyn18/roberta-base-spanish-squades-becasv3")
6
-
7
- def predict(input, history=[]):
8
- # tokenize the new input sentence
9
- new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
10
-
11
- # append the new user input tokens to the chat history
12
- bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
13
 
14
- # generate a response
15
- history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
16
 
17
- # convert the tokens to text, and then split the responses into lines
18
- response = tokenizer.decode(history[0]).split("<|endoftext|>")
19
- response.remove("")
20
-
21
- # write some HTML
22
- html = "<div class='chatbot'>"
23
- for m, msg in enumerate(response):
24
- cls = "user" if m%2 == 0 else "bot"
25
- html += "<div class='msg {}'> {}</div>".format(cls, msg)
26
- html += "</div>"
27
-
28
- return html, history
29
 
30
- import gradio as gr
31
 
32
- css = """
33
- .chatbox {display:flex;flex-direction:column}
34
- .msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
35
- .msg.user {background-color:cornflowerblue;color:white}
36
- .msg.bot {background-color:lightgray;align-self:self-end}
37
- .footer {display:none !important}
38
- """
39
 
40
- gr.Interface(fn=predict,
41
- theme="default",
42
- inputs=[gr.inputs.Textbox(placeholder="How are you?"), "state"],
43
- outputs=["html", "state"],
44
- css=css).launch()
 
 
1
+ import os
2
+ import sys
3
+ from transformers import pipeline
4
+ import gradio as gr
 
 
 
 
 
 
 
 
5
 
6
+ model = pipeline('question-answering', model='deepset/tinyroberta-squad2', tokenizer='deepset/tinyroberta-squad2')
 
7
 
8
+ def qa(passage, question):
9
+ question = question
10
+ context = passage
11
+ nlp_input = {
12
+ 'question': question,
13
+ 'context': context
14
+ }
 
 
 
 
 
15
 
16
+ return model(nlp_input)['answer']
17
 
18
+ passage = "The quick brown fox jumped over the lazy dog."
19
+ question = "Who jumps over the lazy dog?"
 
 
 
 
 
20
 
21
+ iface = gr.Interface(qa,
22
+ title="Question Answering using RoBERTa",
23
+ inputs=[gr.inputs.Textbox(lines=15), "text"],
24
+ outputs=["text"],
25
+ examples=[["{}".format(passage), "{}".format(question)]])
26
+ iface.launch()