Spaces:
Sleeping
Sleeping
import sentencepiece | |
import gradio as gr | |
import re | |
import torch | |
from transformers import T5Tokenizer, T5ForConditionalGeneration | |
# Load pre-trained model and tokenizer | |
tokenizer = T5Tokenizer.from_pretrained("ahmed792002/Finetuning_T5_HealthCare_Chatbot") | |
model = T5ForConditionalGeneration.from_pretrained("ahmed792002/Finetuning_T5_HealthCare_Chatbot") | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
# Function to clean input text | |
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 | |
# Chatbot function | |
def chatbot(query, history, system_message): | |
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, # Adjust this as needed for your use case | |
num_beams=5, | |
temperature=0.7, # Adjust this as needed | |
top_p=0.95, # Adjust this as needed | |
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(share=True) # Set `share=True` to create a public link | |