-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5db431
commit c6fe6c3
Showing
5 changed files
with
200 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |