|
from flask import Flask, render_template, request, send_file |
|
import google.generativeai as genai |
|
from google.generativeai import types |
|
import base64 |
|
import os |
|
import io |
|
import unicodedata |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
genai.configure(api_key=os.environ["GOOGLE_API_KEY"]) |
|
|
|
def emoji_to_text(emoji): |
|
try: |
|
return unicodedata.name(emoji) |
|
except ValueError: |
|
return "" |
|
|
|
def is_emoji(text): |
|
import re |
|
emoji_pattern = re.compile( |
|
"[" |
|
"\U0001F000-\U0001F9FF" |
|
"\U0001F300-\U0001F5FF" |
|
"\U00002702-\U000027B0" |
|
"\U000024C2-\U0001F251" |
|
"]+", flags=re.UNICODE) |
|
|
|
return bool(emoji_pattern.fullmatch(text)) |
|
|
|
def generate_creepy_emoji(emoji): |
|
description = emoji_to_text(emoji) |
|
response = genai.models.generate_content( |
|
model="gemini-2.0-flash-exp-image-generation", |
|
contents=(f""" |
|
Generate a hyper-realistic and highly detailed image of the {emoji} emoji. This image should depict a **realistic, textured**, and **wrinkled** version of the {description} emoji, transforming it into an unsettling and eerie representation. |
|
If it is not a face emoji, add a face to the image. If it is a cat face emoji, make sure you include the cat aspects in the emoji. |
|
Plan out what you are going to generate before, and make sure you understand what the emoji contains. |
|
Make sure you generate the image as well as the prompt. |
|
|
|
- **Facial Features**: The skin should appear aged, with deep wrinkles, pores, and imperfections, making it look as lifelike as possible. |
|
- **Texture & Detail**: The skin should have a hyper-detailed texture, including fine lines, blemishes, and realistic lighting effects to enhance the **creepy and unsettling** aesthetic. |
|
- **Color & Lighting**: Use **appropriate colors** (typically yellow for facial emojis), ensuring **shadows and highlights** enhance realism. The lighting should create an eerie atmosphere, possibly with unnatural or dim lighting to increase the unsettling effect. |
|
- **Mood & Style**: The image should evoke a sense of unease, making the emoji appear unnervingly lifelike, almost **uncanny valley** in effect. |
|
- **Follow Emoji**: Ensure that all elements of the emoji are present, such as tears or laughing tears of joy, according to its nature. |
|
"""), |
|
config=types.GenerateContentConfig( |
|
response_modalities=[ |
|
"image", |
|
"text", |
|
], |
|
), |
|
) |
|
|
|
if response and response.candidates: |
|
for candidate in response.candidates: |
|
if candidate.content and candidate.content.parts: |
|
for part in candidate.content.parts: |
|
if hasattr(part, "inline_data") and part.inline_data: |
|
return part.inline_data.data |
|
return None |
|
|
|
@app.route("/", methods=["GET", "POST"]) |
|
def index(): |
|
return render_template("index.html") |
|
|
|
@app.route("/generate", methods=["POST"]) |
|
def generate(): |
|
emoji = request.form["emoji"] |
|
if not is_emoji(emoji): |
|
return {"error": "Please enter only emoji characters."}, 400 |
|
|
|
image_data = generate_creepy_emoji(emoji) |
|
if image_data: |
|
encoded_image = base64.b64encode(image_data).decode('utf-8') |
|
return {"image": f"data:image/png;base64,{encoded_image}"} |
|
else: |
|
return {"error": "No image generated."}, 400 |
|
|
|
if __name__ == "__main__": |
|
app.run(host="0.0.0.0", port=7860) |
|
|