Spaces:
Runtime error
Runtime error
Chidam Gopal commited on
intent classifier app
Browse filesFiles changed (3)
- app.pyAdded+42
- infer_intent.pyAdded+64
- requirements.txtAdded+4
123456789101112131415161718192021222324252627282930313233343536373839404142import streamlit as stimport streamlit.components.v1 as componentsfrom infer_intent import IntentClassifierimport matplotlib.pyplot as plt
st.set_page_config(layout="wide")st.title("Intent classifier")
@st.cache_resourcedef get_intent_classifier(): cls = IntentClassifier() return cls
cls = get_intent_classifier()query = st.text_input("Enter a query", value="What is the weather today")pred_result, proba_result = cls.find_intent(query)
st.markdown(f"prediction = :green[{pred_result}]")keys = list(proba_result.keys())values = list(proba_result.values())
# Creating the bar plotfig, ax = plt.subplots()ax.barh(keys, values)
# Adding labels and titleax.set_xlabel('Intent')ax.set_ylabel('Values')ax.set_title('Intents probability score')
col1, col2 = st.columns([2,4])
with col1: st.pyplot(fig)
with col2: exp = st.expander("Explore training data") with exp: html_file = "reports/web_search_intents.html" with open(html_file, 'r', encoding='utf-8') as f: plotly_html = f.read() components.html(plotly_html, height=900, width=900)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364from transformers import AutoModelForSequenceClassification, AutoTokenizerimport torch
class IntentClassifier: def __init__(self): self.id2label = {0: 'information_intent', 1: 'yelp_intent', 2: 'navigation_intent', 3: 'travel_intent', 4: 'purchase_intent', 5: 'weather_intent', 6: 'translation_intent', 7: 'unknown'} self.label2id = {label:id for id,label in self.id2label.items()}
self.tokenizer = AutoTokenizer.from_pretrained("chidamnat2002/intent_classifier") self.intent_model = AutoModelForSequenceClassification.from_pretrained('chidamnat2002/intent_classifier', num_labels=8, torch_dtype=torch.bfloat16, id2label=self.id2label, label2id=self.label2id)
def find_intent(self, sequence, verbose=False): inputs = self.tokenizer(sequence, return_tensors="pt", # ONNX requires inputs in NumPy format padding="max_length", # Pad to max length truncation=True, # Truncate if the text is too long max_length=64)
self.intent_model.eval() with torch.no_grad(): outputs = self.intent_model(**inputs) logits = outputs.logits prediction = torch.argmax(logits, dim=1).item() probabilities = torch.softmax(logits, dim=1) rounded_probabilities = torch.round(probabilities, decimals=3) pred_result = self.id2label[prediction] proba_result = dict(zip(self.label2id.keys(), rounded_probabilities.tolist()[0])) if verbose: print(sequence + " -> " + pred_result) print(proba_result, "\n") return pred_result, proba_result
def main(): text_list = [ 'floor repair cost', 'pet store near me', 'who is the us president', 'italian food', 'sandwiches for lunch', "cheese burger cost", "What is the weather today", "what is the capital of usa", "cruise trip to carribean", ] cls = IntentClassifier() for sequence in text_list: cls.find_intent(sequence)
if __name__ == '__main__': main()1234transformers==4.45.1torch==2.4.1streamlit==1.38.0matplotlib==3.9.2app.pyAdded+42
infer_intent.pyAdded+64
requirements.txtAdded+4