Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,22 @@ from urllib.parse import quote
|
|
4 |
import os
|
5 |
import openai
|
6 |
from gradio_client import Client
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def generate_image_url(keywords):
|
9 |
truncated_keywords = keywords[:20]
|
10 |
return f"https://image.pollinations.ai/prompt/{quote(truncated_keywords)}"
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
class PDF(FPDF):
|
13 |
def chapter_title(self, num, label):
|
14 |
self.set_font('Arial', 'B', 12)
|
@@ -22,9 +33,10 @@ class PDF(FPDF):
|
|
22 |
self.ln(60)
|
23 |
|
24 |
def add_chapter(self, num, title, body, image_url):
|
|
|
25 |
self.add_page()
|
26 |
self.chapter_title(num, title)
|
27 |
-
self.chapter_body(body,
|
28 |
|
29 |
def save_as_pdf():
|
30 |
global chapters
|
|
|
4 |
import os
|
5 |
import openai
|
6 |
from gradio_client import Client
|
7 |
+
import requests
|
8 |
+
from PIL import Image
|
9 |
+
from io import BytesIO
|
10 |
+
|
11 |
|
12 |
def generate_image_url(keywords):
|
13 |
truncated_keywords = keywords[:20]
|
14 |
return f"https://image.pollinations.ai/prompt/{quote(truncated_keywords)}"
|
15 |
|
16 |
+
def download_image(image_url):
|
17 |
+
response = requests.get(image_url)
|
18 |
+
img = Image.open(BytesIO(response.content))
|
19 |
+
local_image_path = f"/tmp/{image_url.split('/')[-1]}.png"
|
20 |
+
img.save(local_image_path)
|
21 |
+
return local_image_path
|
22 |
+
|
23 |
class PDF(FPDF):
|
24 |
def chapter_title(self, num, label):
|
25 |
self.set_font('Arial', 'B', 12)
|
|
|
33 |
self.ln(60)
|
34 |
|
35 |
def add_chapter(self, num, title, body, image_url):
|
36 |
+
local_image_path = download_image(image_url)
|
37 |
self.add_page()
|
38 |
self.chapter_title(num, title)
|
39 |
+
self.chapter_body(body, local_image_path)
|
40 |
|
41 |
def save_as_pdf():
|
42 |
global chapters
|