Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,93 +0,0 @@
|
|
1 |
-
import torch
|
2 |
-
import streamlit as st
|
3 |
-
from transformers import RobertaTokenizer, RobertaForSequenceClassification
|
4 |
-
import re
|
5 |
-
import string
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
def tokenize_sentences(sentence):
|
10 |
-
encoded_dict = tokenizer.encode_plus(
|
11 |
-
sentence,
|
12 |
-
add_special_tokens=True,
|
13 |
-
max_length=128,
|
14 |
-
padding='max_length',
|
15 |
-
truncation=True,
|
16 |
-
return_attention_mask=True,
|
17 |
-
return_tensors='pt'
|
18 |
-
)
|
19 |
-
return torch.cat([encoded_dict['input_ids']], dim=0), torch.cat([encoded_dict['attention_mask']], dim=0)
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
def preprocess_query(query):
|
24 |
-
query = str(query).lower()
|
25 |
-
query = query.strip()
|
26 |
-
query=query.translate(str.maketrans("", "", string.punctuation))
|
27 |
-
return query
|
28 |
-
|
29 |
-
def predict_aspects(sentence, threshold):
|
30 |
-
input_ids, attention_mask = tokenize_sentences(sentence)
|
31 |
-
with torch.no_grad():
|
32 |
-
outputs = aspects_model(input_ids, attention_mask=attention_mask)
|
33 |
-
logits = outputs.logits
|
34 |
-
predicted_aspects = torch.sigmoid(logits).squeeze().tolist()
|
35 |
-
results = dict()
|
36 |
-
for label, prediction in zip(LABEL_COLUMNS_ASPECTS, predicted_aspects):
|
37 |
-
if prediction < threshold:
|
38 |
-
continue
|
39 |
-
precentage = round(float(prediction) * 100, 2)
|
40 |
-
results[label] = precentage
|
41 |
-
return results
|
42 |
-
|
43 |
-
|
44 |
-
# Load tokenizer and model
|
45 |
-
BERT_MODEL_NAME_FOR_ASPECTS_CLASSIFICATION = 'roberta-base'
|
46 |
-
tokenizer = RobertaTokenizer.from_pretrained(BERT_MODEL_NAME_FOR_ASPECTS_CLASSIFICATION, do_lower_case=True)
|
47 |
-
|
48 |
-
# Define the aspect labels
|
49 |
-
LABEL_COLUMNS_ASPECTS = [
|
50 |
-
'FOOD-CUISINE', 'FOOD-DEALS', 'FOOD-DIET_OPTION', 'FOOD-EXPERIENCE', 'FOOD-FLAVOR',
|
51 |
-
'FOOD-GENERAL', 'FOOD-INGREDIENT', 'FOOD-KITCHEN', 'FOOD-MEAL', 'FOOD-MENU',
|
52 |
-
'FOOD-PORTION', 'FOOD-PRESENTATION', 'FOOD-PRICE', 'FOOD-QUALITY', 'FOOD-RECOMMENDATION',
|
53 |
-
'FOOD-TASTE', 'GENERAL-GENERAL', 'RESTAURANT-ATMOSPHERE', 'RESTAURANT-BUILDING',
|
54 |
-
'RESTAURANT-DECORATION', 'RESTAURANT-EXPERIENCE', 'RESTAURANT-FEATURES',
|
55 |
-
'RESTAURANT-GENERAL', 'RESTAURANT-HYGIENE', 'RESTAURANT-KITCHEN', 'RESTAURANT-LOCATION',
|
56 |
-
'RESTAURANT-OPTIONS', 'RESTAURANT-RECOMMENDATION', 'RESTAURANT-SEATING_PLAN',
|
57 |
-
'RESTAURANT-VIEW', 'SERVICE-BEHAVIOUR', 'SERVICE-EXPERIENCE', 'SERVICE-GENERAL',
|
58 |
-
'SERVICE-WAIT_TIME'
|
59 |
-
]
|
60 |
-
|
61 |
-
# Load the model with the specified number of labels
|
62 |
-
aspects_model = RobertaForSequenceClassification.from_pretrained(
|
63 |
-
BERT_MODEL_NAME_FOR_ASPECTS_CLASSIFICATION,
|
64 |
-
num_labels=len(LABEL_COLUMNS_ASPECTS)
|
65 |
-
)
|
66 |
-
|
67 |
-
# Load the state dictionary
|
68 |
-
aspects_model.load_state_dict(torch.load('./Restaurant_Reviews (1).tsv',map_location=torch.device('cpu')),strict=False)
|
69 |
-
|
70 |
-
# Set the model to evaluation mode
|
71 |
-
aspects_model.eval()
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
# Streamlit App
|
76 |
-
st.title("Implicit and Explicit Aspect Extraction")
|
77 |
-
|
78 |
-
sentence = st.text_input("Enter a sentence:")
|
79 |
-
threshold = st.slider("Threshold", min_value=0.0, max_value=1.0, step=0.01, value=0.5)
|
80 |
-
|
81 |
-
if sentence:
|
82 |
-
processed_sentence = preprocess_query(sentence)
|
83 |
-
results = predict_aspects(processed_sentence, threshold)
|
84 |
-
if len(results) > 0:
|
85 |
-
st.write("Predicted Aspects:")
|
86 |
-
table_data = [["Category","Aspect", "Probability"]]
|
87 |
-
for aspect, percentage in results.items():
|
88 |
-
aspect_parts = aspect.split("-")
|
89 |
-
table_data.append(aspect_parts + [f"{percentage}%"])
|
90 |
-
st.table(table_data)
|
91 |
-
else:
|
92 |
-
st.write("No aspects above the threshold.")
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|