1
+ from PIL import Image
2
+ import base64
3
+ import io
1
4
import os
2
5
from dotenv import load_dotenv
3
6
from typing import Dict
7
+ import urllib .parse
4
8
import requests
5
9
from langchain_openai import ChatOpenAI
6
10
from langchain_core .prompts import ChatPromptTemplate , PromptTemplate
7
- from langchain .chains .combine_documents import create_stuff_documents_chain
8
- from langchain_community .utilities .dalle_image_generator import DallEAPIWrapper
9
11
from langchain_core .output_parsers import StrOutputParser
12
+ import openai
13
+ from ..utils import get_extension_from_mimetype
10
14
11
15
# Load environment variables from .env file
12
16
load_dotenv ()
13
17
KB_API_HOST = os .environ ["KB_API_HOST" ]
14
18
19
+ from ..libs .storage import GCPStorage
20
+ storage = GCPStorage ()
21
+ STORAGE_THUMBNAIL_FOLDER = os .environ ["STORAGE_THUMBNAIL_FOLDER" ]
22
+ STORAGE_PROXY_PATH = os .environ ["STORAGE_PROXY_PATH" ]
23
+
15
24
llm = ChatOpenAI (temperature = 0 , model_name = "gpt-4o-mini" )
16
25
# Define prompt
17
26
prompt = ChatPromptTemplate .from_messages (
@@ -107,7 +116,7 @@ def generate_course_summary(course_id: str):
107
116
"description" : course_details ["result" ]["content" ]["description" ],
108
117
"toc" : formatted_toc
109
118
})
110
- return result
119
+ return course_details [ "result" ][ "content" ], result
111
120
112
121
def generate_image_prompt (summary : str ):
113
122
prompt = PromptTemplate .from_template (prompt_template )
@@ -125,12 +134,49 @@ def generate_image_prompt(summary: str):
125
134
def generate_image (image_prompt : str ):
126
135
############################
127
136
# Generate a thumbnail image
128
- image_url = DallEAPIWrapper ( model = "dall-e-3" , size = "1792x1024" ). run ( f"""
137
+ prompt = f"""
129
138
Do not print any text on image, just use it AS-IS:
130
139
{ image_prompt }
131
140
132
141
Guidelines:
133
142
- Please ensure that the image does not include any text or human imagery.
134
143
- Generate image without map of india.
135
- """ )
136
- return image_url
144
+ """
145
+ response = openai .images .generate (
146
+ prompt = prompt ,
147
+ model = "dall-e-3" ,
148
+ size = "1024x1024" ,
149
+ quality = "standard" ,
150
+ n = 1 ,
151
+ response_format = "b64_json"
152
+ )
153
+ return response .data [0 ].b64_json
154
+
155
+ # Compress and convert image to JPEG
156
+ def compress_image (image_data ):
157
+ # Decode the base64 image data
158
+ image_bytes = base64 .b64decode (image_data )
159
+
160
+ # Load the image into a PIL Image object
161
+ image = Image .open (io .BytesIO (image_bytes ))
162
+
163
+ # Convert to JPEG and compress the image
164
+ compressed_buffer = io .BytesIO ()
165
+ image .save (compressed_buffer , format = "JPEG" , quality = 80 ) # Adjust quality if needed
166
+ compressed_image_data = compressed_buffer .getvalue ()
167
+ return compressed_image_data
168
+
169
+ def generate_public_url (content , image_data , mime_type = None ):
170
+
171
+ if mime_type is None :
172
+ mime_type = "image/jpeg"
173
+
174
+ compressed_image_data = compress_image (image_data )
175
+
176
+ extension = get_extension_from_mimetype (mime_type )
177
+ filename = f"{ content ["name" ]} .{ extension } "
178
+ filepath = os .path .join (STORAGE_THUMBNAIL_FOLDER , content ["identifier" ], filename )
179
+ storage .write_file (filepath , compressed_image_data , mime_type )
180
+ # image_urls.append(storage.public_url(filepath))
181
+ public_url = urllib .parse .urljoin (KB_API_HOST , os .path .join (STORAGE_PROXY_PATH , content ["identifier" ], filename ))
182
+ return public_url
0 commit comments