SocialScrape commited on
Commit
ca412a7
·
verified ·
1 Parent(s): 79feba9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import gradio as gr
4
+
5
+ MODEL_NAME = "SocialScrape/longformer-my-classifier" # твоя модель на Hugging Face
6
+
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
8
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
9
+
10
+ def classify_text(text):
11
+ inputs = tokenizer(text, return_tensors="pt", truncation=True)
12
+ outputs = model(**inputs)
13
+ probabilities = torch.softmax(outputs.logits, dim=-1)[0].detach().numpy()
14
+ predicted_class = probabilities.argmax()
15
+
16
+ return {
17
+ "Клас": int(predicted_class),
18
+ "Ймовірності класів": probabilities.tolist()
19
+ }
20
+
21
+ iface = gr.Interface(
22
+ fn=classify_text,
23
+ inputs=gr.Textbox(label="Введіть текст для класифікації"),
24
+ outputs=gr.JSON(label="Результати класифікації"),
25
+ title="Longformer UK Text Classifier"
26
+ )
27
+
28
+ iface.launch()