Update app.py
Browse files
app.py
CHANGED
@@ -46,26 +46,46 @@
|
|
46 |
# main()
|
47 |
|
48 |
import streamlit as st
|
|
|
|
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
# Replace this function with your own logic to categorize sentences
|
53 |
-
categories = ['Restaurants', 'Food', 'Travel', 'New York City']
|
54 |
-
return categories
|
55 |
|
56 |
-
#
|
57 |
-
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
#
|
60 |
-
|
61 |
-
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
# main()
|
47 |
|
48 |
import streamlit as st
|
49 |
+
from sentence_transformers import SentenceTransformer
|
50 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
51 |
|
52 |
+
# Load pre-trained Sentence Transformer model
|
53 |
+
model = SentenceTransformer('bert-base-nli-mean-tokens')
|
|
|
|
|
|
|
54 |
|
55 |
+
# Define your dataset here (example categories)
|
56 |
+
categories = {
|
57 |
+
'sports': ['football', 'basketball', 'tennis'],
|
58 |
+
'politics': ['election', 'government', 'policy'],
|
59 |
+
'technology': ['AI', 'machine learning', 'data science']
|
60 |
+
}
|
61 |
|
62 |
+
# Function to get relevant categories based on user query
|
63 |
+
def get_relevant_categories(query):
|
64 |
+
query_embedding = model.encode([query])
|
65 |
+
category_scores = {}
|
66 |
|
67 |
+
for category, keywords in categories.items():
|
68 |
+
keyword_embeddings = model.encode(keywords)
|
69 |
+
similarity_scores = cosine_similarity(query_embedding, keyword_embeddings)
|
70 |
+
category_scores[category] = sum(similarity_scores)[0]
|
71 |
|
72 |
+
relevant_categories = [category for category, score in sorted(category_scores.items(), key=lambda x: x[1], reverse=True) if score > 0]
|
73 |
+
return relevant_categories
|
74 |
+
|
75 |
+
# Streamlit app layout and UI
|
76 |
+
def main():
|
77 |
+
st.title("Sentence Categorization App")
|
78 |
+
st.write("Enter a sentence to categorize:")
|
79 |
+
user_input = st.text_input('', value='', max_chars=None, key=None, type='default')
|
80 |
+
|
81 |
+
if st.button('Categorize'):
|
82 |
+
if user_input:
|
83 |
+
relevant_categories = get_relevant_categories(user_input)
|
84 |
+
st.write("Relevant Categories:")
|
85 |
+
for category in relevant_categories:
|
86 |
+
st.write(f"- {category}")
|
87 |
+
else:
|
88 |
+
st.write("Please enter a sentence for categorization.")
|
89 |
+
|
90 |
+
if __name__ == "__main__":
|
91 |
+
main()
|