Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
+
|
| 6 |
+
MODEL_NAME = "SocialScrape/longformer-my-classifier"
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 10 |
+
|
| 11 |
+
# Функція класифікації
|
| 12 |
+
def classify_text(text):
|
| 13 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True)
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
probabilities = torch.softmax(outputs.logits, dim=-1)[0].detach().numpy()
|
| 16 |
+
predicted_class = probabilities.argmax()
|
| 17 |
+
|
| 18 |
+
return int(predicted_class), probabilities.tolist()
|
| 19 |
+
|
| 20 |
+
# Інтерфейс Streamlit
|
| 21 |
+
st.title("Longformer UK Text Classifier")
|
| 22 |
+
text_input = st.text_area("Введіть текст для класифікації")
|
| 23 |
+
|
| 24 |
+
if st.button("Класифікувати"):
|
| 25 |
+
if text_input.strip() != "":
|
| 26 |
+
predicted_class, probabilities = classify_text(text_input)
|
| 27 |
+
st.markdown("### Результати класифікації:")
|
| 28 |
+
st.write(f"**Клас:** {predicted_class}")
|
| 29 |
+
st.write(f"**Ймовірності класів:** {probabilities}")
|
| 30 |
+
else:
|
| 31 |
+
st.warning("Будь ласка, введіть текст для класифікації.")
|