Chillyblast commited on
Commit
1d5a90b
·
verified ·
1 Parent(s): 200b888

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import RobertaTokenizer, RobertaForSequenceClassification
4
+ from datasets import load_dataset, Dataset
5
+ import pandas as pd
6
+
7
+ # Load the dataset
8
+ ds = load_dataset("bitext/Bitext-customer-support-llm-chatbot-training-dataset")
9
+
10
+ # Convert the dataset to a pandas DataFrame
11
+ df = ds['train'].to_pandas()
12
+
13
+ # Define labels based on your intent categories
14
+ label2id = {label: idx for idx, label in enumerate(df['intent'].unique())}
15
+ id2label = {idx: label for label, idx in label2id.items()}
16
+
17
+ # Encode labels
18
+ df['label'] = df['intent'].map(label2id)
19
+
20
+ # Ensure 'instruction', 'label', 'intent', and 'response' columns are included
21
+ df = df[['instruction', 'label', 'intent', 'response']]
22
+
23
+ # Load the tokenizer and model
24
+ tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
25
+ model = RobertaForSequenceClassification.from_pretrained('path_to_your_saved_model_directory')
26
+
27
+ # Ensure the model is in evaluation mode
28
+ model.eval()
29
+
30
+ # Function to get the predicted intent and response
31
+ def get_intent_and_response(instruction):
32
+ # Tokenize the input instruction
33
+ inputs = tokenizer(instruction, return_tensors="pt", truncation=True, padding='max_length', max_length=128)
34
+
35
+ # Perform inference
36
+ with torch.no_grad():
37
+ outputs = model(**inputs)
38
+ logits = outputs.logits
39
+ predicted_label_id = torch.argmax(logits, dim=1).item()
40
+
41
+ # Decode the predicted label to get the intent
42
+ predicted_intent = id2label[predicted_label_id]
43
+
44
+ # Fetch the appropriate response based on the predicted intent
45
+ response = df[df['intent'] == predicted_intent].iloc[0]['response']
46
+
47
+ return predicted_intent, response
48
+
49
+ # Streamlit app setup
50
+ st.title("Customer Support Chatbot")
51
+ st.write("Ask a question, and I'll do my best to help you.")
52
+
53
+ instruction = st.text_input("You:")
54
+
55
+ if st.button("Submit"):
56
+ if instruction:
57
+ predicted_intent, response = get_intent_and_response(instruction)
58
+ st.write(f"**Predicted Intent:** {predicted_intent}")
59
+ st.write(f"**Assistant:** {response}")
60
+ else:
61
+ st.write("Please enter an instruction.")
62
+
63
+ if st.button("Exit"):
64
+ st.write("Exiting the chat.")