NEXAS commited on
Commit
ccfcb67
·
verified ·
1 Parent(s): 2b1d8e8

Update utils/doi.py

Browse files
Files changed (1) hide show
  1. utils/doi.py +97 -97
utils/doi.py CHANGED
@@ -1,97 +1,97 @@
1
- import base64
2
- import requests
3
- import os
4
- import logging
5
- from dotenv import load_dotenv
6
-
7
- # Load environment variables
8
- load_dotenv()
9
-
10
- # Configure logging
11
- logging.basicConfig(
12
- level=logging.INFO,
13
- format="%(asctime)s - %(levelname)s - %(message)s",
14
- handlers=[
15
- logging.StreamHandler(), # Log to console
16
- logging.FileHandler("api_request_logs.log") # Log to a file
17
- ]
18
- )
19
-
20
- # Get the API key from environment variable
21
- GROQ_API_KEY = "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"
22
- if not GROQ_API_KEY:
23
- raise ValueError("GROQ_API_KEY is not set in the .env file")
24
-
25
- def process_image_and_get_description(image_path, model="llama-3.2-90b-vision-preview", retries=3):
26
- """
27
- Process the image using the Groq API and get a description.
28
- Retries in case of failure.
29
-
30
- Args:
31
- image_path (str): Path to the image.
32
- model (str): Model to use for processing.
33
- retries (int): Number of retries before giving up.
34
-
35
- Returns:
36
- str: Description of the image or an error message.
37
- """
38
- encoded_image = image_path
39
- # # Encode the image to base64
40
- # try:
41
- # with open(image_path, "rb") as image_file:
42
- # encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
43
- # logging.info("Successfully encoded the image to base64.")
44
- # except Exception as e:
45
- # logging.error(f"Error encoding the image: {e}")
46
- # return "Error encoding the image."
47
-
48
- # Prepare the message payload
49
- messages = [
50
- {
51
- "role": "user",
52
- "content": [
53
- {"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."},
54
- {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}}
55
- ]
56
- }
57
- ]
58
-
59
- for attempt in range(1, retries + 1):
60
- try:
61
- logging.info(f"Attempt {attempt} to process the image with Groq API.")
62
-
63
- # Make the API request
64
- response = requests.post(
65
- "https://api.groq.com/openai/v1/chat/completions",
66
- json={
67
- "model": model,
68
- "messages": messages,
69
- "max_tokens": 4096,
70
- "stop": None,
71
- "stream": False
72
- },
73
- headers={
74
- "Authorization": f"Bearer {GROQ_API_KEY}",
75
- "Content-Type": "application/json"
76
- },
77
- timeout=30
78
- )
79
-
80
- # Process the response
81
- if response.status_code == 200:
82
- result = response.json()
83
- answer = result["choices"][0]["message"]["content"]
84
- logging.info("Successfully processed the image and received a response.")
85
- return answer
86
- else:
87
- logging.warning(f"Received error response: {response.status_code} - {response.text}")
88
- except requests.RequestException as e:
89
- logging.error(f"RequestException on attempt {attempt}: {e}")
90
-
91
- logging.error("All attempts to process the image failed.")
92
- return "Error: Unable to process the image after multiple attempts."
93
-
94
- # # Example usage
95
- # image_path = r"/content/temp.jpeg"
96
- # description = process_image_and_get_description(image_path)
97
- # print(description)
 
1
+ import base64
2
+ import requests
3
+ import os
4
+ import logging
5
+ from dotenv import load_dotenv
6
+
7
+ # Load environment variables
8
+ load_dotenv()
9
+
10
+ # Configure logging
11
+ logging.basicConfig(
12
+ level=logging.INFO,
13
+ format="%(asctime)s - %(levelname)s - %(message)s",
14
+ handlers=[
15
+ logging.StreamHandler(), # Log to console
16
+ logging.FileHandler("api_request_logs.log") # Log to a file
17
+ ]
18
+ )
19
+
20
+ # Get the API key from environment variable
21
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY2")
22
+ if not GROQ_API_KEY:
23
+ raise ValueError("GROQ_API_KEY is not set in the .env file")
24
+
25
+ def process_image_and_get_description(image_path, model="llama-3.2-90b-vision-preview", retries=3):
26
+ """
27
+ Process the image using the Groq API and get a description.
28
+ Retries in case of failure.
29
+
30
+ Args:
31
+ image_path (str): Path to the image.
32
+ model (str): Model to use for processing.
33
+ retries (int): Number of retries before giving up.
34
+
35
+ Returns:
36
+ str: Description of the image or an error message.
37
+ """
38
+ encoded_image = image_path
39
+ # # Encode the image to base64
40
+ # try:
41
+ # with open(image_path, "rb") as image_file:
42
+ # encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
43
+ # logging.info("Successfully encoded the image to base64.")
44
+ # except Exception as e:
45
+ # logging.error(f"Error encoding the image: {e}")
46
+ # return "Error encoding the image."
47
+
48
+ # Prepare the message payload
49
+ messages = [
50
+ {
51
+ "role": "user",
52
+ "content": [
53
+ {"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."},
54
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}}
55
+ ]
56
+ }
57
+ ]
58
+
59
+ for attempt in range(1, retries + 1):
60
+ try:
61
+ logging.info(f"Attempt {attempt} to process the image with Groq API.")
62
+
63
+ # Make the API request
64
+ response = requests.post(
65
+ "https://api.groq.com/openai/v1/chat/completions",
66
+ json={
67
+ "model": model,
68
+ "messages": messages,
69
+ "max_tokens": 4096,
70
+ "stop": None,
71
+ "stream": False
72
+ },
73
+ headers={
74
+ "Authorization": f"Bearer {GROQ_API_KEY}",
75
+ "Content-Type": "application/json"
76
+ },
77
+ timeout=30
78
+ )
79
+
80
+ # Process the response
81
+ if response.status_code == 200:
82
+ result = response.json()
83
+ answer = result["choices"][0]["message"]["content"]
84
+ logging.info("Successfully processed the image and received a response.")
85
+ return answer
86
+ else:
87
+ logging.warning(f"Received error response: {response.status_code} - {response.text}")
88
+ except requests.RequestException as e:
89
+ logging.error(f"RequestException on attempt {attempt}: {e}")
90
+
91
+ logging.error("All attempts to process the image failed.")
92
+ return "Error: Unable to process the image after multiple attempts."
93
+
94
+ # # Example usage
95
+ # image_path = r"/content/temp.jpeg"
96
+ # description = process_image_and_get_description(image_path)
97
+ # print(description)