JUNGU commited on
Commit
a58e796
ยท
1 Parent(s): 582f42c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -30
app.py CHANGED
@@ -1,31 +1,40 @@
 
1
  import gradio as gr
2
  from urllib.parse import quote
3
- import openai
4
- from fpdf import FPDF
5
- import urllib.request
6
  import os
 
7
  from gradio_client import Client
8
 
9
- # FPDF ํด๋ž˜์Šค ํ™•์žฅ
 
 
 
10
  class PDF(FPDF):
11
  def chapter_title(self, num, label):
12
  self.set_font('Arial', 'B', 12)
13
- self.cell(0, 10, 'Chapter %d : %s' % (num, label), 0, 1, 'C')
14
  self.ln(10)
15
 
16
- def chapter_body(self, body, image_path):
17
  self.set_font('Arial', '', 12)
18
  self.multi_cell(0, 10, body)
19
- self.image(image_path, x=10, w=190)
20
- self.ln()
21
 
22
  def add_chapter(self, num, title, body, image_url):
23
- image_path = f"/mnt/data/image_{num}.png"
24
- urllib.request.urlretrieve(image_url, image_path)
25
  self.add_page()
26
  self.chapter_title(num, title)
27
- self.chapter_body(body, image_path)
28
- os.remove(image_path) # ์ด๋ฏธ์ง€ ํŒŒ์ผ ์‚ญ์ œ
 
 
 
 
 
 
 
 
29
 
30
  OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
31
  clipi_client = Client("https://fffiloni-clip-interrogator-2.hf.space/")
@@ -36,36 +45,21 @@ current_story = ""
36
  current_image_url = ""
37
  chapters = []
38
 
39
- def generate_image_url(keywords):
40
- truncated_keywords = keywords[:20]
41
- return f"https://image.pollinations.ai/prompt/{quote(truncated_keywords)}"
42
-
43
- def save_as_pdf():
44
- global chapters
45
- pdf = PDF()
46
- pdf.set_auto_page_break(auto=True, margin=15)
47
- pdf.set_title("Chapters")
48
- for i, chapter in enumerate(chapters, 1):
49
- pdf.add_chapter(i, f"Chapter {i}", chapter["story"], chapter["image"])
50
- pdf_path = "/mnt/data/chapters.pdf"
51
- pdf.output(pdf_path)
52
- return pdf_path
53
-
54
  def next_chapter(audience, keyword, protagonist):
55
- global chapter_num, current_story, current_image_url
56
  current_image_url = generate_image_url(current_story)
57
  gr.Info(f'Chapter {chapter_num}๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค...')
58
  chapter_prompt = f"{story_intro}\n\nKeyword: {keyword}\nProtagonist: {protagonist}\n\n![Chapter {chapter_num} Image]({current_image_url})\n\nChapter {chapter_num} ๋‚ด์šฉ์„ ๋งŒ๋“ค์–ด์ค˜."
59
  chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo-16k", messages=[{"role": "user", "content": chapter_prompt}])
60
  current_story = chat_completion.choices[0].message.content
61
- chapter_num += 1
62
  chapters.append({"story": current_story, "image": current_image_url})
 
63
  return current_story, current_image_url
64
 
65
  def infer(image_input, audience, keyword, protagonist):
66
  global story_intro, current_story, current_image_url, chapter_num, chapters
67
- chapters = []
68
  chapter_num = 1
 
69
  gr.Info('Calling CLIP Interrogator, ์ด๋ฏธ์ง€๋ฅผ ํ•ด์„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค...')
70
  clipi_result = clipi_client.predict(image_input, "best", 4, api_name="/clipi2")[0]
71
  story_intro = f"""
@@ -80,11 +74,13 @@ def infer(image_input, audience, keyword, protagonist):
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;}
86
  """
87
 
 
88
  with gr.Blocks(css=css) as demo:
89
  with gr.Column(elem_id="col-container"):
90
  gr.Markdown(
 
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
8
 
9
+ def generate_image_url(keywords):
10
+ truncated_keywords = keywords[:20]
11
+ return f"https://image.pollinations.ai/prompt/{quote(truncated_keywords)}"
12
+
13
  class PDF(FPDF):
14
  def chapter_title(self, num, label):
15
  self.set_font('Arial', 'B', 12)
16
+ self.cell(0, 10, f"Chapter {num} : {label}", 0, 1, 'C')
17
  self.ln(10)
18
 
19
+ def chapter_body(self, body, image_url):
20
  self.set_font('Arial', '', 12)
21
  self.multi_cell(0, 10, body)
22
+ self.image(image_url, x=10, w=100)
23
+ self.ln(60)
24
 
25
  def add_chapter(self, num, title, body, image_url):
 
 
26
  self.add_page()
27
  self.chapter_title(num, title)
28
+ self.chapter_body(body, image_url)
29
+
30
+ def save_as_pdf():
31
+ global chapters
32
+ pdf_path = "/mnt/data/chapters.pdf"
33
+ pdf = PDF()
34
+ for i, chapter in enumerate(chapters, start=1):
35
+ pdf.add_chapter(i, f"Chapter {i}", chapter["story"], chapter["image"])
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/")
 
45
  current_image_url = ""
46
  chapters = []
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  def next_chapter(audience, keyword, protagonist):
49
+ global chapter_num, current_story, current_image_url, chapters
50
  current_image_url = generate_image_url(current_story)
51
  gr.Info(f'Chapter {chapter_num}๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค...')
52
  chapter_prompt = f"{story_intro}\n\nKeyword: {keyword}\nProtagonist: {protagonist}\n\n![Chapter {chapter_num} Image]({current_image_url})\n\nChapter {chapter_num} ๋‚ด์šฉ์„ ๋งŒ๋“ค์–ด์ค˜."
53
  chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo-16k", messages=[{"role": "user", "content": chapter_prompt}])
54
  current_story = chat_completion.choices[0].message.content
 
55
  chapters.append({"story": current_story, "image": current_image_url})
56
+ chapter_num += 1
57
  return current_story, current_image_url
58
 
59
  def infer(image_input, audience, keyword, protagonist):
60
  global story_intro, current_story, current_image_url, chapter_num, chapters
 
61
  chapter_num = 1
62
+ chapters = []
63
  gr.Info('Calling CLIP Interrogator, ์ด๋ฏธ์ง€๋ฅผ ํ•ด์„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค...')
64
  clipi_result = clipi_client.predict(image_input, "best", 4, api_name="/clipi2")[0]
65
  story_intro = f"""
 
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(