annafilina commited on
Commit
3047c0d
·
1 Parent(s): efdcad5

Update pages/answers.py

Browse files
Files changed (1) hide show
  1. pages/answers.py +39 -14
pages/answers.py CHANGED
@@ -43,6 +43,36 @@ pairs = [
43
  ]
44
 
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  def get_answer(context, question):
47
  nlp = pipeline('question-answering', model=model, tokenizer=tokenizer)
48
  QA_input = {'question': question, 'context': context}
@@ -54,27 +84,24 @@ def get_answer(context, question):
54
  def main():
55
  st.title("Question Answering App :robot_face:")
56
  st.divider()
57
- st.markdown("### **Enter the context and question, then click on ':blue[Get Answer]' to retrieve the answer:**")
58
 
59
  context_placeholder = "Enter the context here..."
60
  question_placeholder = "Enter the question here..."
61
 
62
- context = st.text_area("**:blue[Context]**", value=context_placeholder)
63
- question = st.text_input("**:blue[Question]**", value=question_placeholder)
64
 
65
- if st.button(":blue[**Random Example**]"):
66
  pair = random.choice(pairs)
67
- context.text_area("**:blue[Context]**", value=pair['context'])
68
- question.text_input("**:blue[Question]**", value=pair['question'])
69
 
70
- if st.button(":blue[**Get Answer**]"):
71
- context_value = context.text_area("**:blue[Context]**")
72
- question_value = question.text_input("**:blue[Question]**")
73
-
74
- if context_value.strip() == "" or question_value.strip() == "":
75
  st.warning("Please enter the context and question.")
76
  else:
77
- answer = get_answer(context_value, question_value)
78
  st.success(f"Answer: {answer}")
79
 
80
 
@@ -84,5 +111,3 @@ if __name__ == "__main__":
84
 
85
 
86
 
87
-
88
-
 
43
  ]
44
 
45
 
46
+ def get_answer(context, question):
47
+ nlp = pipeline('question-answering', model=model, tokenizer=tokenizer)
48
+ QA_input = {'question': question, 'context': context}
49
+ res = nlp(QA_input)
50
+ answer = res['answer']
51
+ return answer
52
+
53
+
54
+
55
+ Приношу извинения за неудобства. Кажется, я снова сделал ошибку. Для достижения желаемого поведения необходимо использовать st.sidebar для сохранения и обновления значений переменных context и question. Вот исправленный код:
56
+
57
+ python
58
+ Copy code
59
+ import random
60
+ import streamlit as st
61
+ from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
62
+
63
+
64
+ model_name = "deepset/roberta-base-squad2"
65
+ model = AutoModelForQuestionAnswering.from_pretrained(model_name)
66
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
67
+
68
+ pairs = [
69
+ {
70
+ 'context': "In recent years, climate change has become a pressing global issue. Rising temperatures, melting ice caps, and extreme weather events are all indicators of the changing climate. Governments around the world are implementing measures to reduce carbon emissions and promote renewable energy sources. The Paris Agreement, signed in 2015, aims to limit global warming to well below 2 degrees Celsius. Despite these efforts, challenges remain in achieving sustainable development while mitigating climate change.",
71
+ 'question': "What are some measures governments are taking to address climate change?"
72
+ }
73
+ ]
74
+
75
+
76
  def get_answer(context, question):
77
  nlp = pipeline('question-answering', model=model, tokenizer=tokenizer)
78
  QA_input = {'question': question, 'context': context}
 
84
  def main():
85
  st.title("Question Answering App :robot_face:")
86
  st.divider()
87
+ st.sidebar.markdown("### **Enter the context and question:**")
88
 
89
  context_placeholder = "Enter the context here..."
90
  question_placeholder = "Enter the question here..."
91
 
92
+ context = st.sidebar.text_area("Context", value=context_placeholder)
93
+ question = st.sidebar.text_input("Question", value=question_placeholder)
94
 
95
+ if st.sidebar.button("Random Example"):
96
  pair = random.choice(pairs)
97
+ context = pair['context']
98
+ question = pair['question']
99
 
100
+ if st.button("Get Answer"):
101
+ if context.strip() == "" or question.strip() == "":
 
 
 
102
  st.warning("Please enter the context and question.")
103
  else:
104
+ answer = get_answer(context, question)
105
  st.success(f"Answer: {answer}")
106
 
107
 
 
111
 
112
 
113