|
import streamlit as st |
|
from transformers import GPT2LMHeadModel, GPT2Tokenizer |
|
import time |
|
|
|
|
|
model_path = r"C:\Users\Kush\Desktop\dfuchatbot\nlp" |
|
tokenizer = GPT2Tokenizer.from_pretrained(model_path) |
|
model = GPT2LMHeadModel.from_pretrained(model_path) |
|
|
|
|
|
st.set_page_config(page_title="Diabetic Foot Ulcer Chatbot", page_icon=":hospital:") |
|
|
|
|
|
st.title("Diabetic Foot Ulcer Chatbot") |
|
st.markdown("Welcome to the Diabetic Foot Ulcer Chatbot. Ask any questions related to diabetic foot ulcers!") |
|
|
|
|
|
def chatbot(user_input): |
|
|
|
if user_input.lower() == 'exit': |
|
st.info("Chat ended. Goodbye!") |
|
return |
|
|
|
|
|
with st.spinner(text="Chatbot is typing..."): |
|
time.sleep(2) |
|
|
|
|
|
response = generate_response(user_input) |
|
|
|
|
|
st.text_area("Chatbot:", value=response, height=100, max_chars=500) |
|
|
|
|
|
def generate_response(user_input): |
|
|
|
input_ids = tokenizer.encode(user_input, return_tensors="pt") |
|
|
|
|
|
output = model.generate( |
|
input_ids, |
|
max_length=100, |
|
num_return_sequences=1, |
|
no_repeat_ngram_size=2, |
|
top_k=50, |
|
top_p=0.95, |
|
temperature=0.7, |
|
do_sample=True, |
|
pad_token_id=tokenizer.eos_token_id, |
|
early_stopping=True |
|
) |
|
|
|
|
|
return tokenizer.decode(output[0], skip_special_tokens=True) |
|
|
|
|
|
user_input = st.text_input("You:") |
|
|
|
|
|
if st.button("Send"): |
|
chatbot(user_input) |
|
|