Spaces:
Runtime error
Runtime error
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, send_file
|
2 |
+
from PIL import Image
|
3 |
+
import io
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# Load the pre-trained GPT-2 model and tokenizer
|
8 |
+
from transformers import AutoTokenizer, AutoModel
|
9 |
+
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-2.7B")
|
11 |
+
model = AutoModel.from_pretrained("EleutherAI/gpt-neo-2.7B")
|
12 |
+
|
13 |
+
# Define a function to generate images from model output
|
14 |
+
from PIL import Image, ImageDraw, ImageFont
|
15 |
+
import numpy as np
|
16 |
+
|
17 |
+
def generate_image(text):
|
18 |
+
# Generate model output
|
19 |
+
input_ids = tokenizer.encode(text, return_tensors="pt")
|
20 |
+
output = model.generate(input_ids)
|
21 |
+
|
22 |
+
# Convert output to image
|
23 |
+
image_array = output[0].numpy().transpose(1, 2, 0)
|
24 |
+
image_array = ((image_array + 1) / 2.0) * 255.0
|
25 |
+
image_array = image_array.astype(np.uint8)
|
26 |
+
image = Image.fromarray(image_array)
|
27 |
+
|
28 |
+
# Add text to image
|
29 |
+
draw = ImageDraw.Draw(image)
|
30 |
+
font = ImageFont.truetype("arial.ttf", size=50)
|
31 |
+
draw.text((50, 50), text, font=font, fill=(0, 0, 0))
|
32 |
+
|
33 |
+
return image
|
34 |
+
|
35 |
+
# Define a route to handle the form submission
|
36 |
+
@app.route('/', methods=['GET', 'POST'])
|
37 |
+
def text_to_image():
|
38 |
+
if request.method == 'POST':
|
39 |
+
# Get text input from user
|
40 |
+
text = request.form['text']
|
41 |
+
|
42 |
+
# Generate image from text
|
43 |
+
image = generate_image(text)
|
44 |
+
|
45 |
+
# Return image to user
|
46 |
+
img_io = io.BytesIO()
|
47 |
+
image.save(img_io, 'JPEG')
|
48 |
+
img_io.seek(0)
|
49 |
+
return send_file(img_io, mimetype='image/jpeg')
|
50 |
+
else:
|
51 |
+
return render_template('index.html')
|
52 |
+
|
53 |
+
if __name__ == '__main__':
|
54 |
+
app.run()
|