Keshav-rejoice commited on
Commit
b33ccab
·
verified ·
1 Parent(s): 471f9d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -38
app.py CHANGED
@@ -7,6 +7,8 @@ from tensorflow.keras.models import load_model
7
  from keras.preprocessing.image import img_to_array
8
  from keras.applications.inception_v3 import preprocess_input
9
  import os
 
 
10
 
11
  openai.api_key = os.getenv('OPENAI_API_KEY')
12
 
@@ -18,14 +20,34 @@ class_labels = [
18
  "Peeling",
19
  ]
20
 
21
- def encode_image(image_path):
22
- with open(image_path, "rb") as image_file:
23
- return base64.b64encode(image_file.read()).decode("utf-8")
24
-
25
  @st.cache_resource
26
  def load_trained_model():
27
  return load_model('my_new_model12.h5')
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  loaded_model = load_trained_model()
30
 
31
  st.title("Wall Defect Classification and AI Analysis")
@@ -34,46 +56,36 @@ st.write("Upload an image to classify wall defects and generate AI-based descrip
34
  uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
35
 
36
  if uploaded_file is not None:
 
 
 
37
  # Display the uploaded image
38
- st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
39
-
40
- # Read and preprocess the input image
41
- file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
42
- input_img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
43
  input_img_resized = cv2.resize(input_img, dsize=(256,256), interpolation=cv2.INTER_CUBIC)
44
-
45
  x = img_to_array(input_img_resized)
46
  x = np.expand_dims(x, axis=0)
47
  x = preprocess_input(x)
48
-
49
  preds = loaded_model.predict(x)
50
-
51
- # Get the index of the class with the maximum probability
52
  class_index = np.argmax(preds[0])
53
-
54
- # The corresponding maximum probability
55
  max_probability = preds[0][class_index]
56
-
57
- # Get the class name for the predicted index
58
  class_name = class_labels[class_index]
59
 
60
- # Prepare the results text
61
  results_text = f"{class_name} (Class {class_index}): Probability {max_probability:.2f}\n"
62
-
63
- # Display classification results in a text box
64
  st.text_area("Classification Results:", value=results_text, height=200)
65
 
66
- # Encode the uploaded image as Base64
67
- base64_image = base64.b64encode(file_bytes).decode("utf-8")
68
-
69
- # If probability < 0.59, show a warning and skip AI analysis
70
  if max_probability < 0.59:
71
  st.warning(
72
  "The confidence for this prediction is below 59%. "
73
  "Please do a manual review."
74
  )
75
  else:
76
- # Generate AI-based descriptions using OpenAI API
 
 
77
  defects_string = class_name
78
  ai_prompt = (
79
  f"Our trained model predicts the following defect: {defects_string}. "
@@ -81,31 +93,30 @@ if uploaded_file is not None:
81
  f"for this defect? The output format should be:\n"
82
  f"Category ID: <Category_ID>\n"
83
  f"Title: <Title>\n"
84
- f"Description: <description>"
85
- # f"Please generate description in 150 words"
86
  )
87
-
88
  st.write("Analyzing image with AI...")
89
  try:
90
  response = openai.ChatCompletion.create(
91
- model="gpt-4o",
92
  messages=[
93
  {
94
  "role": "user",
95
- "content": ai_prompt
96
- },
97
- {
98
- # Some OpenAI endpoints may not support directly passing an image in a single message like this.
99
- # If necessary, provide a direct link or your own approach to referencing the image.
100
- "role": "user",
101
- "content": f"Base64 Image:\n{base64_image}"
 
 
102
  }
103
  ],
104
  max_tokens=300,
105
  )
106
- # Extract AI-generated descriptions
107
  ai_description = response.choices[0].message.content
108
  st.text_area("AI-Generated Description:", value=ai_description, height=200)
109
-
110
  except Exception as e:
111
  st.error(f"An error occurred while generating AI-based descriptions: {str(e)}")
 
7
  from keras.preprocessing.image import img_to_array
8
  from keras.applications.inception_v3 import preprocess_input
9
  import os
10
+ from PIL import Image
11
+ import io
12
 
13
  openai.api_key = os.getenv('OPENAI_API_KEY')
14
 
 
20
  "Peeling",
21
  ]
