File size: 1,689 Bytes
bd61da5
233c774
 
 
 
ff3389c
233c774
 
 
 
 
 
 
ff3389c
076a158
bd61da5
 
 
233c774
bd61da5
6b329ae
 
 
 
233c774
 
ff3389c
 
 
 
 
b7137a2
233c774
b7137a2
bcfdb10
ff3389c
076a158
bcfdb10
 
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
import streamlit as st
from transformers import AutoTokenizer, TFAutoModelForCausalLM

# MODEL TO CALL

model_name = "Alirani/distilgpt2-finetuned-synopsis-genres_final"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = TFAutoModelForCausalLM.from_pretrained(model_name)

def generate_synopsis(model, tokenizer, title):
    input_ids = tokenizer(title, return_tensors="tf")
    output = model.generate(input_ids['input_ids'], max_length=150, num_beams=5, no_repeat_ngram_size=2, top_k=50, attention_mask=input_ids['attention_mask'])
    synopsis = tokenizer.decode(output[0], skip_special_tokens=True)
    processed_synopsis = "".join(synopsis.split('|')[2].rpartition('.')[:2]).strip()
    return processed_synopsis

favicon = "https://i.ibb.co/JRdhFZg/favicon-32x32.png"

st.set_page_config(page_title="LoreFinder-demo", page_icon = favicon, layout = 'wide', initial_sidebar_state = 'auto')

st.title('Demo LoreFinder')

st.header('Generate a story')

prod_title = st.text_input('Type a title to generate a synopsis')

option_genres = st.selectbox(
    'Select a genre to tailor your synopsis',
    ('Family', 'Romance', 'Comedy', 'Action', 'Documentary', 'Adventure', 'Drama', 'Mystery', 'Crime', 'Thriller', 'Science Fiction', 'History', 'Music', 'Western', 'Fantasy', 'TV Movie', 'Horror', 'Animation', 'Reality')
    )

button_synopsis = st.button('Get synopsis')

if button_synopsis:
    if len(prod_title.split(' ')) > 0:
        gen_synopsis = generate_synopsis(model, tokenizer, f"{prod_title} | {option_genres} | ")
        st.text_area('Generated synopsis', value=gen_synopsis, disabled=True)
    else:
        st.write('Write a title for the generator to work !')