JUNGU commited on
Commit
71f80a1
ยท
1 Parent(s): 09f390b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -23
app.py CHANGED
@@ -1,10 +1,8 @@
1
  import gradio as gr
2
- from share_btn import community_icon_html, loading_icon_html, share_js
3
- import re
4
- import os
5
  import openai
6
- OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
7
  from gradio_client import Client
 
 
8
  clipi_client = Client("https://fffiloni-clip-interrogator-2.hf.space/")
9
 
10
  def generate_image_url(keywords):
@@ -17,7 +15,7 @@ def infer(image_input, audience, keyword, protagonist):
17
  story_intro = f"""
18
  # Illustrated Tales
19
  ## Created by [Sigkawat Pengnoo](https://flowgpt.com/prompt/qzv2D3OvHkzkfSE4rQCqv) at FlowGPT
20
-
21
  Keyword: {keyword}
22
  Protagonist: {protagonist}
23
  ํ•œ๊ตญ์–ด๋กœ ๋‹ต๋ณ€ํ•ด์ค˜.
@@ -29,18 +27,74 @@ def infer(image_input, audience, keyword, protagonist):
29
 
30
  chapters = []
31
  current_image_url = generate_image_url(clipi_result)
32
- for chapter_num in range(1, 4): # 3๊ฐœ์˜ ์žฅ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
 
33
  chapter_prompt = f"{story_intro}\n\n![Chapter {chapter_num} Image]({current_image_url})\n\nChapter {chapter_num} ๋‚ด์šฉ์„ ๋งŒ๋“ค์–ด์ค˜."
34
  chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo-16k", messages=[{"role": "user", "content": chapter_prompt}])
35
  chapter_story = chat_completion.choices[0].message.content
36
  chapters.append(chapter_story)
37
- current_image_url = generate_image_url(chapter_story) # ๋‹ค์Œ ์žฅ์˜ ์ด๋ฏธ์ง€ URL์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
38
 
39
  formatted_text = '\n'.join(chapters)
40
  return formatted_text, gr.Group.update(visible=True)
41
 
42
  css = """
43
- # ์—ฌ๊ธฐ์— ์ด์ „์— ์ •์˜ํ•œ CSS ์ฝ”๋“œ๋ฅผ ๋„ฃ์œผ์„ธ์š”.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  """
45
 
46
  with gr.Blocks(css=css) as demo:
@@ -60,19 +114,6 @@ with gr.Blocks(css=css) as demo:
60
  submit_btn = gr.Button('์ด์•ผ๊ธฐ์™€ ๊ทธ๋ฆผ์„ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”')
61
  with gr.Column():
62
  story = gr.Textbox(label="์ƒ์„ฑ๋œ ์ด์•ผ๊ธฐ์™€ ๊ทธ๋ฆผ", elem_id="story")
63
- with gr.Group(elem_id="share-btn-container", visible=False) as share_group:
64
- community_icon = gr.HTML(community_icon_html)
65
- loading_icon = gr.HTML(loading_icon_html)
66
- share_button = gr.Button("Share to community", elem_id="share-btn")
67
-
68
- gr.Examples(examples=[["./examples/crabby.png", "Children", "ํ•ด๋ณ€์—์„œ์˜ ๋ชจํ—˜", "๊ฒŒ ์ฃผ์ธ๊ณต"],["./examples/hopper.jpeg", "Adult", "์‚ฐ์—์„œ์˜ ์—ฌํ–‰", "ํ† ๋ผ ์ฃผ์ธ๊ณต"]],
69
- fn=infer,
70
- inputs=[image_in, audience, keyword_in, protagonist_in],
71
- outputs=[story, share_group],
72
- cache_examples=True
73
- )
74
-
75
- submit_btn.click(fn=infer, inputs=[image_in, audience, keyword_in, protagonist_in], outputs=[story, share_group])
76
- share_button.click(None, [], [], _js=share_js)
77
 
78
- demo.queue(max_size=12).launch()
 
 
1
  import gradio as gr
 
 
 
2
  import openai
 
3
  from gradio_client import Client
