tuantc commited on
Commit
ab69189
·
1 Parent(s): d243699

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+
5
+ tokenizer = AutoTokenizer.from_pretrained("tuantc/flysugarbot")
6
+ model = AutoModelForSequenceClassification.from_pretrained("tuantc/flysugarbot")
7
+ model.eval()
8
+
9
+
10
+ def predict_intent(model, tokenizer, text_instance,label_map):
11
+ device = torch.device("cpu")
12
+ encoding = tokenizer(text_instance, return_tensors='pt', padding=True, truncation=True, max_length=512)
13
+ input_ids = encoding['input_ids'].to(device)
14
+ attention_masks = encoding['attention_mask'].to(device)
15
+ with torch.no_grad():
16
+ outputs = model(input_ids, attention_mask=attention_masks)
17
+ logits = outputs[0]
18
+ predictions = torch.argmax(logits, dim=-1).item()
19
+ predicted_label = label_map[predictions]
20
+ prob = F.softmax(logits, dim=1).max().item()
21
+ return predicted_label, prob
22
+ def bot_response(message, history):
23
+ predicted_intent, prob = predict_intent(model=model, tokenizer=tokenizer, text_instance=message,label_map=label_map)
24
+ return f"Intent này là {predicted_intent} với confidence {prob:.2f}"
25
+ demo = gr.ChatInterface(random_response)
26
+
27
+
28
+ gr.Interface(
29
+ fn=bot_response,
30
+ inputs=["text", "state"],
31
+ outputs=["chatbot", "state"],
32
+ theme="finlaymacklon/boxy_violet",
33
+ ).launch()