File size: 1,838 Bytes
a979393
 
7b74d53
a979393
 
5ec1b00
9a67f50
 
a979393
 
 
 
9a67f50
a979393
b746c52
7b74d53
9a67f50
b746c52
7b74d53
a979393
 
 
 
 
 
 
 
 
 
 
 
79a28b0
a979393
 
 
 
 
 
 
7b74d53
a979393
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
from textPreprocessing import text2prompt
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
import torch

model_id = 'ai-forever/ruT5-large'

"""bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="fp4",
    bnb_4bit_compute_dtype=torch.bfloat16
)"""

model = AutoModelForCausalLM.from_pretrained(
    model_id,
    # quantization_config=bnb_config # Необходимо раскомментить при досутпе к GPU
)
tokenizer = AutoTokenizer.from_pretrained(model_id)

def predict(input_text, t, m):
    """
        Вывести финансовую рекомендацию на основе:
        input_text: str 
            - Контекст в виде новости из области экономики
        t: tokenizer
            - Токенизатор для модели
        m: model
            - Instruct-based модель
    """
    prompt = text2prompt(input_text)
    inputs = tokenizer(prompt, return_tensors="pt")
    generate_ids = model.generate(inputs.input_ids, max_new_tokens=128)
    answer = tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
    return answer.replace(prompt, "")

gradio_app = gr.Interface(
    predict,
    inputs=gr.Textbox(
        label="Входная новость", container=True,
        lines=8, placeholder="Акции кредитного банка \"X\" обрушились в цене из-за дефолта по ипотечным кредитам"
    ),
    outputs=[gr.Label(label="Финансовая рекомендация на основе новости:")],
    title="Finam Finetuned Mistral Instruct (FFMI)",
)

if __name__ == "__main__":
    gradio_app.launch()