generated from streamlit/chatbot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_model_utils.py
58 lines (49 loc) · 2.1 KB
/
base_model_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import streamlit as st
import base64
import re
def call_chat_model(client, messages):
return client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
temperature=1,
max_tokens=4096
)
def call_image_model(client, file):
# Read the file-like object (uploaded_file)
image_data = file.read()
# Encode the file data in base64
base64_image = base64.b64encode(image_data).decode('utf-8')
# Create API request
response = client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": f"""You are given an image of the user's food.
This is their nutrition plan: <nutrition_plan>{st.session_state.nutrition_tracker}</nutrition_plan).
Figure out what kind of food is in the image (and its nutrition facts) and adjust the user's nutrition plan to include the food items in the image.
Respond back to the user using <message></message> tags briefly explaining to them how you changed their nutrition plan based on the image they uploaded.
Also provide the updated nutrition plan using <nutrition_plan></nutrition_plan> tags.""",
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
},
}
],
}
],
max_tokens=4096,
)
# Parse the response content
content = response.choices[0].message.content
# Use regex to extract tags
message_pattern = r"<message>(.*?)</message>"
nutrition_pattern = r"<nutrition_plan>(.*?)</nutrition_plan>"
message = re.findall(message_pattern, content, re.DOTALL)
nutrition = re.findall(nutrition_pattern, content, re.DOTALL)
return message[0] if message else "", nutrition[0] if nutrition else ""