Medical-Bot / app.py
Sanchit2207's picture
Update app.py
9a008a9 verified
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torch
import gradio as gr
tokenizer = AutoTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
model = AutoModelForQuestionAnswering.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
def answer_question(context, question):
inputs = tokenizer.encode_plus(question, context, return_tensors="pt")
outputs = model(**inputs)
start_scores = outputs.start_logits
end_scores = outputs.end_logits
start = torch.argmax(start_scores)
end = torch.argmax(end_scores) + 1
if start >= end:
return "I couldn't find an answer."
answer = tokenizer.convert_tokens_to_string(
tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][start:end])
)
return answer
def chatbot_response(question):
context = (
"COVID-19 is a respiratory illness caused by the SARS-CoV-2 virus. It spreads mainly through respiratory droplets when an infected person coughs, sneezes, or talks. "
"The virus was first identified in December 2019 in Wuhan, China, and has since led to a global pandemic. Common symptoms include fever, cough, fatigue, and loss of taste or smell. "
"In severe cases, COVID-19 can lead to pneumonia, acute respiratory distress syndrome (ARDS), multi-organ failure, and even death. "
"It is particularly dangerous for older adults and people with underlying health conditions such as heart disease, diabetes, and respiratory conditions. "
"\n\n"
"Transmission: COVID-19 spreads mainly via close contact with infected individuals, especially when they are symptomatic. The virus can also spread through asymptomatic individuals, "
"making it challenging to control without preventive measures such as masks, social distancing, and quarantine. The virus can survive on surfaces for hours or days, so it's important to disinfect commonly touched objects and wash hands frequently."
"\n\n"
"Symptoms: In addition to fever, cough, and loss of taste or smell, some people may experience shortness of breath, body aches, headaches, sore throat, and gastrointestinal symptoms such as diarrhea. "
"Symptoms typically appear 2-14 days after exposure. The severity of symptoms can range from mild or asymptomatic cases to severe illness requiring hospitalization or ventilator support."
"\n\n")
return answer_question(context, question)
iface = gr.Interface(fn=chatbot_response, inputs="text", outputs="text", title="Medical Chatbot")
iface.launch()