Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -1,19 +1,32 @@ | |
| 1 | 
             
            import gradio as gr
         | 
| 2 | 
            -
            from transformers import AutoModelForCausalLM, AutoTokenizer
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 3 |  | 
| 4 | 
             
            # Загрузка модели и токенизатора
         | 
| 5 | 
             
            model_name = "IlyaGusev/saiga_yandexgpt_8b"
         | 
| 6 | 
             
            tokenizer = AutoTokenizer.from_pretrained(model_name)
         | 
| 7 | 
            -
            model = AutoModelForCausalLM.from_pretrained( | 
|  | |
|  | |
|  | |
|  | |
| 8 |  | 
| 9 | 
             
            # Функция для генерации текста
         | 
| 10 | 
             
            def generate_text(input_text):
         | 
| 11 | 
            -
                inputs = tokenizer(input_text, return_tensors="pt")
         | 
| 12 | 
             
                outputs = model.generate(
         | 
| 13 | 
             
                    **inputs,
         | 
| 14 | 
            -
                    max_new_tokens=300,   | 
| 15 | 
            -
                    do_sample=True,       | 
| 16 | 
            -
                    temperature=0.7 | 
| 17 | 
             
                )
         | 
| 18 | 
             
                return tokenizer.decode(outputs[0], skip_special_tokens=True)
         | 
| 19 |  | 
| @@ -22,8 +35,8 @@ interface = gr.Interface( | |
| 22 | 
             
                fn=generate_text,
         | 
| 23 | 
             
                inputs=gr.Textbox(lines=2, placeholder="Введите ваш запрос..."),
         | 
| 24 | 
             
                outputs="text",
         | 
| 25 | 
            -
                title="Saiga YandexGPT 8B Demo",
         | 
| 26 | 
            -
                description="Задайте вопрос модели Saiga YandexGPT 8B | 
| 27 | 
             
            )
         | 
| 28 |  | 
| 29 | 
             
            # Запуск приложения
         | 
|  | |
| 1 | 
             
            import gradio as gr
         | 
| 2 | 
            +
            from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
         | 
| 3 | 
            +
            import torch
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # Настройка 4-битной квантизации
         | 
| 6 | 
            +
            quantization_config = BitsAndBytesConfig(
         | 
| 7 | 
            +
                load_in_4bit=True,  
         | 
| 8 | 
            +
                bnb_4bit_compute_dtype=torch.float16,  #
         | 
| 9 | 
            +
                bnb_4bit_quant_type="nf4",  
         | 
| 10 | 
            +
                bnb_4bit_use_double_quant=True 
         | 
| 11 | 
            +
            )
         | 
| 12 |  | 
| 13 | 
             
            # Загрузка модели и токенизатора
         | 
| 14 | 
             
            model_name = "IlyaGusev/saiga_yandexgpt_8b"
         | 
| 15 | 
             
            tokenizer = AutoTokenizer.from_pretrained(model_name)
         | 
| 16 | 
            +
            model = AutoModelForCausalLM.from_pretrained(
         | 
| 17 | 
            +
                model_name,
         | 
| 18 | 
            +
                quantization_config=quantization_config,
         | 
| 19 | 
            +
                device_map="auto" 
         | 
| 20 | 
            +
            )
         | 
| 21 |  | 
| 22 | 
             
            # Функция для генерации текста
         | 
| 23 | 
             
            def generate_text(input_text):
         | 
| 24 | 
            +
                inputs = tokenizer(input_text, return_tensors="pt")  # Перенос на GPU
         | 
| 25 | 
             
                outputs = model.generate(
         | 
| 26 | 
             
                    **inputs,
         | 
| 27 | 
            +
                    max_new_tokens=300,  
         | 
| 28 | 
            +
                    do_sample=True,      
         | 
| 29 | 
            +
                    temperature=0.7     
         | 
| 30 | 
             
                )
         | 
| 31 | 
             
                return tokenizer.decode(outputs[0], skip_special_tokens=True)
         | 
| 32 |  | 
|  | |
| 35 | 
             
                fn=generate_text,
         | 
| 36 | 
             
                inputs=gr.Textbox(lines=2, placeholder="Введите ваш запрос..."),
         | 
| 37 | 
             
                outputs="text",
         | 
| 38 | 
            +
                title="Saiga YandexGPT 8B Demo (4-bit)",
         | 
| 39 | 
            +
                description="Задайте вопрос модели Saiga YandexGPT 8B в 4-битной квантизации!"
         | 
| 40 | 
             
            )
         | 
| 41 |  | 
| 42 | 
             
            # Запуск приложения
         | 
