Spaces:
Sleeping
Sleeping
File size: 2,998 Bytes
67f1b47 |
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
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.")
|