JUNGU commited on
Commit
c6443c1
·
1 Parent(s): a58e796

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -4
app.py CHANGED
@@ -36,6 +36,13 @@ def save_as_pdf():
36
  pdf.output(pdf_path)
37
  return pdf_path
38
 
 
 
 
 
 
 
 
39
  OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
40
  clipi_client = Client("https://fffiloni-clip-interrogator-2.hf.space/")
41
 
@@ -74,13 +81,12 @@ def infer(image_input, audience, keyword, protagonist):
74
  current_story = clipi_result
75
  return next_chapter(audience, keyword, protagonist)
76
 
77
- # CSS for layout
78
  css = """
79
  #col-container {max-width: 910px; margin-left: auto; margin-right: auto;}
80
  a {text-decoration-line: underline; font-weight: 600;}
81
  """
82
 
83
- # Gradio UI
84
  with gr.Blocks(css=css) as demo:
85
  with gr.Column(elem_id="col-container"):
86
  gr.Markdown(
@@ -101,9 +107,22 @@ with gr.Blocks(css=css) as demo:
101
  with gr.Column():
102
  chapter_story = gr.Markdown(label="이야기", elem_id="chapter_story")
103
  chapter_image = gr.Image(label="그림", elem_id="chapter_image")
 
104
 
105
- submit_btn.click(fn=infer, inputs=[image_in, audience, keyword_in, protagonist_in], outputs=[chapter_story, chapter_image])
106
- next_chapter_btn.click(fn=lambda: next_chapter(audience=audience.value, keyword=keyword_in.value, protagonist=protagonist_in.value), outputs=[chapter_story, chapter_image])
107
  save_pdf_btn.click(fn=save_as_pdf, outputs=gr.File(label="PDF 다운로드"))
108
 
109
  demo.queue(max_size=12).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  pdf.output(pdf_path)
37
  return pdf_path
38
 
39
+ def create_markdown_table():
40
+ global chapters
41
+ markdown_table = "| 챕터 | 이야기 | 그림 |\n|-------|-------|------|\n"
42
+ for i, chapter in enumerate(chapters, start=1):
43
+ markdown_table += f"| Chapter {i} | {chapter['story']} | ![Chapter {i} Image]({chapter['image']}) |\n"
44
+ return markdown_table
45
+
46
  OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
47
  clipi_client = Client("https://fffiloni-clip-interrogator-2.hf.space/")
48
 
 
81
  current_story = clipi_result
82
  return next_chapter(audience, keyword, protagonist)
83
 
84
+ # Gradio UI
85
  css = """
86
  #col-container {max-width: 910px; margin-left: auto; margin-right: auto;}
87
  a {text-decoration-line: underline; font-weight: 600;}
88
  """
89
 
 
90
  with gr.Blocks(css=css) as demo:
91
  with gr.Column(elem_id="col-container"):
92
  gr.Markdown(
 
107
  with gr.Column():
108
  chapter_story = gr.Markdown(label="이야기", elem_id="chapter_story")
109
  chapter_image = gr.Image(label="그림", elem_id="chapter_image")
110
+ table_markdown = gr.Markdown(label="이야기와 그림 정리", elem_id="table_markdown") # 추가된 마크다운 테이블
111
 
112
+ submit_btn.click(fn=infer, inputs=[image_in, audience, keyword_in, protagonist_in], outputs=[chapter_story, chapter_image, table_markdown])
113
+ next_chapter_btn.click(fn=lambda: next_chapter(audience=audience.value, keyword=keyword_in.value, protagonist=protagonist_in.value), outputs=[chapter_story, chapter_image, table_markdown])
114
  save_pdf_btn.click(fn=save_as_pdf, outputs=gr.File(label="PDF 다운로드"))
115
 
116
  demo.queue(max_size=12).launch()
117
+
118
+ # 수정된 함수
119
+ def next_chapter(audience, keyword, protagonist):
120
+ global chapter_num, current_story, current_image_url, chapters
121
+ current_image_url = generate_image_url(current_story)
122
+ gr.Info(f'Chapter {chapter_num}를 생성하고 있습니다...')
123
+ chapter_prompt = f"{story_intro}\n\nKeyword: {keyword}\nProtagonist: {protagonist}\n\n![Chapter {chapter_num} Image]({current_image_url})\n\nChapter {chapter_num} 내용을 만들어줘."
124
+ chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo-16k", messages=[{"role": "user", "content": chapter_prompt}])
125
+ current_story = chat_completion.choices[0].message.content
126
+ chapters.append({"story": current_story, "image": current_image_url})
127
+ chapter_num += 1
128
+ return current_story, current_image_url, create_markdown_table() # 마크다운 테이블 추가