22
 
 
 
 
 
23
  @st.cache_resource
24
  def load_trained_model():
25
  return load_model('my_new_model12.h5')
26
 
27
+ def compress_image(image_bytes, max_size_kb=500):
28
+ # Open the image
29
+ img = Image.open(io.BytesIO(image_bytes))
30
+
31
+ # Initialize quality
32
+ quality = 95
33
+ output_bytes = io.BytesIO()
34
+
35
+ # Compress until size is under max_size_kb
36
+ while True:
37
+ output_bytes.seek(0)
38
+ output_bytes.truncate()
39
+ img.save(output_bytes, format='JPEG', quality=quality)
40
+ if len(output_bytes.getvalue()) <= max_size_kb * 1024 or quality <= 5:
41
+ break
42
+ quality -= 5
43
+
44
+ return output_bytes.getvalue()
45
+
46
+ def process_image_for_openai(image_bytes):
47
+ # Compress image to ensure it fits within token limits
48
+ compressed_image = compress_image(image_bytes)
49
+ return base64.b64encode(compressed_image).decode('utf-8')
50
+
51
  loaded_model = load_trained_model()
52
 
53
  st.title("Wall Defect Classification and AI Analysis")
 
56
  uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
57
 
58
  if uploaded_file is not None:
59
+ # Read file bytes once
60
+ file_bytes = uploaded_file.getvalue()
61
+
62
  # Display the uploaded image
63
+ st.image(file_bytes, caption="Uploaded Image", use_column_width=True)
64
+
65
+ # Process for model prediction
66
+ input_img = cv2.imdecode(np.frombuffer(file_bytes, np.uint8), cv2.IMREAD_COLOR)
 
67
  input_img_resized = cv2.resize(input_img, dsize=(256,256), interpolation=cv2.INTER_CUBIC)
 
68
  x = img_to_array(input_img_resized)
69
  x = np.expand_dims(x, axis=0)
70
  x = preprocess_input(x)
71
+
72
  preds = loaded_model.predict(x)
 
 
73
  class_index = np.argmax(preds[0])
 
 
74
  max_probability = preds[0][class_index]
 
 
75
  class_name = class_labels[class_index]
76
 
 
77
  results_text = f"{class_name} (Class {class_index}): Probability {max_probability:.2f}\n"
 
 
78
  st.text_area("Classification Results:", value=results_text, height=200)
79
 
 
 
 
 
80
  if max_probability < 0.59:
81
  st.warning(
82
  "The confidence for this prediction is below 59%. "
83
  "Please do a manual review."
84
  )
85
  else:
86
+ # Compress and encode image for OpenAI
87
+ compressed_base64 = process_image_for_openai(file_bytes)
88
+
89
  defects_string = class_name
90
  ai_prompt = (
91
  f"Our trained model predicts the following defect: {defects_string}. "
 
93
  f"for this defect? The output format should be:\n"
94
  f"Category ID: <Category_ID>\n"
95
  f"Title: <Title>\n"
96
+ f"Description: <description in 100 words or less>"
 
97
  )
98
+
99
  st.write("Analyzing image with AI...")
100
  try:
101
  response = openai.ChatCompletion.create(
102
+ model="gpt-4-vision-preview", # Using vision model instead of gpt-4
103
  messages=[
104
  {
105
  "role": "user",
106
+ "content": [
107
+ {"type": "text", "text": ai_prompt},
108
+ {
109
+ "type": "image_url",
110
+ "image_url": {
111
+ "url": f"data:image/jpeg;base64,{compressed_base64}"
112
+ }
113
+ }
114
+ ]
115
  }
116
  ],
117
  max_tokens=300,
118
  )
 
119
  ai_description = response.choices[0].message.content
120
  st.text_area("AI-Generated Description:", value=ai_description, height=200)
 
121
  except Exception as e:
122
  st.error(f"An error occurred while generating AI-based descriptions: {str(e)}")