File size: 2,524 Bytes
224cd33
d182e77
 
b3ae3fe
f68bac7
d182e77
 
4372d3f
d182e77
 
b3ae3fe
 
 
 
 
 
4372d3f
b3ae3fe
 
4372d3f
d182e77
 
 
 
b3ae3fe
f0c301d
 
b3ae3fe
8cc9f0a
 
 
 
 
 
 
 
 
 
b615c79
f0c301d
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

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()