capradeepgujaran
commited on
Commit
•
85d2f78
1
Parent(s):
2811ee2
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import base64
|
|
3 |
import gradio as gr
|
4 |
from PIL import Image
|
5 |
import io
|
|
|
6 |
from groq import Groq
|
7 |
|
8 |
# Load environment variables
|
@@ -11,58 +12,58 @@ GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
|
11 |
# Initialize Groq client
|
12 |
client = Groq(api_key=GROQ_API_KEY)
|
13 |
|
14 |
-
def
|
|
|
|
|
|
|
|
|
|
|
15 |
if image is None:
|
16 |
return "Error: No image uploaded", "", ""
|
17 |
|
18 |
try:
|
19 |
-
|
20 |
-
buffered = io.BytesIO()
|
21 |
-
image.save(buffered, format="PNG")
|
22 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
23 |
-
image_data_url = f"data:image/png;base64,{img_str}"
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
"type": "image_url",
|
38 |
-
"image_url": {
|
39 |
-
"url": image_data_url
|
40 |
-
}
|
41 |
}
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
temperature=0.7,
|
46 |
-
max_tokens=
|
47 |
top_p=1,
|
48 |
stream=False,
|
|
|
49 |
stop=None
|
50 |
)
|
51 |
|
52 |
-
result = completion.choices[0].message.content
|
53 |
-
|
54 |
-
# Parse the result
|
55 |
-
snag_category = "N/A"
|
56 |
-
snag_description = "N/A"
|
57 |
-
desnag_steps = "N/A"
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
elif line.startswith("Snag Description:"):
|
63 |
-
snag_description = line.split(":", 1)[1].strip()
|
64 |
-
elif line.startswith("Steps to Desnag:"):
|
65 |
-
desnag_steps = "\n".join(result.split("Steps to Desnag:")[1].strip().split("\n"))
|
66 |
|
67 |
return snag_category, snag_description, desnag_steps
|
68 |
except Exception as e:
|
@@ -71,17 +72,20 @@ def analyze_construction_image(image):
|
|
71 |
# Create the Gradio interface
|
72 |
iface = gr.Interface(
|
73 |
fn=analyze_construction_image,
|
74 |
-
inputs=
|
|
|
|
|
|
|
75 |
outputs=[
|
76 |
gr.Textbox(label="Snag Category"),
|
77 |
gr.Textbox(label="Snag Description"),
|
78 |
gr.Textbox(label="Steps to Desnag")
|
79 |
],
|
80 |
-
title="Construction Image Analyzer (Llama 3.2 Vision via Groq)",
|
81 |
-
description="Upload a construction site image to identify issues and get desnag steps using Llama 3.2 Vision technology through Groq API.",
|
82 |
examples=[
|
83 |
-
["example_image1.jpg"],
|
84 |
-
["example_image2.jpg"]
|
85 |
],
|
86 |
cache_examples=True,
|
87 |
theme="default"
|
|
|
3 |
import gradio as gr
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
+
import json
|
7 |
from groq import Groq
|
8 |
|
9 |
# Load environment variables
|
|
|
12 |
# Initialize Groq client
|
13 |
client = Groq(api_key=GROQ_API_KEY)
|
14 |
|
15 |
+
def encode_image(image):
|
16 |
+
buffered = io.BytesIO()
|
17 |
+
image.save(buffered, format="PNG")
|
18 |
+
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
19 |
+
|
20 |
+
def analyze_construction_image(image, follow_up_question=""):
|
21 |
if image is None:
|
22 |
return "Error: No image uploaded", "", ""
|
23 |
|
24 |
try:
|
25 |
+
image_data_url = f"data:image/png;base64,{encode_image(image)}"
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
messages = [
|
28 |
+
{
|
29 |
+
"role": "user",
|
30 |
+
"content": [
|
31 |
+
{
|
32 |
+
"type": "text",
|
33 |
+
"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."
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"type": "image_url",
|
37 |
+
"image_url": {
|
38 |
+
"url": image_data_url
|
|
|
|
|
|
|
|
|
39 |
}
|
40 |
+
}
|
41 |
+
]
|
42 |
+
}
|
43 |
+
]
|
44 |
+
|
45 |
+
if follow_up_question:
|
46 |
+
messages.append({
|
47 |
+
"role": "user",
|
48 |
+
"content": follow_up_question
|
49 |
+
})
|
50 |
+
|
51 |
+
completion = client.chat.completions.create(
|
52 |
+
model="llama-3.2-90b-vision-preview",
|
53 |
+
messages=messages,
|
54 |
temperature=0.7,
|
55 |
+
max_tokens=1000,
|
56 |
top_p=1,
|
57 |
stream=False,
|
58 |
+
response_format={"type": "json_object"},
|
59 |
stop=None
|
60 |
)
|
61 |
|
62 |
+
result = json.loads(completion.choices[0].message.content)
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
snag_category = result.get('snag_category', 'N/A')
|
65 |
+
snag_description = result.get('snag_description', 'N/A')
|
66 |
+
desnag_steps = '\n'.join(result.get('desnag_steps', ['N/A']))
|
|
|
|
|
|
|
|
|
67 |
|
68 |
return snag_category, snag_description, desnag_steps
|
69 |
except Exception as e:
|
|
|
72 |
# Create the Gradio interface
|
73 |
iface = gr.Interface(
|
74 |
fn=analyze_construction_image,
|
75 |
+
inputs=[
|
76 |
+
gr.Image(type="pil", label="Upload Construction Image"),
|
77 |
+
gr.Textbox(label="Follow-up Question (Optional)")
|
78 |
+
],
|
79 |
outputs=[
|
80 |
gr.Textbox(label="Snag Category"),
|
81 |
gr.Textbox(label="Snag Description"),
|
82 |
gr.Textbox(label="Steps to Desnag")
|
83 |
],
|
84 |
+
title="Construction Image Analyzer (Llama 3.2 90B Vision via Groq)",
|
85 |
+
description="Upload a construction site image to identify issues and get desnag steps using Llama 3.2 90B Vision technology through Groq API. You can also ask follow-up questions about the image.",
|
86 |
examples=[
|
87 |
+
["example_image1.jpg", "What safety concerns do you see?"],
|
88 |
+
["example_image2.jpg", "Is there any visible structural damage?"]
|
89 |
],
|
90 |
cache_examples=True,
|
91 |
theme="default"
|