4
+
5
+ OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
6
  clipi_client = Client("https://fffiloni-clip-interrogator-2.hf.space/")
7
 
8
  def generate_image_url(keywords):
 
15
  story_intro = f"""
16
  # Illustrated Tales
17
  ## Created by [Sigkawat Pengnoo](https://flowgpt.com/prompt/qzv2D3OvHkzkfSE4rQCqv) at FlowGPT
18
+
19
  Keyword: {keyword}
20
  Protagonist: {protagonist}
21
  ํ•œ๊ตญ์–ด๋กœ ๋‹ต๋ณ€ํ•ด์ค˜.
 
27
 
28
  chapters = []
29
  current_image_url = generate_image_url(clipi_result)
30
+ for chapter_num in range(1, 4): # 3๊ฐœ์˜ ์žฅ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
31
+ gr.Info(f'Chapter {chapter_num}๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค...')
32
  chapter_prompt = f"{story_intro}\n\n![Chapter {chapter_num} Image]({current_image_url})\n\nChapter {chapter_num} ๋‚ด์šฉ์„ ๋งŒ๋“ค์–ด์ค˜."
33
  chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo-16k", messages=[{"role": "user", "content": chapter_prompt}])
34
  chapter_story = chat_completion.choices[0].message.content
35
  chapters.append(chapter_story)
36
+ current_image_url = generate_image_url(chapter_story) # ๋‹ค์Œ ์žฅ์˜ ์ด๋ฏธ์ง€ URL์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
37
 
38
  formatted_text = '\n'.join(chapters)
39
  return formatted_text, gr.Group.update(visible=True)
40
 
41
  css = """
42
+ #col-container {max-width: 910px; margin-left: auto; margin-right: auto;}
43
+ a {text-decoration-line: underline; font-weight: 600;}
44
+ a {text-decoration-line: underline; font-weight: 600;}
45
+ .animate-spin {
46
+ animation: spin 1s linear infinite;
47
+ }
48
+ @keyframes spin {
49
+ from {
50
+ transform: rotate(0deg);
51
+ }
52
+ to {
53
+ transform: rotate(360deg);
54
+ }
55
+ }
56
+ #share-btn-container {
57
+ display: flex;
58
+ padding-left: 0.5rem !important;
59
+ padding-right: 0.5rem !important;
60
+ background-color: #000000;
61
+ justify-content: center;
62
+ align-items: center;
63
+ border-radius: 9999px !important;
64
+ max-width: 13rem;
65
+ }
66
+ div#share-btn-container > div {
67
+ flex-direction: row;
68
+ background: black;
69
+ align-items: center;
70
+ }
71
+ #share-btn-container:hover {
72
+ background-color: #060606;
73
+ }
74
+ #share-btn {
75
+ all: initial;
76
+ color: #ffffff;
77
+ font-weight: 600;
78
+ cursor:pointer;
79
+ font-family: 'IBM Plex Sans', sans-serif;
80
+ margin-left: 0.5rem !important;
81
+ padding-top: 0.5rem !important;
82
+ padding-bottom: 0.5rem !important;
83
+ right:0;
84
+ }
85
+ #share-btn * {
86
+ all: unset;
87
+ }
88
+ #share-btn-container div:nth-child(-n+2){
89
+ width: auto !important;
90
+ min-height: 0px !important;
91
+ }
92
+ #share-btn-container .wrap {
93
+ display: none !important;
94
+ }
95
+ #share-btn-container.hidden {
96
+ display: none!important;
97
+ }
98
  """
99
 
100
  with gr.Blocks(css=css) as demo:
 
114
  submit_btn = gr.Button('์ด์•ผ๊ธฐ์™€ ๊ทธ๋ฆผ์„ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”')
115
  with gr.Column():
116
  story = gr.Textbox(label="์ƒ์„ฑ๋œ ์ด์•ผ๊ธฐ์™€ ๊ทธ๋ฆผ", elem_id="story")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
+ submit_btn.click(fn=infer, inputs=[image_in, audience, keyword_in, protagonist_in], outputs=[story])
119
+ demo.queue(max_size=12).launch()