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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -17
app.py CHANGED
@@ -1,7 +1,6 @@
1
  from fpdf import FPDF
2
  import gradio as gr
3
  from urllib.parse import quote
4
- import re
5
  import os
6
  import openai
7
  from gradio_client import Client
@@ -42,7 +41,7 @@ def create_markdown_table():
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
 
@@ -61,7 +60,7 @@ def next_chapter(audience, keyword, protagonist):
61
  current_story = chat_completion.choices[0].message.content
62
  chapters.append({"story": current_story, "image": current_image_url})
63
  chapter_num += 1
64
- return current_story, current_image_url
65
 
66
  def infer(image_input, audience, keyword, protagonist):
67
  global story_intro, current_story, current_image_url, chapter_num, chapters
@@ -81,7 +80,6 @@ def infer(image_input, audience, keyword, protagonist):
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;}
@@ -107,22 +105,10 @@ with gr.Blocks(css=css) as demo:
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() # 마크다운 테이블 추가
 
1
  from fpdf import FPDF
2
  import gradio as gr
3
  from urllib.parse import quote
 
4
  import os
5
  import openai
6
  from gradio_client import Client
 
41
  for i, chapter in enumerate(chapters, start=1):
42
  markdown_table += f"| Chapter {i} | {chapter['story']} | ![Chapter {i} Image]({chapter['image']}) |\n"
43
  return markdown_table
44
+
45
  OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
46
  clipi_client = Client("https://fffiloni-clip-interrogator-2.hf.space/")
47
 
 
60
  current_story = chat_completion.choices[0].message.content
61
  chapters.append({"story": current_story, "image": current_image_url})
62
  chapter_num += 1
63
+ return current_story, current_image_url, create_markdown_table()
64
 
65
  def infer(image_input, audience, keyword, protagonist):
66
  global story_intro, current_story, current_image_url, chapter_num, chapters
 
80
  current_story = clipi_result
81
  return next_chapter(audience, keyword, protagonist)
82
 
 
83
  css = """
84
  #col-container {max-width: 910px; margin-left: auto; margin-right: auto;}
85
  a {text-decoration-line: underline; font-weight: 600;}
 
105
  with gr.Column():
106
  chapter_story = gr.Markdown(label="이야기", elem_id="chapter_story")
107
  chapter_image = gr.Image(label="그림", elem_id="chapter_image")
108
+ table_markdown = gr.Markdown(label="이야기와 그림 정리", elem_id="table_markdown")
109
 
110
  submit_btn.click(fn=infer, inputs=[image_in, audience, keyword_in, protagonist_in], outputs=[chapter_story, chapter_image, table_markdown])
111
  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])
112
  save_pdf_btn.click(fn=save_as_pdf, outputs=gr.File(label="PDF 다운로드"))
113
 
114
  demo.queue(max_size=12).launch()