Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
from urllib.parse import quote
|
3 |
-
|
4 |
-
from
|
5 |
-
from reportlab.lib.styles import getSampleStyleSheet
|
6 |
import urllib.request
|
7 |
-
import re
|
8 |
import os
|
9 |
-
import openai
|
10 |
from gradio_client import Client
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
16 |
clipi_client = Client("https://fffiloni-clip-interrogator-2.hf.space/")
|
@@ -27,17 +42,13 @@ def generate_image_url(keywords):
|
|
27 |
|
28 |
def save_as_pdf():
|
29 |
global chapters
|
|
|
|
|
|
|
|
|
|
|
30 |
pdf_path = "/mnt/data/chapters.pdf"
|
31 |
-
pdf
|
32 |
-
story = []
|
33 |
-
styles = getSampleStyleSheet()
|
34 |
-
for i, chapter in enumerate(chapters):
|
35 |
-
story.append(Paragraph(f"Chapter {i + 1}", styles["Heading1"]))
|
36 |
-
story.append(Paragraph(chapter["story"], styles["BodyText"]))
|
37 |
-
image_path = f"/mnt/data/image_{i}.png"
|
38 |
-
download_image(chapter["image"], image_path)
|
39 |
-
story.append(Image(image_path))
|
40 |
-
pdf.build(story)
|
41 |
return pdf_path
|
42 |
|
43 |
def next_chapter(audience, keyword, protagonist):
|
@@ -97,6 +108,6 @@ with gr.Blocks(css=css) as demo:
|
|
97 |
|
98 |
submit_btn.click(fn=infer, inputs=[image_in, audience, keyword_in, protagonist_in], outputs=[chapter_story, chapter_image])
|
99 |
next_chapter_btn.click(fn=lambda: next_chapter(audience=audience.value, keyword=keyword_in.value, protagonist=protagonist_in.value), outputs=[chapter_story, chapter_image])
|
100 |
-
save_pdf_btn.click(fn=save_as_pdf, outputs=gr.
|
101 |
|
102 |
demo.queue(max_size=12).launch()
|
|
|
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/")
|
|
|
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):
|
|
|
108 |
|
109 |
submit_btn.click(fn=infer, inputs=[image_in, audience, keyword_in, protagonist_in], outputs=[chapter_story, chapter_image])
|
110 |
next_chapter_btn.click(fn=lambda: next_chapter(audience=audience.value, keyword=keyword_in.value, protagonist=protagonist_in.value), outputs=[chapter_story, chapter_image])
|
111 |
+
save_pdf_btn.click(fn=save_as_pdf, outputs=gr.Download(label="PDF ๋ค์ด๋ก๋"))
|
112 |
|
113 |
demo.queue(max_size=12).launch()
|