Spaces:
Runtime error
Runtime error
Commit
Β·
72c2afe
1
Parent(s):
936fabe
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Importar as bibliotecas necessΓ‘rias
|
2 |
+
import gradio as gr
|
3 |
+
import transformers
|
4 |
+
from PIL import Image
|
5 |
+
import requests
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
# Definir o modelo de geraΓ§Γ£o de texto
|
9 |
+
model = transformers.pipeline('text-generation', model='gpt2')
|
10 |
+
|
11 |
+
# Definir a funΓ§Γ£o de conversΓ£o de texto em imagem
|
12 |
+
def text_to_image(text):
|
13 |
+
# Gerar o texto a partir do texto de entrada
|
14 |
+
generated_text = model(text, max_length=50)[0]['generated_text']
|
15 |
+
# Remover os caracteres especiais do texto gerado
|
16 |
+
generated_text = generated_text.replace('\n', ' ').replace('\r', '')
|
17 |
+
# Criar uma URL para obter a imagem a partir do texto gerado
|
18 |
+
url = f"https://api.img4me.com/?text={generated_text}&font=arial&fcolor=000000&size=35&bcolor=FFFFFF&type=png"
|
19 |
+
# Obter a imagem a partir da URL
|
20 |
+
response = requests.get(url)
|
21 |
+
image_url = response.content.decode()
|
22 |
+
image = Image.open(BytesIO(requests.get(image_url).content))
|
23 |
+
# Retornar a imagem
|
24 |
+
return image
|
25 |
+
|
26 |
+
# Definir a interface do aplicativo
|
27 |
+
iface = gr.Interface(
|
28 |
+
fn=text_to_image,
|
29 |
+
inputs=gr.inputs.Textbox(lines=2, label="Texto de entrada"),
|
30 |
+
outputs=gr.outputs.Image(type="pil", label="Imagem de saΓda")
|
31 |
+
)
|
32 |
+
|
33 |
+
# LanοΏ½οΏ½ar o aplicativo
|
34 |
+
iface.launch()
|