quocanh944 commited on
Commit
d663586
1 Parent(s): 72cd379

Add application file

Browse files
Files changed (2) hide show
  1. app.py +35 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+
5
+ tokenizer = AutoTokenizer.from_pretrained("VietAI/vit5-base")
6
+ model = AutoModelForSeq2SeqLM.from_pretrained("quocanh944/viT5-med-qa")
7
+
8
+ def generate_answer(question):
9
+ global model, tokenizer
10
+ model.eval()
11
+ input_text = "hỏi: " + question
12
+ inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True, padding="max_length")
13
+ input_ids = inputs.input_ids
14
+ attention_mask = inputs.attention_mask
15
+
16
+ with torch.no_grad():
17
+ outputs = model.generate(input_ids=input_ids, attention_mask=attention_mask, max_length=128, num_beams=4, early_stopping=True)
18
+
19
+ answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
20
+ return answer
21
+
22
+ title = "Interactive demo: ViT5 with Medical Dataset"
23
+ description = "Demo for ViT5 with Medical Dataset. The model is fine-tuned on a Vietnamese medical dataset. The model is able to answer questions related to medical knowledge. Please input your question in the textbox and click submit to get the answer."
24
+ article = "This is a demo for ViT5 with Medical Dataset. The model is fine-tuned on a Vietnamese medical dataset. The model is able to answer questions related to medical knowledge. Please input your question in the textbox and click submit to get the answer."
25
+ examples = ["Tôi bị đau tay thì nên làm gì?", "Covid-19 là gì?", "Tôi nên làm gì khi bị sùi mào gà?", "Tôi nên ăn gì để tăng cân?"]
26
+
27
+ iface = gr.Interface(fn=generate_answer,
28
+ inputs=gr.Textbox(),
29
+ outputs=gr.Textbox(),
30
+ title=title,
31
+ description=description,
32
+ article=article,
33
+ examples=examples)
34
+
35
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers==4.40.2
2
+ torch==2.3.0