-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
124 lines (100 loc) · 4.34 KB
/
app.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from flask import Flask, render_template, request
import os
import shutil
import transformers
from transformers import TFAutoModelForCausalLM, AutoTokenizer
import tensorflow as tf
import logging
from scripts.gpt.system.generate_text import generate_text
import webbrowser
transformers.logging.set_verbosity_error()
tf.get_logger().setLevel(logging.ERROR)
app = Flask(__name__, static_url_path='/static')
model_name = "gpt2"
model = TFAutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name, pad_token_id=50256)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/image-generator')
def image_generator():
return render_template('image-generator.html')
@app.route('/synthia-gpt')
def synthia_gpt():
return render_template('synthia-gpt.html')
@app.route('/generate', methods=['POST'])
def generate():
prompt = request.form['prompt']
generated_text = generate_text(prompt, model, tokenizer)
return render_template('synthia-gpt.html', prompt=prompt, generated_text=generated_text)
def copy_newest_images(output_directory, target_directory, num_images=10):
# Get the list of generated images sorted by modification time
generated_images = sorted(os.listdir(output_directory), key=lambda f: os.path.getmtime(os.path.join(output_directory, f)))
# Ensure the target directory exists
os.makedirs(target_directory, exist_ok=True)
# Copy the 10 newest images to the target directory
for image in generated_images[-num_images:]:
source_path = os.path.join(output_directory, image)
target_path = os.path.join(target_directory, image)
shutil.copyfile(source_path, target_path)
@app.route('/text_to_image', methods=['POST'])
def text_to_image():
prompt = request.form.get('prompt')
output_directory = 'output/generated_images'
os.makedirs(output_directory, exist_ok=True)
counter = 1
output_filename = f'output_{counter}.jpg'
while os.path.exists(os.path.join(output_directory, output_filename)):
counter += 1
output_filename = f'output_{counter}.jpg'
text_to_image_code = f"""
from diffusers import AutoPipelineForText2Image
import torch
from PIL import Image
pipe = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float32, variant="fp16")
pipe.to("cpu")
prompt = "{prompt}"
image = pipe(prompt=prompt, num_inference_steps=1, guidance_scale=0.0).images[0]
output_directory = 'output/generated_images'
os.makedirs(output_directory, exist_ok=True)
image.save(os.path.join(output_directory, '{output_filename}'))
"""
print("Generated code snippet:")
print(text_to_image_code)
exec(text_to_image_code)
# Copy the 10 newest images to the static/generated_images folder
copy_newest_images(output_directory, 'static/generated_images', num_images=10)
return render_template('image-generator.html', result_image=output_filename)
@app.route('/image_to_image', methods=['POST'])
def image_to_image():
prompt = request.form.get('prompt')
output_directory = 'output/generated_images'
os.makedirs(output_directory, exist_ok=True)
counter = 1
output_filename = f'output_{counter}.jpg'
while os.path.exists(os.path.join(output_directory, output_filename)):
counter += 1
output_filename = f'output_{counter}.jpg'
image_to_image_code = f"""
from diffusers import AutoPipelineForImage2Image
from diffusers.utils import load_image
import torch
from PIL import Image
pipe = AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float32, variant="fp16")
pipe.to("cpu")
image_path = "input/input.jpg"
init_image = load_image(image_path).resize((512, 512))
prompt = "{prompt}"
image = pipe(prompt, image=init_image, num_inference_steps=2, strength=0.5, guidance_scale=0.0).images[0]
output_directory = 'output/generated_images'
os.makedirs(output_directory, exist_ok=True)
image.save(os.path.join(output_directory, '{output_filename}'))
"""
print("Generated code snippet:")
print(image_to_image_code)
exec(image_to_image_code)
copy_newest_images(output_directory, 'static/generated_images', num_images=10)
return render_template('image-generator.html', result_image=output_filename)
if __name__ == "__main__":
webbrowser.open('http://127.0.0.1:5000/')
app.run(debug=True, use_reloader=False)