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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -24
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
- 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')
@@ -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
- 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
 
@@ -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, generated_image]
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])