tuongvxx1 commited on
Commit
8865786
1 Parent(s): 89591ca

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import hf_hub_download
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
+
6
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
+ tokenizer = AutoTokenizer.from_pretrained("VietAI/vit5-base")
8
+ model = AutoModelForSeq2SeqLM.from_pretrained("VietAI/vit5-base")
9
+ model_file = hf_hub_download(repo_id="tuongvxx1/medBot", filename="medicalBot_ver4.pth")
10
+ state_dict = torch.load(model_file, map_location=torch.device('cpu'))
11
+ model.load_state_dict(new_state_dict)
12
+ model.to(device)
13
+
14
+ def generate_answer(question, model, tokenizer, device):
15
+ model.eval()
16
+ input_text = "hỏi: " + question
17
+ inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True, padding="max_length")
18
+ input_ids = inputs.input_ids.to(device)
19
+ attention_mask = inputs.attention_mask.to(device)
20
+
21
+ with torch.no_grad():
22
+ outputs = model.generate(input_ids=input_ids, attention_mask=attention_mask, max_length=128, num_beams=4, early_stopping=True)
23
+ answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
+ return answer
25
+
26
+ def run(ques):
27
+ return generate_answer(ques, model, tokenizer, device)
28
+
29
+ demo = gr.Interface(fn=run, inputs=gr.Textbox(label="Nhập câu hỏi"), outputs=gr.Textbox(label="Câu trả lời"))
30
+ demo.launch()