Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
CursedPrograms committed Feb 12, 2024
1 parent d5db431 commit c6fe6c3
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 41 deletions.
107 changes: 95 additions & 12 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,113 @@
from flask import Flask, render_template, request
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')
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('index.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('index.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 the 10 newest images to the static/generated_images folder
copy_newest_images(output_directory, 'static/generated_images', num_images=10)

return render_template('index.html', result_image=output_filename)

if __name__ == "__main__":
webbrowser.open('http://127.0.0.1:5000/')
app.run(debug=True, use_reloader=False)
25 changes: 23 additions & 2 deletions static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@
--text-color: #fff;
}

a {
display: block;
width: 200px;
padding: 10px;
margin: 10px auto;
background-color: #3498db;
color: #fff;
text-decoration: none;
border-radius: 5px;
transition: background-color 0.3s;
}

a:hover {
background-color: #2980b9;
}

h1 {
margin-bottom: 20px;
}

body {
font-family: Arial, sans-serif;
background-color: var(--main-bg-color);
Expand All @@ -13,6 +33,8 @@ body {
}

.content {
text-align: center;
width: 80%;
max-width: 800px;
margin: 20px auto;
padding: 20px;
Expand Down Expand Up @@ -70,11 +92,10 @@ button:hover {
background-color: #0056b3;
}

/* Footer styles */
#dynamicFooter {
margin-top: 20px;
padding: 10px;
background-color: var(--accent-color);
color: var(--text-color);
color: white;
text-align: center;
}
35 changes: 35 additions & 0 deletions templates/image-generator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<title>SDXL-Turbo Model Card</title>
<meta charset="UTF-8">
<link href="" rel="icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>

<h1>SDXL-Turbo Model Card</h1>
<form action="/text_to_image" method="post">
<label for="prompt">Enter a text prompt:</label>
<input type="text" name="prompt" required>
<button type="submit">Text-to-image</button>
</form>

<form action="/image_to_image" method="post">
<label for="prompt">Enter a text prompt:</label>
<input type="text" name="prompt" required>
<button type="submit">Image-to-image</button>
</form>

<div id="imagecontainer">
<h1>Image Generation</h1>
{% if result_image %}
<h2>Result Image:</h2>
<img src="{{ url_for('static', filename='generated_images/' + result_image) }}" alt="Result Image">
{% endif %}
</div>
<footer id="dynamicFooter"></footer>
<script src="https://synthwomb.github.io/synth.womb/scripts/footer.js"></script>
</body>
</html>
38 changes: 11 additions & 27 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
<!-- templates/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="https://cursedprograms.github.io/cursedentertainment/cursedFavicon.png" rel="icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SynthiaGPT</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="" rel="icon" type="image/x-icon" />
<title>Home Page</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<div class="content">
<h1>SynthiaGPT</h1>
<div id="messages">
{% if prompt %}
<div class="user-message">
<strong>You:</strong>
<p>{{ prompt }}</p>
</div>
{% endif %}
{% if generated_text %}
<div class="synth-message">
<strong>SynthiaGPT:</strong>
<p>{{ generated_text }}</p>
</div>
{% endif %}
</div>
<form action="/generate" method="post">
<label for="prompt">Your Message:</label>
<input type="text" id="prompt" name="prompt" required>
<button type="submit">Send</button>
</form>
</div>
<h1>Welcome to the Home Page</h1>
<a href="{{ url_for('image_generator') }}">Go to Image Generator</a>
<a href="{{ url_for('synthia_gpt') }}">Go to Synthia GPT</a>
</div>
<footer id="dynamicFooter"></footer>
<script src="https://synthwomb.github.io/synth.womb/scripts/footer.js"></script>
</body>
</html>
</html>
36 changes: 36 additions & 0 deletions templates/synthia-gpt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="" rel="icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SynthiaGPT</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<div class="content">
<h1>SynthiaGPT</h1>
<div id="messages">
{% if prompt %}
<div class="user-message">
<strong>You:</strong>
<p>{{ prompt }}</p>
</div>
{% endif %}
{% if generated_text %}
<div class="synth-message">
<strong>SynthiaGPT:</strong>
<p>{{ generated_text }}</p>
</div>
{% endif %}
</div>
<form action="/generate" method="post">
<label for="prompt">Your Message:</label>
<input type="text" id="prompt" name="prompt" required>
<button type="submit">Send</button>
</form>
</div>
<footer id="dynamicFooter"></footer>
<script src="https://synthwomb.github.io/synth.womb/scripts/footer.js"></script>
</body>
</html>

0 comments on commit c6fe6c3

Please sign in to comment.