csaguiar commited on
Commit
54e4be1
1 Parent(s): ebf1de3

first app with image generation in portuguese

Browse files
Files changed (1) hide show
  1. app.py +75 -2
app.py CHANGED
@@ -1,4 +1,77 @@
1
  import streamlit as st
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from diffusers import StableDiffusionPipeline
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
 
5
+ DIFFUSION_MODEL_ID = "runwayml/stable-diffusion-v1-5"
6
+ TRANSLATION_MODEL_ID = "Narrativa/mbart-large-50-finetuned-opus-pt-en-translation" # noqa
7
+
8
+
9
+ def load_translation_models(translation_model_id):
10
+ tokenizer = AutoTokenizer.from_pretrained(
11
+ translation_model_id,
12
+ use_auth_token=True
13
+ )
14
+ text_model = AutoModelForSeq2SeqLM.from_pretrained(
15
+ translation_model_id,
16
+ use_auth_token=True
17
+ )
18
+
19
+ return tokenizer, text_model
20
+
21
+
22
+ def pipeline_generate(diffusion_model_id):
23
+ pipe = StableDiffusionPipeline.from_pretrained(
24
+ diffusion_model_id,
25
+ use_auth_token=True
26
+ )
27
+ pipe = pipe.to("mps")
28
+
29
+ # Recommended if your computer has < 64 GB of RAM
30
+ pipe.enable_attention_slicing()
31
+
32
+ return pipe
33
+
34
+
35
+ def translate(prompt, tokenizer, text_model):
36
+ pt_tokens = tokenizer([prompt], return_tensors="pt")
37
+ en_tokens = text_model.generate(
38
+ **pt_tokens, max_new_tokens=100,
39
+ num_beams=8, early_stopping=True
40
+ )
41
+ en_prompt = tokenizer.batch_decode(en_tokens, skip_special_tokens=True)
42
+ print(f"translation: [PT] {prompt} -> [EN] {en_prompt[0]}")
43
+
44
+ return en_prompt[0]
45
+
46
+
47
+ def generate_image(pipe, prompt):
48
+ # First-time "warmup" pass (see explanation above)
49
+ _ = pipe(prompt, num_inference_steps=1)
50
+
51
+ return pipe(prompt).images[0]
52
+
53
+
54
+ def process_prompt(prompt):
55
+ tokenizer, text_model = load_translation_models(TRANSLATION_MODEL_ID)
56
+ prompt = translate(prompt, tokenizer, text_model)
57
+ pipe = pipeline_generate(DIFFUSION_MODEL_ID)
58
+ image = generate_image(pipe, prompt)
59
+ return image
60
+
61
+
62
+ st.write("# Crie imagens com Stable Diffusion")
63
+ prompt_input = st.text_input("Escreva uma descrição da imagem")
64
+
65
+ placeholder = st.empty()
66
+ btn = placeholder.button('Processar imagem', disabled=False, key=1)
67
+ reload = st.button('Reiniciar', disabled=False)
68
+
69
+ if btn:
70
+ placeholder.button('Processar imagem', disabled=True, key=2)
71
+ image = process_prompt(prompt_input)
72
+ st.image(image)
73
+ placeholder.button('Processar imagem', disabled=False, key=3)
74
+ placeholder.empty()
75
+
76
+ if reload:
77
+ st.experimental_rerun()