|
import streamlit as st |
|
from transformers import AutoTokenizer |
|
import torch |
|
from transformers import AutoModelForQuestionAnswering |
|
import pandas as pd |
|
|
|
from PIL import Image |
|
image = Image.open('amazon.jpg') |
|
|
|
st.image(image, caption='Photo by Ivars Utināns on Unsplash') |
|
|
|
st.header('QA from Context') |
|
|
|
st.write("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.") |
|
|
|
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." |
|
|
|
|
|
|
|
|
|
model_cpkt = "deepset/minilm-uncased-squad2" |
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_cpkt) |
|
|
|
model = AutoModelForQuestionAnswering.from_pretrained(model_cpkt) |
|
|
|
|
|
question = st.text_input('Please ask a question about the Amazon') |
|
|
|
inputs = tokenizer(question,context,return_tensors="pt") |
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
|
|
|
|
start_logits = outputs.start_logits |
|
end_logits = outputs.end_logits |
|
|
|
|
|
start_idx = torch.argmax(start_logits) |
|
end_idx = torch.argmax(end_logits) + 1 |
|
answer_span = inputs["input_ids"][0][start_idx:end_idx] |
|
answer = tokenizer.decode(answer_span) |
|
|
|
try: |
|
if len(question) >5: |
|
st.write(f"Answer: {answer}") |
|
else: |
|
st.write('Please ask a longer question about the Amazon') |
|
|
|
except: |
|
st.write('There has been an error') |