capradeepgujaran commited on
Commit
c8ee59e
·
verified ·
1 Parent(s): 6568b76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -42
app.py CHANGED
@@ -1,17 +1,15 @@
1
  import os
2
  import base64
3
- import requests
4
  import gradio as gr
5
  from PIL import Image
6
  import io
 
7
 
8
  # Load environment variables
9
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
10
- GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
11
 
12
- def encode_image(image_path):
13
- with open(image_path, "rb") as image_file:
14
- return base64.b64encode(image_file.read()).decode('utf-8')
15
 
16
  def analyze_construction_image(image):
17
  if image is None:
@@ -22,44 +20,40 @@ def analyze_construction_image(image):
22
  buffered = io.BytesIO()
23
  image.save(buffered, format="PNG")
24
  img_str = base64.b64encode(buffered.getvalue()).decode()
 
25
 
26
  # Prepare the message for Groq API
27
- messages = [
28
- {
29
- "role": "system",
30
- "content": "You are an AI assistant specialized in analyzing construction site images. Identify issues, categorize them, and provide steps to resolve them."
31
- },
32
- {
33
- "role": "user",
34
- "content": [
35
- {
36
- "type": "text",
37
- "text": "Analyze this construction image. Identify the snag category, provide a detailed snag description, and list steps to desnag."
38
- },
39
- {
40
- "type": "image_url",
41
- "image_url": f"data:image/png;base64,{img_str}"
42
- }
43
- ]
44
- }
45
- ]
 
 
 
 
 
 
 
 
 
 
46
 
47
- # Make API request to Groq
48
- headers = {
49
- "Authorization": f"Bearer {GROQ_API_KEY}",
50
- "Content-Type": "application/json"
51
- }
52
- data = {
53
- "model": "llama3-2-vision-90b", # Adjust model name if necessary
54
- "messages": messages,
55
- "max_tokens": 300,
56
- "temperature": 0.7
57
- }
58
-
59
- response = requests.post(GROQ_API_URL, headers=headers, json=data)
60
- response.raise_for_status()
61
-
62
- result = response.json()["choices"][0]["message"]["content"]
63
 
64
  # Parse the result
65
  lines = result.split('\n')
@@ -80,8 +74,8 @@ iface = gr.Interface(
80
  gr.Textbox(label="Snag Description"),
81
  gr.Textbox(label="Steps to Desnag")
82
  ],
83
- title="Construction Image Analyzer (Llama 3.2-Vision via Groq)",
84
- description="Upload a construction site image to identify issues and get desnag steps using Llama 3.2-Vision technology through Groq API.",
85
  examples=[
86
  ["example_image1.jpg"],
87
  ["example_image2.jpg"]
 
1
  import os
2
  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
9
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
 
10
 
11
+ # Initialize Groq client
12
+ client = Groq(api_key=GROQ_API_KEY)
 
13
 
14
  def analyze_construction_image(image):
15
  if image is None:
 
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
  # Prepare the message for Groq API
26
+ completion = client.chat.completions.create(
27
+ model="llama-3.2-11b-vision-preview",
28
+ messages=[
29
+ {
30
+ "role": "system",
31
+ "content": "You are an AI assistant specialized in analyzing construction site images. Identify issues, categorize them, and provide steps to resolve them."
32
+ },
33
+ {
34
+ "role": "user",
35
+ "content": [
36
+ {
37
+ "type": "text",
38
+ "text": "Analyze this construction image. Identify the snag category, provide a detailed snag description, and list steps to desnag."
39
+ },
40
+ {
41
+ "type": "image_url",
42
+ "image_url": {
43
+ "url": image_data_url
44
+ }
45
+ }
46
+ ]
47
+ }
48
+ ],
49
+ temperature=0.7,
50
+ max_tokens=300,
51
+ top_p=1,
52
+ stream=False,
53
+ stop=None
54
+ )
55
 
56
+ result = completion.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  # Parse the result
59
  lines = result.split('\n')
 
74
  gr.Textbox(label="Snag Description"),
75
  gr.Textbox(label="Steps to Desnag")
76
  ],
77
+ title="Construction Image Analyzer (Llama 3.2 Vision via Groq)",
78
+ description="Upload a construction site image to identify issues and get desnag steps using Llama 3.2 Vision technology through Groq API.",
79
  examples=[
80
  ["example_image1.jpg"],
81
  ["example_image2.jpg"]