|
import base64
|
|
import requests
|
|
import os
|
|
import logging
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="%(asctime)s - %(levelname)s - %(message)s",
|
|
handlers=[
|
|
logging.StreamHandler(),
|
|
logging.FileHandler("api_request_logs.log")
|
|
]
|
|
)
|
|
|
|
|
|
GROQ_API_KEY = "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"
|
|
if not GROQ_API_KEY:
|
|
raise ValueError("GROQ_API_KEY is not set in the .env file")
|
|
|
|
def process_image_and_get_description(image_path, model="llama-3.2-90b-vision-preview", retries=3):
|
|
"""
|
|
Process the image using the Groq API and get a description.
|
|
Retries in case of failure.
|
|
|
|
Args:
|
|
image_path (str): Path to the image.
|
|
model (str): Model to use for processing.
|
|
retries (int): Number of retries before giving up.
|
|
|
|
Returns:
|
|
str: Description of the image or an error message.
|
|
"""
|
|
encoded_image = image_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
messages = [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "Analyze the image to identify what is happening, describe the overall context, and perform OCR to extract any visible text. Additionally, specify whether the subject is a human, animal, or object, and provide a detailed description of any object the human is holding or their specific actions."},
|
|
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}}
|
|
]
|
|
}
|
|
]
|
|
|
|
for attempt in range(1, retries + 1):
|
|
try:
|
|
logging.info(f"Attempt {attempt} to process the image with Groq API.")
|
|
|
|
|
|
response = requests.post(
|
|
"https://api.groq.com/openai/v1/chat/completions",
|
|
json={
|
|
"model": model,
|
|
"messages": messages,
|
|
"max_tokens": 4096,
|
|
"stop": None,
|
|
"stream": False
|
|
},
|
|
headers={
|
|
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
"Content-Type": "application/json"
|
|
},
|
|
timeout=30
|
|
)
|
|
|
|
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
answer = result["choices"][0]["message"]["content"]
|
|
logging.info("Successfully processed the image and received a response.")
|
|
return answer
|
|
else:
|
|
logging.warning(f"Received error response: {response.status_code} - {response.text}")
|
|
except requests.RequestException as e:
|
|
logging.error(f"RequestException on attempt {attempt}: {e}")
|
|
|
|
logging.error("All attempts to process the image failed.")
|
|
return "Error: Unable to process the image after multiple attempts."
|
|
|
|
|
|
|
|
|
|
|
|
|