File size: 1,686 Bytes
aa49290
 
 
 
 
 
 
 
ec83c3c
1e8f625
 
aa49290
 
 
 
7c19f2c
ec83c3c
1e8f625
aa49290
 
1e8f625
 
 
 
 
 
 
 
 
aa49290
 
 
 
 
1e8f625
 
 
 
 
 
 
 
 
 
 
 
aa49290
 
1e8f625
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
import torch
from transformers import GPT2Tokenizer, GPT2LMHeadModel
import streamlit as st

# Load the tokenizer and model
tokenizer = GPT2Tokenizer.from_pretrained('webtoon_tokenizer')
model = GPT2LMHeadModel.from_pretrained('webtoon_model')

# Check if GPU is available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# Define the function that generates the description
def generate_description(title):
    # Preprocess the input
    input_text = f"{title}"
    input_ids = tokenizer.encode(input_text, return_tensors='pt').to(device)
    attention_mask = (input_ids != tokenizer.pad_token_id).long().to(device)

    # Generate the output using the model
    with torch.no_grad():  # Disable gradient calculation for inference
        output = model.generate(
            input_ids=input_ids,
            attention_mask=attention_mask,  # Pass attention_mask to avoid warnings
            max_length=100,  # Reduce max_length for quicker inference
            num_beams=2,     # Reduce num_beams for quicker inference
            early_stopping=True,
            no_repeat_ngram_size=2
        )

    # Convert the output to text
    description = tokenizer.decode(output[0], skip_special_tokens=True)
    return description

# Define the app
def main():
    st.title('Webtoon Description Generator')

    # Get the input from the user
    title = st.text_input('Enter the title of the Webtoon:', '')

    # Generate the description
    if st.button('Generate Description'):
        with st.spinner('Generating...'):
            description = generate_description(title)
        st.success(description)

if __name__ == '__main__':
    main()