Spaces:
Sleeping
Sleeping
File size: 3,549 Bytes
98bf2c9 cca62d0 d119163 98bf2c9 d119163 98bf2c9 d119163 98bf2c9 d119163 98bf2c9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
from groq import Groq
import base64
from PIL import Image
import io
from .config import Config
class GroqAnalyzer:
def __init__(self):
Config.validate()
self.client = Groq(api_key=Config.GROQ_API_KEY)
self.model = "meta-llama/llama-4-maverick-17b-128e-instruct"
def analyze_damage(self, image_path):
try:
with open(image_path, "rb") as image_file:
base64_image = base64.b64encode(image_file.read()).decode()
system_message = """You are a professional car damage assessment expert. Your task is to analyze car images and provide structured, consistent damage reports.
Please look the direction of car image to respond correctly and do the analysis based on the visible car parts and damage.
Please follow these exact guidelines:
1. Use only these severity levels: High, Medium, Low
2. For status, use these terms: Damaged, Intact, Partially Visible, Potentially damaged
3. For actions, use these categories: Replacement/Repair, Inspection, Inspection/Repair
4. Use your knowledge to identify and assess all visible and relevant car parts in the image.
"""
analysis_template = """Analyze the car image and fill out the following table for all visible and relevant car parts (not limited to the example list below):
Component | Status | Severity | Action Needed
Example components: Front Bumper, Rear Bumper, Hood, Grille, Headlights, Fenders, Doors, Quarter Panels, Trunk, Roof, etc.
After the table, write a summary that ONLY references the same components and findings as in the table above. Do not mention any components or damage not present in the table.
[SUMMARY]
Start with "The image shows..." and describe:
1. Primary damage location and severity
2. Key components affected
3. Required immediate actions
4. Additional inspection recommendations
Keep the format consistent and use only the predefined terms for severity, status, and actions."""
completion = self.client.chat.completions.create(
model=self.model,
messages=[
{
"role": "system",
"content": system_message
},
{
"role": "user",
"content": [
{
"type": "text",
"text": analysis_template
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
temperature=0.3, # Lower temperature for more consistent outputs
max_completion_tokens=1024,
top_p=0.9, # Slightly lower top_p for more focused outputs
stream=True,
stop=None
)
# Handle streaming response
full_response = ""
for chunk in completion:
if chunk.choices[0].delta.content:
full_response += chunk.choices[0].delta.content
return full_response
except Exception as e:
print(f"Error in Groq analysis: {e}")
return f"Error analyzing image: {str(e)}" |