File size: 2,830 Bytes
959f9aa
0e90e8a
959f9aa
 
 
 
 
 
 
 
 
 
 
 
cfd0198
 
 
959f9aa
0e90e8a
cfd0198
0e90e8a
 
 
 
 
 
 
 
959f9aa
2b05902
959f9aa
 
374c9d2
618f4aa
 
 
 
 
 
 
959f9aa
 
 
115b885
 
 
 
 
aae549c
ad6d3e6
115b885
 
959f9aa
 
de7efb2
 
959f9aa
 
 
de7efb2
959f9aa
707a8d6
959f9aa
 
cfd0198
959f9aa
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import gradio as gr
import spaces
from peft import AutoPeftModelForCausalLM
from transformers import AutoTokenizer

# Define the prompt template
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
{}
### Input:
{}
### Response:
{}"""

# Fixed instruction text
instruction_text = "You are a blogger named Artemiy Lebedev, your purpose is to generate a post in Russian based on the post article"

# Function to generate responses
@spaces.GPU
def generate_response(input_text):
    # Load the model and tokenizer within the GPU context
    model = AutoPeftModelForCausalLM.from_pretrained(
        "shakaryan/lebedev_qwen2.5",
        load_in_4bit=True,  # Adjust based on your setup
    ).to("cuda")
    tokenizer = AutoTokenizer.from_pretrained("shakaryan/lebedev_qwen2.5")
    EOS_TOKEN = tokenizer.eos_token  # Ensure proper sequence termination

    # Format the prompt
    formatted_prompt = alpaca_prompt.format(instruction_text, input_text, "")
    # Tokenize and generate response
    inputs = tokenizer(formatted_prompt, return_tensors="pt").to("cuda")
    outputs = model.generate(**inputs, max_new_tokens=256, use_cache=True)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)

    # Extract part after ### Response:
    response_start = response.find("### Response:") + len("### Response:\n")
    response_clean = response[response_start:].replace("<|im_end|>", "").strip()
    
    return response_clean

# Define the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown(
        """
        ### Генератор постов в стиле Артемия Лебедева
        Этот генератор создает посты в стиле Артемия Лебедева. 
        Попробуйте написать заголовок поста, и генератор создаст текст. 
        Подробнее о стиле: [Артемий Лебедев в Telegram](https://t.me/temalebedev) \n
        Телеграм канал автора: [Гегам Шакарян - ИИ в лаваше](https://t.me/ai_in_lavash)
        """
    )
    with gr.Row():
        input_text = gr.Textbox(
            label="Заголовок поста",
            placeholder="Введите заголовок поста здесь...",
            lines=5,
        )
    with gr.Row():
        output = gr.Textbox(label="Сгенерированный пост", lines=10)
    with gr.Row():
        generate_button = gr.Button("Сгенерировать")
        generate_button.click(
            fn=generate_response, 
            inputs=[input_text], 
            outputs=output
        )

# Launch the app
demo.launch()