Update app.py
Browse files
app.py
CHANGED
|
@@ -32,19 +32,27 @@ def convert_history_to_openai_format(history):
|
|
| 32 |
global_system_prompt = "You are a helpful assistant."
|
| 33 |
|
| 34 |
formatted_history = [{"role": "system", "content": global_system_prompt}]
|
| 35 |
-
for user_msg, assistant_msg in history:
|
| 36 |
-
if isinstance(user_msg, tuple) and ('.png' in user_msg[0] or '.jpg' in user_msg[0]):
|
| 37 |
-
encoded_image = encode_image(user_msg[0])
|
| 38 |
-
text = 'Help me based on the image'
|
| 39 |
-
if user_msg[1] != '':
|
| 40 |
-
text = user_msg[1]
|
| 41 |
-
content = [{'type':'text', 'text':text}, {'type':'image_url', 'image_url':{'url':f'data:image/jpeg;base64,{encoded_image}'}}]
|
| 42 |
-
formatted_history.append({"role": 'user', "content": content})
|
| 43 |
-
else:
|
| 44 |
-
formatted_history.append({"role": 'user', "content": user_msg})
|
| 45 |
-
if isinstance(assistant_msg, str):
|
| 46 |
-
formatted_history.append({"role": 'assistant', "content": assistant_msg})
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
return formatted_history
|
| 49 |
|
| 50 |
def bot(history):
|
|
@@ -55,7 +63,7 @@ def bot(history):
|
|
| 55 |
)
|
| 56 |
|
| 57 |
chatbot_message = response.choices[0].message['content'].strip()
|
| 58 |
-
history[-1][1]
|
| 59 |
return history
|
| 60 |
|
| 61 |
def add_message(history, message):
|
|
@@ -80,4 +88,4 @@ with gr.Blocks() as demo:
|
|
| 80 |
chat_input.submit(lambda message, history: bot(add_message(history, message)), [chat_input, chatbot], [chatbot, chat_input])
|
| 81 |
|
| 82 |
# Launch the Gradio interface
|
| 83 |
-
demo.launch()
|
|
|
|
| 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):
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
chatbot_message = response.choices[0].message['content'].strip()
|
| 66 |
+
history[-1] = (history[-1][0], chatbot_message) # Update the last entry with the assistant's response
|
| 67 |
return history
|
| 68 |
|
| 69 |
def add_message(history, message):
|
|
|
|
| 88 |
chat_input.submit(lambda message, history: bot(add_message(history, message)), [chat_input, chatbot], [chatbot, chat_input])
|
| 89 |
|
| 90 |
# Launch the Gradio interface
|
| 91 |
+
demo.launch(share=True) # Enable public sharing
|