Monica P commited on
Commit
d697d80
1 Parent(s): 137b3fa
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer
3
+ import torch
4
+ from transformers import AutoModelForQuestionAnswering
5
+ import pandas as pd
6
+ import os
7
+
8
+
9
+ st.header('QA from Context')
10
+
11
+ st.text_input("The Amazon rainforest, also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations. The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.")
12
+
13
+ context = "The Amazon rainforest, also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations. The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."
14
+
15
+
16
+
17
+ # fine tuned miniLM that is fast to train
18
+ model_cpkt = "deepset/minilm-uncased-squad2"
19
+
20
+ tokenizer = AutoTokenizer.from_pretrained(model_cpkt)
21
+
22
+ model = AutoModelForQuestionAnswering.from_pretrained(model_cpkt)
23
+
24
+
25
+ question = st.text_input('Please ask a question about the Amazon')
26
+
27
+ inputs = tokenizer(question,context,return_tensors="pt") #pt is for Pytorch Tensor objects
28
+
29
+ with torch.no_grad():
30
+ outputs = model(**inputs)
31
+
32
+
33
+ start_logits = outputs.start_logits
34
+ end_logits = outputs.end_logits
35
+
36
+
37
+ start_idx = torch.argmax(start_logits)
38
+ end_idx = torch.argmax(end_logits) + 1
39
+ answer_span = inputs["input_ids"][0][start_idx:end_idx]
40
+ answer = tokenizer.decode(answer_span)
41
+
42
+
43
+ if len(question) > 5:
44
+ st.write(f"Answer: {answer}")
45
+ else:
46
+ st.write('Please ask a longer question about the Amazon')