capradeepgujaran commited on
Commit
e2524e7
1 Parent(s): 9427909

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -17
app.py CHANGED
@@ -5,29 +5,43 @@ from PIL import Image
5
  import io
6
  import json
7
  from groq import Groq
 
 
 
 
 
8
 
9
  # Load environment variables
10
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
 
 
 
11
 
12
  # Initialize Groq client
13
  client = Groq(api_key=GROQ_API_KEY)
14
 
15
  def encode_image(image):
16
- if isinstance(image, str): # If image is a file path
17
- with open(image, "rb") as image_file:
18
- return base64.b64encode(image_file.read()).decode('utf-8')
19
- elif isinstance(image, Image.Image): # If image is a PIL Image
20
- buffered = io.BytesIO()
21
- image.save(buffered, format="PNG")
22
- return base64.b64encode(buffered.getvalue()).decode('utf-8')
23
- else:
24
- raise ValueError("Unsupported image type")
 
 
 
 
25
 
26
  def analyze_construction_image(image, follow_up_question=""):
27
  if image is None:
 
28
  return "Error: No image uploaded", "", ""
29
 
30
  try:
 
31
  image_data_url = f"data:image/png;base64,{encode_image(image)}"
32
 
33
  messages = [
@@ -36,7 +50,7 @@ def analyze_construction_image(image, follow_up_question=""):
36
  "content": [
37
  {
38
  "type": "text",
39
- "text": "Analyze this construction site image. Identify any issues or snags, categorize them, provide a detailed description, and suggest steps to resolve them. Output the result in JSON format."
40
  },
41
  {
42
  "type": "image_url",
@@ -54,8 +68,9 @@ def analyze_construction_image(image, follow_up_question=""):
54
  "content": follow_up_question
55
  })
56
 
 
57
  completion = client.chat.completions.create(
58
- model="llama-3.2-11b-vision-preview",
59
  messages=messages,
60
  temperature=0.7,
61
  max_tokens=1000,
@@ -65,14 +80,25 @@ def analyze_construction_image(image, follow_up_question=""):
65
  stop=None
66
  )
67
 
68
- result = json.loads(completion.choices[0].message.content)
 
 
 
 
 
 
 
 
 
69
 
70
- snag_category = result.get('snag_category', 'N/A')
71
- snag_description = result.get('snag_description', 'N/A')
72
- desnag_steps = '\n'.join(result.get('desnag_steps', ['N/A']))
73
 
 
74
  return snag_category, snag_description, desnag_steps
75
  except Exception as e:
 
76
  return f"Error: {str(e)}", "", ""
77
 
78
  # Create the Gradio interface
@@ -93,10 +119,10 @@ iface = gr.Interface(
93
  ["example_image1.jpg", "What safety concerns do you see?"],
94
  ["example_image2.jpg", "Is there any visible structural damage?"]
95
  ],
96
- cache_examples=False, # Disable caching to avoid file moving issues
97
  theme="default"
98
  )
99
 
100
  # Launch the app
101
  if __name__ == "__main__":
102
- iface.launch()
 
5
  import io
6
  import json
7
  from groq import Groq
8
+ import logging
9
+
10
+ # Set up logging
11
+ logging.basicConfig(level=logging.DEBUG)
12
+ logger = logging.getLogger(__name__)
13
 
14
  # Load environment variables
15
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
16
+ if not GROQ_API_KEY:
17
+ logger.error("GROQ_API_KEY is not set in environment variables")
18
+ raise ValueError("GROQ_API_KEY is not set")
19
 
20
  # Initialize Groq client
21
  client = Groq(api_key=GROQ_API_KEY)
22
 
23
  def encode_image(image):
24
+ try:
25
+ if isinstance(image, str): # If image is a file path
26
+ with open(image, "rb") as image_file:
27
+ return base64.b64encode(image_file.read()).decode('utf-8')
28
+ elif isinstance(image, Image.Image): # If image is a PIL Image
29
+ buffered = io.BytesIO()
30
+ image.save(buffered, format="PNG")
31
+ return base64.b64encode(buffered.getvalue()).decode('utf-8')
32
+ else:
33
+ raise ValueError(f"Unsupported image type: {type(image)}")
34
+ except Exception as e:
35
+ logger.error(f"Error encoding image: {str(e)}")
36
+ raise
37
 
38
  def analyze_construction_image(image, follow_up_question=""):
39
  if image is None:
40
+ logger.warning("No image provided")
41
  return "Error: No image uploaded", "", ""
42
 
43
  try:
44
+ logger.info("Starting image analysis")
45
  image_data_url = f"data:image/png;base64,{encode_image(image)}"
46
 
47
  messages = [
 
50
  "content": [
51
  {
52
  "type": "text",
53
+ "text": "Analyze this construction site image. Identify any issues or snags, categorize them, provide a detailed description, and suggest steps to resolve them. Format your response as a JSON object with keys 'snag_category', 'snag_description', and 'desnag_steps' (as an array)."
54
  },
55
  {
56
  "type": "image_url",
 
68
  "content": follow_up_question
69
  })
70
 
71
+ logger.info("Sending request to Groq API")
72
  completion = client.chat.completions.create(
73
+ model="llama-3.2-90b-vision-preview",
74
  messages=messages,
75
  temperature=0.7,
76
  max_tokens=1000,
 
80
  stop=None
81
  )
82
 
83
+ logger.info("Received response from Groq API")
84
+ result = completion.choices[0].message.content
85
+ logger.debug(f"Raw API response: {result}")
86
+
87
+ # Try to parse the result as JSON
88
+ try:
89
+ parsed_result = json.loads(result)
90
+ except json.JSONDecodeError:
91
+ logger.error("Failed to parse API response as JSON")
92
+ return "Error: Invalid response format", "", ""
93
 
94
+ snag_category = parsed_result.get('snag_category', 'N/A')
95
+ snag_description = parsed_result.get('snag_description', 'N/A')
96
+ desnag_steps = '\n'.join(parsed_result.get('desnag_steps', ['N/A']))
97
 
98
+ logger.info("Analysis completed successfully")
99
  return snag_category, snag_description, desnag_steps
100
  except Exception as e:
101
+ logger.error(f"Error during image analysis: {str(e)}")
102
  return f"Error: {str(e)}", "", ""
103
 
104
  # Create the Gradio interface
 
119
  ["example_image1.jpg", "What safety concerns do you see?"],
120
  ["example_image2.jpg", "Is there any visible structural damage?"]
121
  ],
122
+ cache_examples=False,
123
  theme="default"
124
  )
125
 
126
  # Launch the app
127
  if __name__ == "__main__":
128
+ iface.launch(debug=True)