Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	final
Browse files- app.py +11 -1
- pages/gpt.py +3 -0
- pages/models/mem.jpeg +0 -0
- pages/text_classif.py +4 -1
- pages/toxicity.py +45 -0
    	
        app.py
    CHANGED
    
    | @@ -1,5 +1,15 @@ | |
| 1 | 
             
            import streamlit as st
         | 
| 2 |  | 
| 3 | 
            -
            st.title(' | 
| 4 |  | 
| 5 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
             
            import streamlit as st
         | 
| 2 |  | 
| 3 | 
            +
            st.title('Проект по NLP(Было больно)')
         | 
| 4 |  | 
| 5 |  | 
| 6 | 
            +
            st.image('pages/models/mem.jpeg')
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            # st.write('Команда Помирающие:')
         | 
| 9 | 
            +
            # st.write('Дмитрий Будажапов')
         | 
| 10 | 
            +
            # st.write('Лариса Хлапушина')
         | 
| 11 | 
            +
            # st.write('Львов Даниил')
         | 
| 12 | 
            +
            st.write('<span style="font-size:40px;">Команда Помирающие</span>', unsafe_allow_html=True)
         | 
| 13 | 
            +
            st.write('<span style="font-size:25px;">1. Дмитрий Будажапов</span>', unsafe_allow_html=True)
         | 
| 14 | 
            +
            st.write('<span style="font-size:25px;">2. Лариса Хлапушина</span>', unsafe_allow_html=True)
         | 
| 15 | 
            +
            st.write('<span style="font-size:25px;">3. Львов Даниил</span>', unsafe_allow_html=True)
         | 
    	
        pages/gpt.py
    CHANGED
    
    | @@ -5,6 +5,9 @@ import torch | |
| 5 | 
             
            import transformers
         | 
| 6 | 
             
            from transformers import GPT2LMHeadModel, GPT2Tokenizer
         | 
| 7 |  | 
|  | |
|  | |
|  | |
| 8 | 
             
            text = st.text_input('Введите сюда вопрос или предложение для генерации текста')
         | 
| 9 |  | 
| 10 |  | 
|  | |
| 5 | 
             
            import transformers
         | 
| 6 | 
             
            from transformers import GPT2LMHeadModel, GPT2Tokenizer
         | 
| 7 |  | 
| 8 | 
            +
             | 
| 9 | 
            +
            st.title('Генерация текста модель rugpt3small_based_on_gpt2 ')
         | 
| 10 | 
            +
             | 
| 11 | 
             
            text = st.text_input('Введите сюда вопрос или предложение для генерации текста')
         | 
| 12 |  | 
| 13 |  | 
    	
        pages/models/mem.jpeg
    ADDED
    
    |   | 
    	
        pages/text_classif.py
    CHANGED
    
    | @@ -10,6 +10,9 @@ import time | |
| 10 | 
             
            from  pages.models.ml import predict
         | 
| 11 | 
             
            from pages.models.rnn import pred
         | 
| 12 |  | 
|  | |
|  | |
|  | |
| 13 | 
             
            text = st.text_input('Введите сюда отзыв')
         | 
| 14 | 
             
            if text:
         | 
| 15 | 
             
                # start_time = time.time()
         | 
| @@ -26,7 +29,7 @@ if text: | |
| 26 |  | 
| 27 | 
             
            # Создание DataFrame с указанными значениями
         | 
| 28 | 
             
            data = {
         | 
| 29 | 
            -
                'Параметр': ['ML', ' | 
| 30 | 
             
                'Значение': [0.52, 0.56, 0.62]
         | 
| 31 | 
             
            }
         | 
| 32 |  | 
|  | |
| 10 | 
             
            from  pages.models.ml import predict
         | 
| 11 | 
             
            from pages.models.rnn import pred
         | 
| 12 |  | 
| 13 | 
            +
             | 
| 14 | 
            +
            st.title('Классификация отзывов')
         | 
| 15 | 
            +
             | 
| 16 | 
             
            text = st.text_input('Введите сюда отзыв')
         | 
| 17 | 
             
            if text:
         | 
| 18 | 
             
                # start_time = time.time()
         | 
|  | |
| 29 |  | 
| 30 | 
             
            # Создание DataFrame с указанными значениями
         | 
| 31 | 
             
            data = {
         | 
| 32 | 
            +
                'Параметр': ['ML-TFIDF-LogReg', 'RNN', 'BERT-LaBSE-LogReg'],
         | 
| 33 | 
             
                'Значение': [0.52, 0.56, 0.62]
         | 
| 34 | 
             
            }
         | 
| 35 |  | 
    	
        pages/toxicity.py
    ADDED
    
    | @@ -0,0 +1,45 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import torch
         | 
| 2 | 
            +
            from transformers import AutoTokenizer, AutoModelForSequenceClassification
         | 
| 3 | 
            +
            import streamlit as st
         | 
| 4 | 
            +
            import pandas as pd
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            model_checkpoint = 'cointegrated/rubert-tiny-toxicity'
         | 
| 7 | 
            +
            tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
         | 
| 8 | 
            +
            model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint)
         | 
| 9 | 
            +
            if torch.cuda.is_available():
         | 
| 10 | 
            +
                model.cuda()
         | 
| 11 | 
            +
                
         | 
| 12 | 
            +
            def text2toxicity(text, aggregate=False):
         | 
| 13 | 
            +
                """ Calculate toxicity of a text (if aggregate=True) or a vector of toxicity aspects (if aggregate=False)"""
         | 
| 14 | 
            +
                with torch.no_grad():
         | 
| 15 | 
            +
                    inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True).to(model.device)
         | 
| 16 | 
            +
                    proba = torch.sigmoid(model(**inputs).logits).cpu().numpy()
         | 
| 17 | 
            +
                if isinstance(text, str):
         | 
| 18 | 
            +
                    proba = proba[0]
         | 
| 19 | 
            +
                if aggregate:
         | 
| 20 | 
            +
                    return 1 - proba.T[0] * (1 - proba.T[-1])
         | 
| 21 | 
            +
                return proba
         | 
| 22 | 
            +
             | 
| 23 | 
            +
             | 
| 24 | 
            +
            st.title("Определение уровня токсичности")
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            # Ввод предложения от пользователя
         | 
| 27 | 
            +
            input_text = st.text_input("Введите предложение:", "")
         | 
| 28 | 
            +
             
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            # Обработка входных данных через модель
         | 
| 31 | 
            +
            if input_text:
         | 
| 32 | 
            +
                # Вывод результатов
         | 
| 33 | 
            +
                my_dict = {
         | 
| 34 | 
            +
                'Не токсичный': (text2toxicity(input_text, False))[0],
         | 
| 35 | 
            +
                'Оскорбление': (text2toxicity(input_text, False))[1],
         | 
| 36 | 
            +
                'Непристойность': (text2toxicity(input_text, False))[2],
         | 
| 37 | 
            +
                'Угроза': (text2toxicity(input_text, False))[3],
         | 
| 38 | 
            +
                'Опасный': (text2toxicity(input_text, False))[4]
         | 
| 39 | 
            +
            }
         | 
| 40 | 
            +
                # my_dict['index'] = 'your_index_value'
         | 
| 41 | 
            +
                # st.write({text2toxicity(input_text, False)[0]: 'non-toxic'})
         | 
| 42 | 
            +
                
         | 
| 43 | 
            +
                df = pd.DataFrame(my_dict, index=['вероятности'])
         | 
| 44 | 
            +
                st.dataframe(df)
         | 
| 45 | 
            +
                st.write(f'Вероятность токсичного комментария {text2toxicity(input_text, True)}')
         |