Spaces:
Sleeping
Sleeping
import streamlit as st | |
import requests | |
import base64 | |
import os | |
# Function to encode the image to base64 | |
def encode_image(image_file): | |
image_bytes = image_file.read() | |
return base64.b64encode(image_bytes).decode('utf-8') | |
def compare_images(task_description, image_1, image_2): | |
api_key = "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" | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {api_key}" | |
} | |
payload = { | |
"model": "gpt-4o-mini", | |
"messages": [ | |
{"role": "system", "content": "You are an expert in analyzing progress in construction tasks based on image comparisons."}, | |
{"role": "user", "content": f"Task: '{task_description}'."}, | |
{"role": "user", | |
"content": [ | |
{"type": "text", | |
"text": "This is the yesterday's image of task."}, | |
{ | |
"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_1}"} | |
} | |
]}, | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "text", | |
"text": "This is today's image of task." | |
}, | |
{ | |
"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_2}"} | |
} | |
] | |
}, | |
{"role": "user", "content": "Now tell me is there any progress made today from yesterday in terms of task."} | |
], | |
"max_tokens": 1000 | |
} | |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload) | |
print(response.json()) | |
return response.json()['choices'][0]['message']['content'] | |
# Streamlit app interface | |
st.title("Construction Task Progress Analyzer") | |
st.write("Upload yesterday's and today's images of the task, and describe the task to analyze progress.") | |
task_description = st.text_input("Enter the task description:") | |
col1, col2, col3, col4 = st.columns(4) | |
with col1: | |
yesterday_image = st.file_uploader("Choose yesterday's image:", type=['png', 'jpg', 'jpeg']) | |
with col2: | |
if yesterday_image is not None: | |
st.image(yesterday_image, caption="yesterday's image", width=100) | |
with col3: | |
today_image = st.file_uploader("Choose today's image:", type=['png', 'jpg', 'jpeg']) | |
with col4: | |
if today_image is not None: | |
st.image(today_image, caption="today's image", width=100) | |
if st.button("Analyze Progress"): | |
if yesterday_image and today_image and task_description: | |
base64_image_1 = encode_image(yesterday_image) | |
base64_image_2 = encode_image(today_image) | |
result = compare_images(task_description, base64_image_1, base64_image_2) | |
st.write("Analysis Result:") | |
st.write(result) | |
else: | |
st.error("Please upload both images and provide the task description.") | |