Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,10 @@ import random
|
|
9 |
import os
|
10 |
from huggingface_hub import InferenceClient
|
11 |
from fpdf import FPDF
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def setup_session():
|
14 |
try:
|
@@ -506,6 +510,63 @@ def update_prompts_and_description(category, style):
|
|
506 |
style_description = get_style_description(style)
|
507 |
return style_description
|
508 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
509 |
with gr.Blocks() as demo:
|
510 |
gr.Markdown(f"# {title}")
|
511 |
|
@@ -566,4 +627,22 @@ with gr.Blocks() as demo:
|
|
566 |
category.change(fn=update_prompts_and_description, inputs=[category, style], outputs=[style_description])
|
567 |
style.change(fn=update_prompts_and_description, inputs=[category, style], outputs=[style_description])
|
568 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
569 |
demo.launch()
|
|
|
9 |
import os
|
10 |
from huggingface_hub import InferenceClient
|
11 |
from fpdf import FPDF
|
12 |
+
from transformers import pipeline
|
13 |
+
import torch
|
14 |
+
from diffusers import StableDiffusionXLPipeline
|
15 |
+
import uuid
|
16 |
|
17 |
def setup_session():
|
18 |
try:
|
|
|
510 |
style_description = get_style_description(style)
|
511 |
return style_description
|
512 |
|
513 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
514 |
+
|
515 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
516 |
+
|
517 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
518 |
+
"SG161222/RealVisXL_V4.0",
|
519 |
+
torch_dtype=torch.float16,
|
520 |
+
use_safetensors=True,
|
521 |
+
add_watermarker=False,
|
522 |
+
variant="fp16"
|
523 |
+
).to(device)
|
524 |
+
|
525 |
+
def generate_image(prompt: str):
|
526 |
+
# ํ๊ธ ์
๋ ฅ ๊ฐ์ง ๋ฐ ๋ฒ์ญ
|
527 |
+
if any('\uac00' <= char <= '\ud7a3' for char in prompt):
|
528 |
+
translated = translator(prompt, max_length=512)
|
529 |
+
prompt = translated[0]['translation_text']
|
530 |
+
|
531 |
+
# Hi-res์ 3840x2160 ์คํ์ผ ์ ์ฉ
|
532 |
+
prompt = f"hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic"
|
533 |
+
|
534 |
+
# ๊ณ ์ ๋ ์ค์ ๊ฐ
|
535 |
+
negative_prompt = "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly, (deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, disgusting, amputation"
|
536 |
+
width = 1024
|
537 |
+
height = 1024
|
538 |
+
guidance_scale = 6
|
539 |
+
num_inference_steps = 100
|
540 |
+
seed = random.randint(0, 2**32 - 1)
|
541 |
+
generator = torch.Generator().manual_seed(seed)
|
542 |
+
|
543 |
+
image = pipe(
|
544 |
+
prompt=prompt,
|
545 |
+
negative_prompt=negative_prompt,
|
546 |
+
width=width,
|
547 |
+
height=height,
|
548 |
+
guidance_scale=guidance_scale,
|
549 |
+
num_inference_steps=num_inference_steps,
|
550 |
+
generator=generator,
|
551 |
+
).images[0]
|
552 |
+
|
553 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
554 |
+
image.save(unique_name)
|
555 |
+
return unique_name
|
556 |
+
|
557 |
+
def generate_images(blog_post):
|
558 |
+
images = []
|
559 |
+
lines = blog_post.split('\n')
|
560 |
+
for i in range(5):
|
561 |
+
if i < len(lines):
|
562 |
+
prompt = lines[i]
|
563 |
+
else:
|
564 |
+
prompt = random.choice(lines)
|
565 |
+
image_path = generate_image(prompt)
|
566 |
+
images.append(image_path)
|
567 |
+
return images
|
568 |
+
|
569 |
+
|
570 |
with gr.Blocks() as demo:
|
571 |
gr.Markdown(f"# {title}")
|
572 |
|
|
|
627 |
category.change(fn=update_prompts_and_description, inputs=[category, style], outputs=[style_description])
|
628 |
style.change(fn=update_prompts_and_description, inputs=[category, style], outputs=[style_description])
|
629 |
|
630 |
+
# ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ๋ฐ ์ถ๋ ฅ ์ถ๊ฐ
|
631 |
+
generate_images_btn = gr.Button("์ด๋ฏธ์ง ์์ฑํ๊ธฐ")
|
632 |
+
image_outputs = [gr.Image(label=f"์์ฑ๋ ์ด๋ฏธ์ง {i+1}") for i in range(5)]
|
633 |
+
|
634 |
+
generate_images_btn.click(
|
635 |
+
fn=generate_images,
|
636 |
+
inputs=[output],
|
637 |
+
outputs=image_outputs
|
638 |
+
)
|
639 |
+
|
640 |
+
save_pdf_btn = gr.Button("PDF๋ก ์ ์ฅํ๊ธฐ")
|
641 |
+
pdf_output = gr.File(label="์์ฑ๋ PDF ํ์ผ")
|
642 |
+
|
643 |
+
save_pdf_btn.click(fn=save_content_to_pdf, inputs=[output], outputs=[pdf_output])
|
644 |
+
|
645 |
+
category.change(fn=update_prompts_and_description, inputs=[category, style], outputs=[style_description])
|
646 |
+
style.change(fn=update_prompts_and_description, inputs=[category, style], outputs=[style_description])
|
647 |
+
|
648 |
demo.launch()
|