Medvira commited on
Commit
5bb6024
·
verified ·
1 Parent(s): c4b81c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -23
app.py CHANGED
@@ -28,31 +28,21 @@ def convert_history_to_openai_format(history):
28
  list of dict: The formatted history for OpenAI with "role" as either "user" or "assistant".
29
  """
30
  global global_system_prompt
31
- if global_system_prompt is None:
32
  global_system_prompt = "You are a helpful assistant."
33
-
34
- formatted_history = [{"role": "system", "content": global_system_prompt}]
35
-
36
- for entry in history:
37
- # Ensure entry is a tuple and has exactly two elements
38
- if isinstance(entry, tuple) and len(entry) == 2:
39
- user_msg, assistant_msg = entry
40
-
41
- if isinstance(user_msg, tuple) and ('.png' in user_msg[0] or '.jpg' in user_msg[0]):
42
- encoded_image = encode_image(user_msg[0])
43
- text = 'Help me based on the image'
44
- if user_msg[1] != '':
45
- text = user_msg[1]
46
- content = [{'type':'text', 'text':text}, {'type':'image_url', 'image_url':{'url':f'data:image/jpeg;base64,{encoded_image}'}}]
47
- formatted_history.append({"role": 'user', "content": content})
48
- else:
49
- formatted_history.append({"role": 'user', "content": user_msg})
50
-
51
- if isinstance(assistant_msg, str):
52
- formatted_history.append({"role": 'assistant', "content": assistant_msg})
53
  else:
54
- print(f"Unexpected entry format in history: {entry}") # Debugging output
55
-
 
56
  return formatted_history
57
 
58
  def bot(history):
 
28
  list of dict: The formatted history for OpenAI with "role" as either "user" or "assistant".
29
  """
30
  global global_system_prompt
31
+ if global_system_prompt == None:
32
  global_system_prompt = "You are a helpful assistant."
33
+ formatted_history = [{"role": "system", "content": global_system_prompt},]
34
+ for user_msg, assistant_msg in history:
35
+ if ('.png' in user_msg[0]) or ('.jpg' in user_msg[0]):
36
+ encoded_image = encode_image(user_msg[0])
37
+ text = 'help me based on the image'
38
+ if user_msg[1] != '':
39
+ text = user_msg[1]
40
+ content = [{'type':'text', 'text':text},{'type':'image_url','image_url':{'url':f'data:image/jpeg;base64,{encoded_image}'}}]
41
+ formatted_history.append({"role": 'user', "content": content})
 
 
 
 
 
 
 
 
 
 
 
42
  else:
43
+ formatted_history.append({"role": 'user', "content": user_msg})
44
+ if isinstance(assistant_msg,str):
45
+ formatted_history.append({"role": 'assistant', "content": assistant_msg})
46
  return formatted_history
47
 
48
  def bot(history):