Spaces:
Runtime error
Runtime error
# -*- coding: utf-8 -*- | |
""" | |
Created on Sun Dec 19 21:10:27 2021 | |
@author: Shaina Raza | |
""" | |
import streamlit as st | |
from GoogleNews import GoogleNews | |
#import torch | |
#from transformers import DistilBertTokenizer, DistilBertForQuestionAnswering | |
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline | |
from PIL import Image | |
#model_path = './covid_qa_distillbert' | |
#%% | |
def qna(): | |
model_name = 'shaina/covid_qa_mpnet' | |
nlp = pipeline('question-answering', model=model_name, tokenizer=model_name) | |
return nlp | |
def answergen(context, question): | |
"""CoQUAD""" | |
tokenizer = DistilBertTokenizer.from_pretrained(model_path,return_token_type_ids = True) | |
model = DistilBertForQuestionAnswering.from_pretrained(model_path, return_dict=False) | |
encoding = tokenizer.encode_plus(question, context) | |
input_ids, attention_mask = encoding["input_ids"], encoding["attention_mask"] | |
start_scores, end_scores = model(torch.tensor([input_ids]), attention_mask=torch.tensor([attention_mask])) | |
ans_tokens = input_ids[torch.argmax(start_scores) : torch.argmax(end_scores)+1] | |
answer_tokens = tokenizer.convert_ids_to_tokens(ans_tokens , skip_special_tokens=True) | |
answer_tokens_to_string = tokenizer.convert_tokens_to_string(answer_tokens) | |
return answer_tokens_to_string | |
def news(): | |
googlenews = GoogleNews() | |
googlenews = GoogleNews(lang='en', region='US') | |
googlenews = GoogleNews(period='7d') | |
googlenews.get_news('Covid19') | |
result=googlenews.result() | |
return result | |
#%% | |
def main(): | |
st.sidebar.header("COVID-19 Question Answering Dataset(CoQUAD)system") | |
wallpaper = Image.open('covid-removebg-preview.png') | |
wallpaper = wallpaper.resize((1400,700)) | |
st.sidebar.image(wallpaper) | |
st.sidebar.info("This project presents a COVID-19 Question Answering (CO-QA) system, which is a Web App that utilises AI and NLP techniques to answer questions about COVID-19 and post-COVID-19 from scientific articles related to COVID-19. The objective is to aid the medical community in addressing critical COVID-19-related questions.") | |
st.sidebar.header("Author") | |
st.sidebar.info("""-Shaina Raza""") | |
st.markdown("<div align='center'><br>" | |
"<img src='https://img.shields.io/badge/Domain-Medical-blue'" | |
"alt='API stability' height='25'/>" | |
"<img src='https://img.shields.io/badge/Model-CoQUAD-red'" | |
"alt='API stability' height='25'/>" | |
"<img src='https://img.shields.io/badge/Web%20App-Streamlit-yellow'" | |
"alt='API stability' height='25'/></div>", unsafe_allow_html=True) | |
st.write(" ") | |
nlp = qna() | |
select_input = st.radio("Select the input", | |
('Select from the examples', 'Type in your questions')) | |
if select_input == 'Select from the examples': | |
options = st.selectbox('Select the preset examples', | |
['', 'example1', 'example2', 'example3']) | |
if options != "": | |
if options == 'example1': | |
context = 'Coronavirus disease 2019 (COVID-19) is a contagious disease caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2). The first known case was identified in Wuhan, China, in December 2019.[7] The disease has since spread worldwide, leading to an ongoing pandemic.' | |
question = 'What is COVID-19?' | |
if options == 'example2': | |
context = 'The first known infections from SARS-CoV-2 were discovered in Wuhan, China. The original source of viral transmission to humans remains unclear, as does whether the virus became pathogenic before or after the spillover event.' | |
question = 'Where was COVID-19 first discovered?' | |
if options == 'example3': | |
context = 'Long COVID, also known as post-COVID-19 syndrome, post-acute sequelae of COVID-19 (PASC), or chronic COVID syndrome (CCS) is a condition characterized by long-term sequelae appearing or persisting after the typical convalescence period of COVID-19. Long COVID can affect nearly every organ system, with sequelae including respiratory system disorders, nervous system and neurocognitive disorders, mental health disorders, metabolic disorders, cardiovascular disorders, gastrointestinal disorders, malaise, fatigue, musculoskeletal pain, and anemia. A wide range of symptoms are commonly reported, including fatigue, headaches, shortness of breath, anosmia (loss of smell), parosmia (distorted smell), muscle weakness, low fever and cognitive dysfunction.' | |
question = 'What is Post-COVID syndrome?' | |
st.subheader('π² Question') | |
st.write(question) | |
st.subheader('π² Context') | |
st.write(context) | |
st.subheader("π² Answer") | |
try: | |
#answer = answergen(context, question) | |
QA_input = { | |
'question': question, | |
'context': context | |
} | |
res = nlp(QA_input) | |
answer = res['answer'] | |
st.success(answer) | |
except: | |
st.error("Sorry! Couldn't process the request") | |
if select_input == 'Type in your questions': | |
context = st.text_area("Enter your context or paragraph", "") | |
question = st.text_input("Enter the Question", "") | |
if st.button("Run QnA") and question != "": | |
st.subheader("π² Answer") | |
try: | |
#answer = answergen(context, question) | |
#if answer == "": | |
QA_input = { | |
'question': question, | |
'context': context | |
} | |
res = nlp(QA_input) | |
answer = res['answer'] | |
st.success(answer) | |
except: | |
st.error("Sorry! Couldn't process the request") | |
st.header("Latest Covid News") | |
latest_news = news() | |
for i in latest_news[0:5]: | |
st.write(i['title']) | |
st.image( | |
i['img'], | |
width=100, # Manually Adjust the width of the image as per requirement | |
) | |
link_ = i['link'] | |
link_ = link_.replace("news.google.com/.", "https://news.google.com") | |
link_path = "read this [article]" + "(" + link_ + ")" | |
st.write(link_path) | |
st.write(" ") | |
if __name__ == "__main__": | |
main() |