Update app.py
Browse files
app.py
CHANGED
@@ -15,6 +15,7 @@ from diffusers import StableDiffusionXLPipeline
|
|
15 |
import uuid
|
16 |
import json
|
17 |
from huggingface_hub.utils._errors import HfHubHTTPError
|
|
|
18 |
|
19 |
def setup_session():
|
20 |
try:
|
@@ -588,24 +589,24 @@ def process_all_titles(category, style, topic):
|
|
588 |
return "\n".join(results)
|
589 |
|
590 |
|
591 |
-
#
|
592 |
-
|
593 |
-
|
594 |
-
# ์ด๋ฏธ์ง ์์ฑ ๋ชจ๋ธ ์ค์
|
595 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
596 |
-
pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-3-medium", torch_dtype=torch.float16)
|
597 |
-
pipe = pipe.to(device)
|
598 |
-
|
599 |
-
def translate_title(title):
|
600 |
-
translated = translator(title, max_length=128)[0]['translation_text']
|
601 |
-
return translated
|
602 |
|
603 |
def generate_image(prompt):
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
608 |
|
|
|
609 |
def process_all_titles(category, style, topic, num_titles):
|
610 |
title_suggestions, _ = suggest_title(category, style, topic, "", "", "")
|
611 |
titles = title_suggestions.split('\n')
|
@@ -619,17 +620,17 @@ def process_all_titles(category, style, topic, num_titles):
|
|
619 |
results.append(f"์ ๋ชฉ: {title}\n์์ฑ ์คํจ: {blog_content}\n\n")
|
620 |
continue
|
621 |
|
622 |
-
#
|
623 |
-
|
624 |
-
image_path = generate_image(translated_title)
|
625 |
|
626 |
-
|
627 |
-
|
|
|
628 |
|
629 |
# ํฌ์คํ
์ ์ก
|
630 |
send_result = send_to_blogger(title, blog_content)
|
631 |
|
632 |
-
results.append(f"์ ๋ชฉ: {title}\n์ ์ก ๊ฒฐ๊ณผ: {send_result}\n์์ฑ๋
|
633 |
except Exception as e:
|
634 |
results.append(f"์ ๋ชฉ: {title}\n์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}\n\n")
|
635 |
|
@@ -637,7 +638,6 @@ def process_all_titles(category, style, topic, num_titles):
|
|
637 |
|
638 |
return "\n".join(results)
|
639 |
|
640 |
-
|
641 |
with gr.Blocks() as demo:
|
642 |
gr.Markdown(f"# {title}")
|
643 |
|
@@ -656,12 +656,11 @@ with gr.Blocks() as demo:
|
|
656 |
|
657 |
start_btn = gr.Button("์์")
|
658 |
result_output = gr.Textbox(label="์ฒ๋ฆฌ ๊ฒฐ๊ณผ", lines=20)
|
659 |
-
generated_image = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง")
|
660 |
|
661 |
start_btn.click(
|
662 |
fn=process_all_titles,
|
663 |
inputs=[category, style, topic, num_titles],
|
664 |
-
outputs=[result_output
|
665 |
)
|
666 |
|
667 |
category.change(fn=update_prompts_and_description, inputs=[category, style], outputs=[style_description])
|
|
|
15 |
import uuid
|
16 |
import json
|
17 |
from huggingface_hub.utils._errors import HfHubHTTPError
|
18 |
+
from gradio_client import Client
|
19 |
|
20 |
def setup_session():
|
21 |
try:
|
|
|
589 |
return "\n".join(results)
|
590 |
|
591 |
|
592 |
+
# ์ด๋ฏธ์ง ์์ฑ API ํด๋ผ์ด์ธํธ ์ค์
|
593 |
+
image_client = Client("http://hugpu.ai:7899/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
594 |
|
595 |
def generate_image(prompt):
|
596 |
+
try:
|
597 |
+
result = image_client.predict(
|
598 |
+
prompt,
|
599 |
+
api_name="/generate"
|
600 |
+
)
|
601 |
+
# API ์๋ต์์ ์ด๋ฏธ์ง URL์ ์ถ์ถํฉ๋๋ค.
|
602 |
+
# ์ค์ ์๋ต ๊ตฌ์กฐ์ ๋ฐ๋ผ ์ด ๋ถ๋ถ์ ์กฐ์ ํด์ผ ํ ์ ์์ต๋๋ค.
|
603 |
+
image_url = result[0] if isinstance(result, list) else result
|
604 |
+
return image_url
|
605 |
+
except Exception as e:
|
606 |
+
print(f"์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
607 |
+
return None
|
608 |
|
609 |
+
# process_all_titles ํจ์ ์์
|
610 |
def process_all_titles(category, style, topic, num_titles):
|
611 |
title_suggestions, _ = suggest_title(category, style, topic, "", "", "")
|
612 |
titles = title_suggestions.split('\n')
|
|
|
620 |
results.append(f"์ ๋ชฉ: {title}\n์์ฑ ์คํจ: {blog_content}\n\n")
|
621 |
continue
|
622 |
|
623 |
+
# ์ด๋ฏธ์ง ์์ฑ
|
624 |
+
image_url = generate_image(title)
|
|
|
625 |
|
626 |
+
if image_url:
|
627 |
+
# ์ด๋ฏธ์ง URL์ ๋ธ๋ก๊ทธ ๋ด์ฉ ๋์ ์ถ๊ฐ
|
628 |
+
blog_content += f"\n\n[์์ฑ๋ ์ด๋ฏธ์ง]({image_url})"
|
629 |
|
630 |
# ํฌ์คํ
์ ์ก
|
631 |
send_result = send_to_blogger(title, blog_content)
|
632 |
|
633 |
+
results.append(f"์ ๋ชฉ: {title}\n์ ์ก ๊ฒฐ๊ณผ: {send_result}\n์์ฑ๋ ์ด๋ฏธ์ง URL: {image_url}\n\n")
|
634 |
except Exception as e:
|
635 |
results.append(f"์ ๋ชฉ: {title}\n์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}\n\n")
|
636 |
|
|
|
638 |
|
639 |
return "\n".join(results)
|
640 |
|
|
|
641 |
with gr.Blocks() as demo:
|
642 |
gr.Markdown(f"# {title}")
|
643 |
|
|
|
656 |
|
657 |
start_btn = gr.Button("์์")
|
658 |
result_output = gr.Textbox(label="์ฒ๋ฆฌ ๊ฒฐ๊ณผ", lines=20)
|
|
|
659 |
|
660 |
start_btn.click(
|
661 |
fn=process_all_titles,
|
662 |
inputs=[category, style, topic, num_titles],
|
663 |
+
outputs=[result_output]
|
664 |
)
|
665 |
|
666 |
category.change(fn=update_prompts_and_description, inputs=[category, style], outputs=[style_description])
|