majorSeaweed commited on
Commit
3752fd0
·
verified ·
1 Parent(s): 1eb4cbe

Upload app (1).py

Browse files
Files changed (1) hide show
  1. app (1).py +175 -0
app (1).py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
+ import base64
4
+ import json
5
+ import re
6
+ import numpy as np
7
+ from PIL import Image
8
+ import matplotlib.pyplot as plt
9
+ import matplotlib.patches as patches
10
+ import streamlit as st
11
+ from openai import OpenAI
12
+ # Set your API key and instantiate the client (make sure your OpenAI client is imported/defined)
13
+ os.environ['OPENAI_API_KEY'] = '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'
14
+ client = OpenAI() # Assumes you have an OpenAI client available
15
+
16
+ # The prompt used to instruct the model
17
+ prompt = """
18
+ You are an expert road quality analyst. You will be shown an image of a road segment. Your task is to thoroughly inspect the condition of the road surface in the image and provide a detailed evaluation.
19
+
20
+ Your output must be in the form of a JSON object with the following structure:
21
+
22
+ {
23
+ "description": "<A detailed analysis of the visible road surface condition. Describe texture, cracks, potholes, construction quality, and overall appearance.>",
24
+ "label": "<One of: 'Excellent', 'Adequate', 'Basic', 'Poor', or 'Not Constructed'>",
25
+ "score": <A numerical value from the following scale: 5, 3, 2, 1, or 0>
26
+ }
27
+
28
+ ### Use the following scoring rubric to assign both the 'label' and the 'score':
29
+
30
+ - **5 – Excellent**: The road surface is in pristine condition, with no visible damage. It may be a newly constructed or expressway-type road with smooth asphalt and no visible cracks, potholes, or any other surface imperfections. It is **ideal for fast, high-volume traffic**.
31
+
32
+ - **3 – Adequate**: The road is constructed and mostly functional. There are a few minor defects such as small cracks or potholes, but the road is still in **generally good condition**. It is suitable for use but could use maintenance to address minor issues.
33
+
34
+ - **2 – Basic**: The road shows clear signs of wear and construction defects, such as visible potholes, cracks, or surface degradation. The **road is still usable but needs repair**, and it may be difficult or uncomfortable to drive on for extended periods.
35
+
36
+ - **1 – Poor**: The road has **major issues** such as large potholes, severe cracks, or other types of significant damage that make it **hard to use**. While the road may still be passable, it poses risks to vehicles and drivers due to the level of deterioration.
37
+
38
+ - **0 – Not Constructed**: The road is **incomplete or not constructed**. This could include images of roads with severe mud, large holes, missing surface material, or roads that are **barely passable or not usable** for normal traffic. It is **not fit for use** and potentially dangerous.
39
+
40
+ ### Specific Classification Criteria:
41
+ - **0 (Not Constructed)**: Roads that are missing large sections, filled with severe mud, or have very large holes making them essentially unusable or extremely hazardous.
42
+ - **1 (Poor)**: Roads with major potholes, severe cracks, or significant surface degradation where it is barely usable. The road is **dangerous for normal traffic**.
43
+ - **2 (Basic)**: Roads with **multiple defects**, such as potholes, cracks, or surface degradation. These roads are **difficult to drive on** but are still passable.
44
+ - **3 (Adequate)**: Roads that are **mostly intact** with only a few minor defects, such as small potholes or cracks, but still in **acceptable condition** for regular use.
45
+ - **5 (Excellent)**: Pristine roads, like expressways or newly constructed highways, that are **in perfect condition** with no visible damage, cracks, or potholes.
46
+
47
+ Make sure your response contains **only the JSON output**, with no extra text or commentary.
48
+ """
49
+
50
+ # prompt = '''You are an expert road quality analyst. You will be provided with an image of a road segment. Your task is to perform a detailed visual analysis of the road surface and output your evaluation as a JSON object strictly in the format below, with no extra commentary:
51
+
52
+ # {
53
+ # "description": "<A detailed analysis of the visible road surface condition. Describe texture, cracks, potholes, construction quality, and overall appearance.>",
54
+ # "label": "<One of: 'Excellent', 'Adequate', 'Basic', 'Poor', or 'Not Constructed'>",
55
+ # "score": <A numerical value from the following scale: 5, 3, 2, 1, or 0>
56
+ # }
57
+
58
+ # Scoring Rubric:
59
+ # - **5 – Excellent**: The road surface is pristine, with no damage. It may be a newly constructed highway or expressway with smooth asphalt and no visible cracks, potholes, or imperfections. Ideal for fast, high-volume traffic.
60
+
61
+ # - **3 – Adequate**: The road is properly constructed and mostly functional. There might be a few minor defects like small cracks or potholes, but overall it is in generally good condition. Some minor maintenance could be beneficial.
62
+
63
+ # - **2 – Basic**: The road shows signs of wear and some construction defects, such as moderate cracks, potholes, or surface degradation. It remains passable but clearly needs repair. **For example, dirt roads that look flat and generally good should be classified as 'Basic' with a score of 2.**
64
+
65
+ # - **1 – Poor**: The road has major issues including large potholes, severe cracks, or significant surface degradation that make it hazardous. Although passable in some cases, it poses a risk to vehicles and drivers.
66
+
67
+ # - **0 – Not Constructed**: The road is incomplete or appears unconstructed. This includes images showing severe gaps, extensive mud, or missing sections, making the road extremely unsafe or unusable.
68
+
69
+ # Important Instructions:
70
+ # - **Consistency:** The "label" field must be exactly one of the specified text values ('Excellent', 'Adequate', 'Basic', 'Poor', or 'Not Constructed'). Do not output a numerical value as the label.
71
+ # - **Accurate Mapping:** The "score" field must correspond exactly to the assigned label based on the rubric. For instance, if you determine the road condition is "Basic", the score must be 2.
72
+ # - **Visual-Only Analysis:** Your evaluation must be based solely on what is visible in the provided image. Do not infer conditions beyond what the image shows.
73
+ # - **Output Format:** Only output a valid JSON object adhering to the above format—no additional text, explanation, or markdown formatting.
74
+
75
+ # Follow these instructions precisely to ensure accuracy and to minimize hallucinations.
76
+ # '''
77
+
78
+ # Function to create a base64 image URL from an uploaded file
79
+ def make_links_from_file(uploaded_file):
80
+ # Ensure we're reading from the start
81
+ uploaded_file.seek(0)
82
+ with Image.open(uploaded_file) as img:
83
+ img = img.convert("RGB")
84
+ if img.width > 512 or img.height > 512:
85
+ img.thumbnail((512, 512))
86
+ buffer = io.BytesIO()
87
+ img.save(buffer, format="PNG")
88
+ encoded_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
89
+ return f"data:image/png;base64,{encoded_image}"
90
+
91
+ # Function to get the response from the model
92
+ def get_response(prompt, img_url, model="gpt-4o-mini"):
93
+ if model == "gpt-4o":
94
+ print("Using gpt-4o model")
95
+ messages = [
96
+ {
97
+ "role": "user",
98
+ "content": [
99
+ { "type": "text", "text": prompt },
100
+ {
101
+ "type": "image_url",
102
+ "image_url": {"url": img_url}
103
+ },
104
+ ],
105
+ }
106
+ ]
107
+
108
+ else:
109
+ messages = [
110
+ {
111
+ "role": "user",
112
+ "content": [
113
+ {"type": "text", "text": prompt},
114
+ {"type": "image_url", "image_url": {"url": img_url}}
115
+ ]
116
+ }
117
+ ]
118
+ response = client.chat.completions.create(
119
+ model=model,
120
+ messages=messages
121
+ )
122
+ return response
123
+
124
+ # Function to clean the output (removes markdown formatting and parses JSON)
125
+ def clean_response(output):
126
+ raw_output = output.choices[0].message.content.strip()
127
+ cleaned_output = re.sub(r"^```(?:json)?\s*|\s*```$", "", raw_output.strip())
128
+ data = json.loads(cleaned_output)
129
+ return data
130
+
131
+ # Function to display the image with an overlay and description
132
+ def show_outs(img, data):
133
+ fig, ax = plt.subplots(figsize=(10, 6))
134
+ ax.imshow(img)
135
+ ax.axis('off')
136
+ label_text = f"Label: {data['label']} | Score: {data['score']}"
137
+ ax.add_patch(patches.Rectangle((0, 0), 1, 0.1, transform=ax.transAxes,
138
+ color='black', alpha=0.5))
139
+ ax.text(0.01, 0.03, label_text, transform=ax.transAxes,
140
+ fontsize=14, color='white', fontweight='bold', va='center')
141
+ plt.tight_layout()
142
+ st.pyplot(fig)
143
+ plt.close(fig)
144
+ st.write("\n📝 Road Surface Description:\n")
145
+ st.write(data.get("description", "No description provided."))
146
+
147
+ # Modified inference pipeline that works with an uploaded file
148
+ def inference_pipeline_uploaded(uploaded_file, prompt, model):
149
+ img_url = make_links_from_file(uploaded_file)
150
+ st.write("Fetching Response...")
151
+ output = get_response(prompt, img_url, model)
152
+ data = clean_response(output)
153
+ # Reset file pointer and reopen image for display
154
+ uploaded_file.seek(0)
155
+ img = Image.open(uploaded_file).convert("RGB")
156
+ if img.width > 512 or img.height > 512:
157
+ img.thumbnail((512, 512))
158
+ img_array = np.array(img)
159
+ show_outs(img_array, data)
160
+
161
+ # Main app layout
162
+ st.title("Road Quality Analysis")
163
+ st.write("Upload an image of a road segment for analysis.")
164
+
165
+ # Toggle between models using a radio button
166
+ model_choice = st.radio("Select Model", options=["GPT 40", "GPT 40 mini"])
167
+ model = "gpt-4o" if model_choice == "GPT 40" else "gpt-4o-mini"
168
+
169
+ # Image file uploader
170
+ uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
171
+
172
+ if uploaded_file is not None:
173
+ st.image(uploaded_file, caption="Uploaded Road Segment", use_column_width=True)
174
+ if st.button("Run Analysis"):
175
+ inference_pipeline_uploaded(uploaded_file, prompt, model)