import sentencepiece import gradio as gr import re import torch from transformers import T5Tokenizer, T5ForConditionalGeneration tokenizer = T5Tokenizer.from_pretrained("ahmed792002/Finetuning_T5_HealthCare_Chatbot") model = T5ForConditionalGeneration.from_pretrained("ahmed792002/Finetuning_T5_HealthCare_Chatbot") def clean_text(text): text = re.sub(r'\r\n', ' ', text) # Remove carriage returns and line breaks text = re.sub(r'\s+', ' ', text) # Remove extra spaces text = re.sub(r'<.*?>', '', text) # Remove any XML tags text = text.strip().lower() # Strip and convert to lower case return text def chatbot(query): query = clean_text(query) input_ids = tokenizer(query,return_tensors="pt",max_length=256,truncation=True) inputs = {key: value.to(device) for key, value in input_ids.items()} outputs = model.generate( input_ids["input_ids"], max_length=1024, num_beams=5, early_stopping=True ) return tokenizer.decode(outputs[0], skip_special_tokens=True) """ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface """ demo = gr.ChatInterface( chatbot, additional_inputs=[ gr.Textbox(value="You are a friendly Chatbot.", label="System message"), ], ) if __name__ == "__main__": demo.launch()