Yerzhxn commited on
Commit
67773b7
·
verified ·
1 Parent(s): ebb24cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py CHANGED
@@ -1,6 +1,13 @@
1
  import streamlit as st
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
 
 
 
 
 
 
 
4
 
5
  # Замените 'username/имя-вашей-модели' на путь к вашей модели на Hugging Face
6
  model_name = 'Yerzhxn/class_vac'
@@ -13,6 +20,25 @@ model = AutoModelForSequenceClassification.from_pretrained(model_name)
13
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
  model.to(device)
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # Интерфейс Streamlit
17
  st.title("Тестирование классификации текста")
18
  st.write("Введите текст, чтобы узнать предсказанный класс.")
@@ -21,7 +47,9 @@ st.write("Введите текст, чтобы узнать предсказа
21
  input_text = st.text_area("Введите текст здесь", "")
22
 
23
  if st.button("Предсказать"):
 
24
  if input_text:
 
25
  # Преобразование текста в формат, подходящий для модели
26
  inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
27
  inputs = {key: value.to(device) for key, value in inputs.items()}
 
1
  import streamlit as st
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
+ import re
5
+ from nltk.tokenize import RegexpTokenizer
6
+ from bs4 import BeautifulSoup as bs
7
+ from nltk.corpus import stopwords
8
+ import nltk
9
+ nltk.download('stopwords')
10
+ nltk.download('punkt')
11
 
12
  # Замените 'username/имя-вашей-модели' на путь к вашей модели на Hugging Face
13
  model_name = 'Yerzhxn/class_vac'
 
20
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21
  model.to(device)
22
 
23
+ def preprocess(sentence):
24
+ soup = bs(sentence, features="html.parser")
25
+ sentence = soup.get_text()
26
+ soup = bs(sentence, features="html.parser")
27
+ sentence = soup.get_text()
28
+
29
+ sentence = str(sentence)
30
+ sentence = sentence.lower()
31
+ sentence = sentence.replace('{html}',"")
32
+ cleanr = re.compile('<.*?>')
33
+ cleantext = re.sub(cleanr, '', sentence)
34
+ rem_url = re.sub(r'http\S+', '',cleantext)
35
+ rem_num = re.sub('[0-9]+', '', rem_url)
36
+ tokenizer = RegexpTokenizer(r'\w+')
37
+ tokens = tokenizer.tokenize(rem_num)
38
+ filtered_words = [w for w in tokens if not w in stopwords.words('russian')]
39
+
40
+ return " ".join(filtered_words)
41
+
42
  # Интерфейс Streamlit
43
  st.title("Тестирование классификации текста")
44
  st.write("Введите текст, чтобы узнать предсказанный класс.")
 
47
  input_text = st.text_area("Введите текст здесь", "")
48
 
49
  if st.button("Предсказать"):
50
+
51
  if input_text:
52
+ input_text = input_text.apply(lambda x: preprocess(x))
53
  # Преобразование текста в формат, подходящий для модели
54
  inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
55
  inputs = {key: value.to(device) for key, value in inputs.items()}