Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import transformers
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the pre-trained language model
|
| 6 |
+
model_name = "bert-base-uncased"
|
| 7 |
+
model = transformers.pipeline("text-classification", model=model_name)
|
| 8 |
+
|
| 9 |
+
# Streamlit App
|
| 10 |
+
def main():
|
| 11 |
+
st.title("Sentence Category Classifier")
|
| 12 |
+
|
| 13 |
+
# Input search sentence
|
| 14 |
+
search_query = st.text_input("Enter a sentence:")
|
| 15 |
+
|
| 16 |
+
result = ""
|
| 17 |
+
|
| 18 |
+
# Process the search sentence when the user clicks the Search button
|
| 19 |
+
if st.button("Search"):
|
| 20 |
+
if search_query:
|
| 21 |
+
# Classify the sentence using the pre-trained model
|
| 22 |
+
categories = classify_sentence(search_query)
|
| 23 |
+
|
| 24 |
+
# Display the categories as output
|
| 25 |
+
if categories:
|
| 26 |
+
result = f"The sentence belongs to the following categories:\n\n"
|
| 27 |
+
for category in categories:
|
| 28 |
+
result += f"• {category}\n"
|
| 29 |
+
else:
|
| 30 |
+
result = "No categories found for the sentence."
|
| 31 |
+
|
| 32 |
+
# Display the result
|
| 33 |
+
st.text(result)
|
| 34 |
+
|
| 35 |
+
# Function to classify the sentence using the pre-trained language model
|
| 36 |
+
@st.cache(allow_output_mutation=True)
|
| 37 |
+
def classify_sentence(query):
|
| 38 |
+
# Classify the sentence using the pre-trained model
|
| 39 |
+
categories = model(query)
|
| 40 |
+
|
| 41 |
+
# Extract the category labels from the model's output
|
| 42 |
+
category_labels = [category['label'] for category in categories]
|
| 43 |
+
|
| 44 |
+
return category_labels
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
main()
|