Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -47,55 +47,68 @@ def bot_streaming(message, history, together_api_key, max_new_tokens=250, temper
|
|
| 47 |
|
| 48 |
messages = [{"role": "system", "content": prompt}]
|
| 49 |
|
| 50 |
-
# Build the conversation history for the API
|
| 51 |
-
for idx, (user_msg, assistant_msg) in enumerate(history):
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
# Prepare the current message
|
| 68 |
content = []
|
| 69 |
user_text = ""
|
| 70 |
|
| 71 |
try:
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
# Handle image input
|
| 79 |
-
if 'files' in message and len(message['files']) > 0:
|
| 80 |
-
file_info = message['files'][0]
|
| 81 |
-
if isinstance(file_info, dict) and 'path' in file_info:
|
| 82 |
-
image_path = file_info['path']
|
| 83 |
-
elif isinstance(file_info, str):
|
| 84 |
-
image_path = file_info
|
| 85 |
-
else:
|
| 86 |
-
raise ValueError("Invalid file information provided.")
|
| 87 |
-
|
| 88 |
-
# Encode the image to base64
|
| 89 |
-
image_base64 = encode_image(image_path)
|
| 90 |
-
content.append({
|
| 91 |
"type": "image_url",
|
| 92 |
-
"image_url": {
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
# If there's an error processing the input, append it to history and yield
|
| 101 |
error_message = f"An error occurred while processing your input: {str(e)}"
|
|
@@ -125,20 +138,21 @@ def bot_streaming(message, history, together_api_key, max_new_tokens=250, temper
|
|
| 125 |
for chunk in stream:
|
| 126 |
# Extract the content from the API response
|
| 127 |
chunk_content = chunk.choices[0].delta.content or ""
|
|
|
|
| 128 |
response += chunk_content
|
| 129 |
-
# Update the last assistant message in history
|
| 130 |
-
if history:
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
else:
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
if not response:
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
# Handle exceptions from the API call
|
|
|
|
| 47 |
|
| 48 |
messages = [{"role": "system", "content": prompt}]
|
| 49 |
|
| 50 |
+
# # Build the conversation history for the API
|
| 51 |
+
# for idx, (user_msg, assistant_msg) in enumerate(history):
|
| 52 |
+
# # Append user messages
|
| 53 |
+
# messages.append({
|
| 54 |
+
# "role": "user",
|
| 55 |
+
# "content": [
|
| 56 |
+
# {"type": "text", "text": user_msg}
|
| 57 |
+
# ]
|
| 58 |
+
# })
|
| 59 |
+
# # Append assistant messages
|
| 60 |
+
# messages.append({
|
| 61 |
+
# "role": "assistant",
|
| 62 |
+
# "content": [
|
| 63 |
+
# {"type": "text", "text": assistant_msg}
|
| 64 |
+
# ]
|
| 65 |
+
# })
|
| 66 |
|
| 67 |
# Prepare the current message
|
| 68 |
content = []
|
| 69 |
user_text = ""
|
| 70 |
|
| 71 |
try:
|
| 72 |
+
content.append({
|
| 73 |
+
"role": "user",
|
| 74 |
+
"content": [
|
| 75 |
+
{"type": "text", "text": message["text"]},
|
| 76 |
+
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
"type": "image_url",
|
| 78 |
+
"image_url": {
|
| 79 |
+
"url": f"data:image/png;base64,{image_base64}",
|
| 80 |
+
},
|
| 81 |
+
},
|
| 82 |
+
],
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
# if isinstance(message, dict):
|
| 86 |
+
# # Handle text input
|
| 87 |
+
# if 'text' in message and message['text']:
|
| 88 |
+
# user_text = message['text']
|
| 89 |
+
# content.append({"type": "text", "text": user_text})
|
| 90 |
+
|
| 91 |
+
# # Handle image input
|
| 92 |
+
# if 'files' in message and len(message['files']) > 0:
|
| 93 |
+
# file_info = message['files'][0]
|
| 94 |
+
# if isinstance(file_info, dict) and 'path' in file_info:
|
| 95 |
+
# image_path = file_info['path']
|
| 96 |
+
# elif isinstance(file_info, str):
|
| 97 |
+
# image_path = file_info
|
| 98 |
+
# else:
|
| 99 |
+
# raise ValueError("Invalid file information provided.")
|
| 100 |
+
|
| 101 |
+
# # Encode the image to base64
|
| 102 |
+
# image_base64 = encode_image(image_path)
|
| 103 |
+
# content.append({
|
| 104 |
+
# "type": "image_url",
|
| 105 |
+
# "image_url": {"url": f"data:image/png;base64,{image_base64}"}
|
| 106 |
+
# })
|
| 107 |
+
# user_text += "\n[User uploaded an image]"
|
| 108 |
+
# else:
|
| 109 |
+
# # If message is a string
|
| 110 |
+
# user_text = message
|
| 111 |
+
# content.append({"type": "text", "text": user_text})
|
| 112 |
except Exception as e:
|
| 113 |
# If there's an error processing the input, append it to history and yield
|
| 114 |
error_message = f"An error occurred while processing your input: {str(e)}"
|
|
|
|
| 138 |
for chunk in stream:
|
| 139 |
# Extract the content from the API response
|
| 140 |
chunk_content = chunk.choices[0].delta.content or ""
|
| 141 |
+
print(chunk.choices[0].delta.content or "", end="", flush=True)
|
| 142 |
response += chunk_content
|
| 143 |
+
# # Update the last assistant message in history
|
| 144 |
+
# if history:
|
| 145 |
+
# history[-1][1] = response
|
| 146 |
+
# yield history
|
| 147 |
+
# else:
|
| 148 |
+
# # If history is somehow empty, append the response
|
| 149 |
+
# history.append(["", response])
|
| 150 |
+
# yield history
|
| 151 |
+
|
| 152 |
+
# if not response:
|
| 153 |
+
# # If no response was generated, notify the user
|
| 154 |
+
# history[-1][1] = "No response generated. Please try again."
|
| 155 |
+
# yield history
|
| 156 |
|
| 157 |
except Exception as e:
|
| 158 |
# Handle exceptions from the API call
|