enigmaize commited on
Commit
5e2ba76
·
verified ·
1 Parent(s): 4ccac33

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
2
+ from scipy.special import softmax
3
+ import gradio as gr
4
+ import torch
5
+
6
+ model_name = "enigmaize/arxiv-nlp_project-scibert"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
+
10
+ # Имена классов (в порядке, соответствующем вашему `num_labels`)
11
+ labels = ['math.AC', 'cs.CV', 'cs.AI', 'cs.SY', 'math.GR', 'cs.CE', 'cs.PL', 'cs.IT', 'cs.DS', 'cs.NE', 'math.ST']
12
+
13
+ def classify_text(text):
14
+ # Токенизация текста
15
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
16
+
17
+ # Инференс
18
+ with torch.no_grad():
19
+ outputs = model(**inputs)
20
+ logits = outputs.logits
21
+
22
+ # Применение softmax для получения вероятностей
23
+ probabilities = torch.nn.functional.softmax(logits, dim=-1).squeeze().tolist()
24
+
25
+ # Создание словаря метка -> вероятность
26
+ results = {label: prob for label, prob in zip(labels, probabilities)}
27
+
28
+ # Сортировка по вероятности (по убыванию)
29
+ sorted_results = dict(sorted(results.items(), key=lambda item: item[1], reverse=True))
30
+
31
+ return sorted_results
32
+
33
+ # Описание интерфейса
34
+ description = "Enter the abstract of a scientific paper, and the model will predict its arXiv category."
35
+
36
+ # Создание интерфейса Gradio
37
+ interface = gr.Interface(
38
+ fn=classify_text, # Функция, которая будет вызываться
39
+ inputs=gr.Textbox(lines=10, placeholder="Paste abstract here...", label="Paper Abstract"), # Вход: текстовое поле
40
+ outputs=gr.Label(num_top_classes=3, label="Predicted Categories"), # Выход: метки с вероятностями
41
+ title="ArXiv Paper Classifier (SciBERT)",
42
+ description=description,
43
+ examples=[
44
+ ["We propose a novel deep learning approach for image recognition using convolutional neural networks."],
45
+ ["We analyze the computational complexity of algorithms for sorting and searching."],
46
+ ["This paper presents a statistical method for analyzing the spread of infectious diseases in populations."]
47
+ ] # Примеры для удобства
48
+ )
49
+
50
+ # Запуск интерфейса (это нужно для локального запуска, не для Spaces)
51
+ # interface.launch()
52
+
53
+ # Для Hugging Face Spaces, просто укажите интерфейс
54
+ interface.launch()