@@ -215,4 +215,114 @@ def develop_prompt_from_text_and_generate_image (response, generate_OpenAIGPT, i
215215 png .write (image_data )
216216 display (Image (data = image_data ))
217217
218- return img_list
218+ return img_list
219+
220+
221+ ########## Chat-like interaction with OpenAI models, text or images ##########
222+
223+ import base64
224+ import requests
225+ import json
226+ from transformers .image_utils import load_image
227+
228+ # Function to encode the image
229+ def encode_image (image_path ):
230+ with open (image_path , "rb" ) as image_file :
231+ return base64 .b64encode (image_file .read ()).decode ('utf-8' )
232+ from io import BytesIO
233+
234+ def is_url (val ) -> bool :
235+ return isinstance (val , str ) and val .startswith ("http" )
236+
237+ def get_answer ( query = 'What is shown in this image?' ,model = "gpt-4o" ,
238+ image = None , payload = None , max_tokens = 1024 , temperature = 0.1 ,
239+ top_p = 0.95 , top_k = 40 , init_instr = "Look at this image: " ,
240+ display_image = False ,
241+ ):
242+
243+ base64_image = None
244+ if image != None :
245+ if is_url (image ):
246+ image = load_image (image )
247+ else :
248+ image = load_image (image )
249+
250+ if display_image :
251+ display (image )
252+
253+ # Convert the image to a byte array
254+ buffered = BytesIO ()
255+ image .save (buffered , format = "PNG" ) # Use the appropriate format for your image
256+ img_byte_array = buffered .getvalue ()
257+
258+ # Encode the byte array into a base64 string
259+ base64_image = base64 .b64encode (img_byte_array ).decode ("utf-8" )
260+
261+ headers = {
262+ "Content-Type" : "application/json" ,
263+ "Authorization" : f"Bearer { api_key } "
264+ }
265+
266+ if payload == None :
267+ if base64_image != None :
268+ payload = {
269+ "model" : model ,
270+ "messages" : [
271+ {
272+ "role" : "user" ,
273+ "content" : [
274+ {
275+ "type" : "text" ,
276+ "text" : query
277+ },
278+ {
279+ "type" : "image_url" ,
280+ "image_url" : {
281+ "url" : f"data:image/jpeg;base64,{ base64_image } "
282+ }
283+ }
284+ ]
285+ }
286+ ],
287+ "max_tokens" : max_tokens
288+ }
289+ else :
290+ payload = {
291+ "model" : model ,
292+ "messages" : [
293+ {
294+ "role" : "user" ,
295+ "content" : [
296+ {
297+ "type" : "text" ,
298+ "text" : query
299+ },
300+
301+ ]
302+ }
303+ ],
304+ "max_tokens" : max_tokens
305+ }
306+
307+ else :
308+ payload ['messages' ].append ({"role" :"user" , "content" : [
309+ {"type" : "text" , "text" : query } ] }, )
310+
311+ response = requests .post ("https://api.openai.com/v1/chat/completions" , headers = headers , json = payload )
312+ response_dict = response .json ()
313+ message_content = response_dict ['choices' ][0 ]['message' ]['content' ]
314+
315+ payload ['messages' ].append ({"role" :"assistant" , "content" : [
316+ {"type" : "text" , "text" : message_content } ] }, )
317+
318+ return message_content , payload
319+ '''
320+ answer, payload= get_answer( query='What is graphene?', payload=None, image= None,
321+ display_image=True, model="gpt-4o-mini")
322+ answer, payload
323+ '''
324+ '''
325+ answer, payload= get_answer( query='What do you see?', payload=None, image= "1920px-Spiderweb_with_frost.jpg",
326+ display_image=True, model="gpt-4o-mini")
327+ answer
328+ '''
0 commit comments