Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import base64
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Function to encode the image to base64
|
7 |
+
def encode_image(image_file):
|
8 |
+
image_bytes = image_file.read()
|
9 |
+
return base64.b64encode(image_bytes).decode('utf-8')
|
10 |
+
|
11 |
+
def compare_images(task_description, image_1, image_2):
|
12 |
+
api_key = "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"
|
13 |
+
headers = {
|
14 |
+
"Content-Type": "application/json",
|
15 |
+
"Authorization": f"Bearer {api_key}"
|
16 |
+
}
|
17 |
+
payload = {
|
18 |
+
"model": "gpt-4o-mini",
|
19 |
+
"messages": [
|
20 |
+
{"role": "system", "content": "You are an expert in analyzing progress in construction tasks based on image comparisons."},
|
21 |
+
{"role": "user", "content": f"Task: '{task_description}'."},
|
22 |
+
{"role": "user",
|
23 |
+
"content": [
|
24 |
+
{"type": "text",
|
25 |
+
"text": "This is the yesterday's image of task."},
|
26 |
+
{
|
27 |
+
"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_1}"}
|
28 |
+
}
|
29 |
+
]},
|
30 |
+
{
|
31 |
+
"role": "user",
|
32 |
+
"content": [
|
33 |
+
{
|
34 |
+
"type": "text",
|
35 |
+
"text": "This is today's image of task."
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_2}"}
|
39 |
+
}
|
40 |
+
]
|
41 |
+
},
|
42 |
+
{"role": "user", "content": "Now tell me is there any progress made today from yesterday in terms of task."}
|
43 |
+
],
|
44 |
+
"max_tokens": 1000
|
45 |
+
}
|
46 |
+
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
47 |
+
print(response.json())
|
48 |
+
return response.json()['choices'][0]['message']['content']
|
49 |
+
|
50 |
+
# Streamlit app interface
|
51 |
+
st.title("Construction Task Progress Analyzer")
|
52 |
+
st.write("Upload yesterday's and today's images of the task, and describe the task to analyze progress.")
|
53 |
+
|
54 |
+
task_description = st.text_input("Enter the task description:")
|
55 |
+
col1, col2, col3, col4 = st.columns(4)
|
56 |
+
with col1:
|
57 |
+
yesterday_image = st.file_uploader("Choose yesterday's image:", type=['png', 'jpg', 'jpeg'])
|
58 |
+
with col2:
|
59 |
+
if yesterday_image is not None:
|
60 |
+
st.image(yesterday_image, caption="yesterday's image", width=100)
|
61 |
+
with col3:
|
62 |
+
today_image = st.file_uploader("Choose today's image:", type=['png', 'jpg', 'jpeg'])
|
63 |
+
with col4:
|
64 |
+
if today_image is not None:
|
65 |
+
st.image(today_image, caption="today's image", width=100)
|
66 |
+
|
67 |
+
|
68 |
+
if st.button("Analyze Progress"):
|
69 |
+
if yesterday_image and today_image and task_description:
|
70 |
+
base64_image_1 = encode_image(yesterday_image)
|
71 |
+
base64_image_2 = encode_image(today_image)
|
72 |
+
result = compare_images(task_description, base64_image_1, base64_image_2)
|
73 |
+
st.write("Analysis Result:")
|
74 |
+
st.write(result)
|
75 |
+
else:
|
76 |
+
st.error("Please upload both images and provide the task description.")
|