fantaxy commited on
Commit
22f5ca4
ยท
verified ยท
1 Parent(s): e46db4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -2
app.py CHANGED
@@ -586,7 +586,58 @@ def process_all_titles(category, style, topic):
586
  time.sleep(5) # API ํ˜ธ์ถœ ์‚ฌ์ด์— 5์ดˆ ๋Œ€๊ธฐ
587
 
588
  return "\n".join(results)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
589
 
 
 
 
 
 
590
  with gr.Blocks() as demo:
591
  gr.Markdown(f"# {title}")
592
 
@@ -600,13 +651,17 @@ with gr.Blocks() as demo:
600
  gr.Markdown("### 3๋‹จ๊ณ„ : ๋ธ”๋กœ๊ทธ ์ฃผ์ œ, ๋˜๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์ƒ์„ธํžˆ ์ž…๋ ฅํ•˜์„ธ์š”")
601
  topic = gr.Textbox(label="๋ธ”๋กœ๊ทธ ์ฃผ์ œ", placeholder="์˜ˆ์‹œ: 8์›” ๊ตญ๋‚ด ์—ฌํ–‰์ง€ ์ถ”์ฒœ")
602
 
 
 
 
603
  start_btn = gr.Button("์‹œ์ž‘")
604
  result_output = gr.Textbox(label="์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ", lines=20)
 
605
 
606
  start_btn.click(
607
  fn=process_all_titles,
608
- inputs=[category, style, topic],
609
- outputs=[result_output]
610
  )
611
 
612
  category.change(fn=update_prompts_and_description, inputs=[category, style], outputs=[style_description])
 
586
  time.sleep(5) # API ํ˜ธ์ถœ ์‚ฌ์ด์— 5์ดˆ ๋Œ€๊ธฐ
587
 
588
  return "\n".join(results)
589
+
590
+
591
+ # ๋ฒˆ์—ญ ๋ชจ๋ธ ์„ค์ •
592
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
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
+ image = pipe(prompt).images[0]
605
+ image_path = f"generated_image_{uuid.uuid4()}.png"
606
+ image.save(image_path)
607
+ return image_path
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')
612
+
613
+ results = []
614
+ for title in titles[:num_titles]:
615
+ try:
616
+ _, _, _, _, _, blog_content, _ = fetch_references_and_generate_all_steps(category, style, topic, title)
617
+
618
+ if blog_content.startswith("API ํ˜ธ์ถœ ์‹คํŒจ"):
619
+ results.append(f"์ œ๋ชฉ: {title}\n์ƒ์„ฑ ์‹คํŒจ: {blog_content}\n\n")
620
+ continue
621
+
622
+ # ์ œ๋ชฉ ๋ฒˆ์—ญ ๋ฐ ์ด๋ฏธ์ง€ ์ƒ์„ฑ
623
+ translated_title = translate_title(title)
624
+ image_path = generate_image(translated_title)
625
+
626
+ # ์ด๋ฏธ์ง€ ๋งํฌ๋ฅผ ๋ธ”๋กœ๊ทธ ๋‚ด์šฉ ๋์— ์ถ”๊ฐ€
627
+ blog_content += f"\n\n[์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€]({image_path})"
628
+
629
+ # ํฌ์ŠคํŒ… ์ „์†ก
630
+ send_result = send_to_blogger(title, blog_content)
631
+
632
+ results.append(f"์ œ๋ชฉ: {title}\n์ „์†ก ๊ฒฐ๊ณผ: {send_result}\n์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€: {image_path}\n\n")
633
+ except Exception as e:
634
+ results.append(f"์ œ๋ชฉ: {title}\n์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}\n\n")
635
 
636
+ time.sleep(5) # API ํ˜ธ์ถœ ์‚ฌ์ด์— 5์ดˆ ๋Œ€๊ธฐ
637
+
638
+ return "\n".join(results)
639
+
640
+
641
  with gr.Blocks() as demo:
642
  gr.Markdown(f"# {title}")
643
 
 
651
  gr.Markdown("### 3๋‹จ๊ณ„ : ๋ธ”๋กœ๊ทธ ์ฃผ์ œ, ๋˜๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์ƒ์„ธํžˆ ์ž…๋ ฅํ•˜์„ธ์š”")
652
  topic = gr.Textbox(label="๋ธ”๋กœ๊ทธ ์ฃผ์ œ", placeholder="์˜ˆ์‹œ: 8์›” ๊ตญ๋‚ด ์—ฌํ–‰์ง€ ์ถ”์ฒœ")
653
 
654
+ gr.Markdown("### 4๋‹จ๊ณ„ : ์ƒ์„ฑํ•  ์ œ๋ชฉ ์ˆ˜๋ฅผ ์„ ํƒํ•˜์„ธ์š”")
655
+ num_titles = gr.Slider(minimum=1, maximum=100, value=2, step=1, label="์ƒ์„ฑํ•  ์ œ๋ชฉ ์ˆ˜")
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, generated_image]
665
  )
666
 
667
  category.change(fn=update_prompts_and_description, inputs=[category, style], outputs=[style_description])