File size: 1,748 Bytes
eeedcbc
93ff22f
 
eeedcbc
93ff22f
eeedcbc
3a4c449
93ff22f
 
f1cb50f
cc70292
3a4c449
93ff22f
 
 
 
 
 
3a4c449
 
f1cb50f
93ff22f
3a4c449
93ff22f
 
 
f1cb50f
93ff22f
f1cb50f
 
93ff22f
 
 
3a4c449
93ff22f
 
 
 
 
 
 
 
1363fd6
 
 
f1cb50f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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