Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,40 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import sentencepiece
|
|
|
|
|
|
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("ahmed792002/Finetuning_T5_HealthCare_Chatbot")
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
return
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
-
|
| 27 |
if __name__ == "__main__":
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import sentencepiece
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import re
|
| 4 |
import torch
|
| 5 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 6 |
|
| 7 |
+
tokenizer = T5Tokenizer.from_pretrained("ahmed792002/Finetuning_T5_HealthCare_Chatbot")
|
| 8 |
+
model = T5ForConditionalGeneration.from_pretrained("ahmed792002/Finetuning_T5_HealthCare_Chatbot")
|
|
|
|
| 9 |
|
| 10 |
+
def clean_text(text):
|
| 11 |
+
text = re.sub(r'\r\n', ' ', text) # Remove carriage returns and line breaks
|
| 12 |
+
text = re.sub(r'\s+', ' ', text) # Remove extra spaces
|
| 13 |
+
text = re.sub(r'<.*?>', '', text) # Remove any XML tags
|
| 14 |
+
text = text.strip().lower() # Strip and convert to lower case
|
| 15 |
+
return text
|
| 16 |
+
def chatbot(query):
|
| 17 |
+
query = clean_text(query)
|
| 18 |
+
input_ids = tokenizer(query,return_tensors="pt",max_length=256,truncation=True)
|
| 19 |
+
inputs = {key: value.to(device) for key, value in input_ids.items()}
|
| 20 |
+
outputs = model.generate(
|
| 21 |
+
input_ids["input_ids"],
|
| 22 |
+
max_length=1024,
|
| 23 |
+
num_beams=5,
|
| 24 |
+
early_stopping=True
|
| 25 |
+
)
|
| 26 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 27 |
+
|
| 28 |
+
"""
|
| 29 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 30 |
+
"""
|
| 31 |
+
demo = gr.ChatInterface(
|
| 32 |
+
chatbot,
|
| 33 |
+
additional_inputs=[
|
| 34 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 35 |
+
],
|
| 36 |
)
|
| 37 |
|
| 38 |
+
|
| 39 |
if __name__ == "__main__":
|
| 40 |
+
demo.launch()
|