|
|
|
|
|
import gradio as gr |
|
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline |
|
import torch |
|
|
|
|
|
device = 0 if torch.cuda.is_available() else -1 |
|
model_name = "microsoft/BioGPT" |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
|
|
|
|
chatbot = pipeline( |
|
"text-generation", |
|
model=model, |
|
tokenizer=tokenizer, |
|
device=device, |
|
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32 |
|
) |
|
|
|
|
|
def get_response(user_input): |
|
response = chatbot( |
|
user_input, |
|
max_length=75, |
|
num_return_sequences=1, |
|
truncation=True |
|
) |
|
return response[0]['generated_text'] |
|
|
|
|
|
def chatbot_interface(user_input): |
|
return get_response(user_input) |
|
|
|
iface = gr.Interface( |
|
fn=chatbot_interface, |
|
inputs=gr.Textbox(lines=2, placeholder="Enter your symptoms here..."), |
|
outputs="text", |
|
title="Health Chatbot", |
|
description="Ask your symptoms and get advice!", |
|
theme="default" |
|
) |
|
|
|
|
|
iface.launch(share=True) |
|
|