File size: 1,574 Bytes
c5b2dfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56a4c1e
c5b2dfc
56a4c1e
 
 
 
 
 
c5b2dfc
 
 
 
 
 
 
 
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
import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer


class StreamlitStreamer(TextStreamer):
    def on_finalized_text(self, text: str, stream_end: bool = False):
        st.session_state['new_mail'] += text
        new_mail.write(st.session_state['new_mail'])

device = 'cuda' if torch.cuda.is_available() else 'cpu'

@st.cache_resource
def load_model():
    return AutoModelForCausalLM.from_pretrained(
        "tomaszki/mail_fixer",
    ).to(device)

@st.cache_resource
def load_tokenizer():
    return AutoTokenizer.from_pretrained("facebook/opt-125m")

model = load_model()
tokenizer = load_tokenizer()

st.title('Mail fixer')
placeholder = '''Hi Emma,

How did you like our art project? I thought it was pretty cool too! But one thing I noticed was that sometimes the letters were hard to see because they were a little bit too small. Let's try making them bigger or changing their shape so everyone can read them better.

Thanks for helping us work on the project! I have lots of other ideas for things we can do together. Can't wait to get started!

Love, James'''
mail = st.text_area('Enter your mail here', placeholder=placeholder, height=300)
new_mail = st.text('')

if mail:
    st.session_state['new_mail'] = ''
    streamer = StreamlitStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
    prompt = f'Original email:\n{mail}\nFixed email:\n'
    tokenized = tokenizer(prompt, return_tensors='pt').to(device)
    output = model.generate(**tokenized, max_new_tokens=1024, streamer=streamer)