Seungah Son commited on
Commit
aba0ac4
1 Parent(s): ca6ca1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -43
app.py CHANGED
@@ -2,56 +2,35 @@ import gradio as gr
2
  from transformers import BertForQuestionAnswering
3
  from transformers import BertTokenizerFast
4
  import torch
5
- from nltk.tokenize import word_tokenize
6
- import timm
7
 
8
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
9
  tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')
10
- #model = BertForQuestionAnswering.from_pretrained("bert-base-uncased")
11
  model = BertForQuestionAnswering.from_pretrained("CountingMstar/ai-tutor-bert-model").to(device)
12
 
13
  def get_prediction(context, question):
14
- inputs = tokenizer.encode_plus(question, context, return_tensors='pt').to(device)
15
- outputs = model(**inputs)
16
-
17
- answer_start = torch.argmax(outputs[0])
18
- answer_end = torch.argmax(outputs[1]) + 1
19
-
20
- answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(inputs['input_ids'][0][answer_start:answer_end]))
21
-
22
- return answer
23
-
24
- def question_answer(context, question):
25
- prediction = get_prediction(context,question)
26
- return prediction
27
 
28
- def split(text):
29
- context, question = '', ''
30
- act = False
31
- tmp = ''
32
- for t in text:
33
- tmp += t
34
- if len(tmp) == 4:
35
- tmp = tmp[1:]
36
- if tmp == '///':
37
- act = True
38
 
39
- if act == True:
40
- question += t
 
41
 
42
- if act == False:
43
- context += t
44
-
45
- return context[:-2], question[1:]
46
-
47
- def greet(texts):
48
- context, question = split(texts)
49
- answer = question_answer(context, question)
50
- return answer
51
- # def greet(text):
52
- # context, question = split(text)
53
- # # answer = question_answer(context, question)
54
- # return context
55
 
56
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
57
- iface.launch()
 
2
  from transformers import BertForQuestionAnswering
3
  from transformers import BertTokenizerFast
4
  import torch
 
 
5
 
6
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
7
  tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')
 
8
  model = BertForQuestionAnswering.from_pretrained("CountingMstar/ai-tutor-bert-model").to(device)
9
 
10
  def get_prediction(context, question):
11
+ inputs = tokenizer.encode_plus(question, context, return_tensors='pt').to(device)
12
+ outputs = model(**inputs)
13
+
14
+ answer_start = torch.argmax(outputs.start_logits)
15
+ answer_end = torch.argmax(outputs.end_logits) + 1
16
+
17
+ answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(inputs['input_ids'][0][answer_start:answer_end]))
18
+
19
+ return answer
 
 
 
 
20
 
21
+ def question_answer(context, question):
22
+ prediction = get_prediction(context, question)
23
+ return prediction
 
 
 
 
 
 
 
24
 
25
+ examples_text = [["A large language model (LLM) is a type of language model notable for its ability to achieve general-purpose language understanding and generation. LLMs acquire these abilities by using massive amounts of data to learn billions of parameters during training and consuming large computational resources during their training and operation.[1] LLMs are artificial neural networks (mainly transformers[2]) and are (pre-)trained using self-supervised learning and semi-supervised learning.","What is large language model?"],
26
+ ["Feature engineering or feature extraction or feature discovery is the process of extracting features (characteristics, properties, attributes) from raw data. Due to deep learning networks, such as convolutional neural networks, that are able to learn features by themselves, domain-specific-based feature engineering has become obsolete for vision and speech processing. Other examples of features in physics include the construction of dimensionless numbers such as Reynolds number in fluid dynamics; then Nusselt number in heat transfer; Archimedes number in sedimentation; construction of first approximations of the solution such as analytical strength of materials solutions in mechanics, etc.", "What is Feature engineering?"],
27
+ ["It calculates soft weights for each word, more precisely for its embedding, in the context window. It can do it either in parallel (such as in transformers) or sequentially (such as recurrent neural networks). Soft weights can change during each runtime, in contrast to hard weights, which are (pre-)trained and fine-tuned and remain frozen afterwards. Attention was developed to address the weaknesses of recurrent neural networks, where words in a sentence are slowly processed one at a time. Machine learning-based attention is a mechanism mimicking cognitive attention. Recurrent neural networks favor more recent words at the end of a sentence while earlier words fade away in volatile neural activations. Attention gives all words equal access to any part of a sentence in a faster parallel scheme and no longer suffers the wait time of serial processing. Earlier uses attached this mechanism to a serial recurrent neural network's language translation system (below), but later uses in Transformers large language models removed the recurrent neural network and relied heavily on the faster parallel attention scheme.", "What is Attention mechanism?"]]
28
 
29
+ iface = gr.Interface(
30
+ fn=question_answer,
31
+ inputs=[gr.Textbox("Context"), gr.Textbox("Question")],
32
+ outputs=gr.Textbox("Answer"),
33
+ examples=examples_text
34
+ )
 
 
 
 
 
 
 
35
 
36
+ iface.launch()