Lautaro Cardarelli commited on
Commit
f555fb0
1 Parent(s): 23ea224

add qa model

Browse files
Files changed (2) hide show
  1. app.py +19 -4
  2. requirements.txt +2 -1
app.py CHANGED
@@ -7,6 +7,8 @@ from transformers import BartForConditionalGeneration
7
  from transformers import BartTokenizer
8
  from transformers import PreTrainedModel
9
  from transformers import PreTrainedTokenizer
 
 
10
 
11
  tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
12
  model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
@@ -94,9 +96,22 @@ def generate_summary(text):
94
  summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
95
  return summary
96
 
 
 
 
 
 
97
 
98
- def generate_question_response(question):
99
- return f'response: {question}'
 
 
 
 
 
 
 
 
100
 
101
 
102
  class SummarizerAndQA:
@@ -114,9 +129,9 @@ class SummarizerAndQA:
114
  self.summary = generate_summary(text)
115
  self.study_generated_questions = generate_questions(text)
116
 
117
- if question != self.question:
118
  self.question = question
119
- self.question_response = generate_question_response(question)
120
 
121
  return self.summary, self.study_generated_questions, self.question_response
122
 
 
7
  from transformers import BartTokenizer
8
  from transformers import PreTrainedModel
9
  from transformers import PreTrainedTokenizer
10
+ from transformers import AutoTokenizer
11
+ from transformers import AutoModelForSeq2SeqLM
12
 
13
  tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
14
  model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
 
96
  summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
97
  return summary
98
 
99
+ # QA
100
+ # Cargar el modelo para preguntas y respuestas
101
+ qa_model_name = "MaRiOrOsSi/t5-base-finetuned-question-answering"
102
+ qa_tokenizer = AutoTokenizer.from_pretrained(qa_model_name)
103
+ qa_model = AutoModelForSeq2SeqLM.from_pretrained(qa_model_name)
104
 
105
+
106
+ def generate_question_response(question, context):
107
+ # Formar el input para el modelo de preguntas y respuestas
108
+ input_text = f"question: {question} context: {context}"
109
+ encoded_input = qa_tokenizer(input_text, return_tensors='pt', max_length=1024, truncation=True)
110
+ output = qa_model.generate(input_ids=encoded_input['input_ids'], attention_mask=encoded_input['attention_mask'])
111
+ response_en = qa_tokenizer.decode(output[0], skip_special_tokens=True)
112
+ translator = Translator()
113
+ translated_response = translator.translate(response_en, dest='es').text
114
+ return f'Respuesta: {translated_response}'
115
 
116
 
117
  class SummarizerAndQA:
 
129
  self.summary = generate_summary(text)
130
  self.study_generated_questions = generate_questions(text)
131
 
132
+ if question != self.question and text != '':
133
  self.question = question
134
+ self.question_response = generate_question_response(question, text)
135
 
136
  return self.summary, self.study_generated_questions, self.question_response
137
 
requirements.txt CHANGED
@@ -5,4 +5,5 @@ accelerate
5
  # We are using this fork since the orignal google library uses an old package version of httpx
6
  # which is not compatible with the last version of gradio
7
  googletrans-py
8
- sentencepiece
 
 
5
  # We are using this fork since the orignal google library uses an old package version of httpx
6
  # which is not compatible with the last version of gradio
7
  googletrans-py
8
+ sentencepiece
9
+ protobuf