Medical-Bot / app.py
Sanchit2207's picture
Update app.py
f0c301d verified
raw
history blame
1 kB
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torch
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")
answer_start_scores, answer_end_scores = model(**inputs).values()
start = torch.argmax(answer_start_scores)
end = torch.argmax(answer_end_scores) + 1
answer = tokenizer.convert_tokens_to_string(
tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][start:end])
)
return answer
import gradio as gr
def chatbot_response(question):
context = "Include a reliable medical knowledge base here like PubMed abstracts or simplified texts."
return answer_question(context, question)
iface = gr.Interface(fn=chatbot_response, inputs="text", outputs="text", title="Medical Chatbot")
iface.launch()