Yanmife commited on
Commit
f0ba130
·
verified ·
1 Parent(s): 88e38f4

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +31 -109
main.py CHANGED
@@ -1,109 +1,31 @@
1
- import os
2
- import sys
3
- import json
4
- from dotenv import load_dotenv
5
- import base64
6
- # from PIL import Image
7
- from groq import Groq
8
- # import requests
9
-
10
- load_dotenv()
11
- groq_api_key = os.getenv("GROQ_API_KEY")
12
-
13
- # def get_image():
14
- # """
15
- # Get the image from the specified path.
16
- # :return: Image object.
17
- # """
18
- # image_path = "image/boil_1.jpeg"
19
- # if not os.path.exists(image_path):
20
- # print(f"Image file {image_path} does not exist.")
21
- # sys.exit(1)
22
-
23
- # image = Image.open(image_path)
24
- # return image
25
-
26
- # def get_image_path():
27
- # """
28
- # Get the image path from the specified environment variable.
29
- # :return: Image path.
30
- # """
31
- # image_path = os.getenv("IMAGE_PATH")
32
- # if not image_path:
33
- # print("IMAGE_PATH environment variable is not set.")
34
- # sys.exit(1)
35
-
36
- # if not os.path.exists(image_path):
37
- # print(f"Image file {image_path} does not exist.")
38
- # sys.exit(1)
39
-
40
- # return image_path
41
-
42
-
43
- image_path = "image/boil_1.jpeg"
44
- if not os.path.exists(image_path):
45
- print(f"Image file {image_path} does not exist.")
46
- sys.exit(1)
47
-
48
- model = "meta-llama/llama-4-scout-17b-16e-instruct"
49
- query = "What is the diagnosis of this image?"
50
- # query = "What is the diagnosis of this image? Please provide a detailed explanation of the findings and any relevant differential diagnoses."
51
-
52
-
53
- def encode_image_to_base64(image_path):
54
- """
55
- Encode an image to base64 format.
56
- :param image_path: Path to the image file.
57
- :return: Base64 encoded string of the image.
58
- """
59
- with open(image_path, "rb") as image_file:
60
- encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
61
- return encoded_string
62
-
63
-
64
- def analyze_image_with_query(query, model, encoded_image):
65
- client=Groq()
66
- messages=[
67
- {
68
- "role": "user",
69
- "content": [
70
- {
71
- "type": "text",
72
- "text": query
73
- },
74
- {
75
- "type": "image_url",
76
- "image_url": {
77
- "url": f"data:image/jpeg;base64,{encoded_image}",
78
- },
79
- },
80
- ],
81
- }]
82
- chat_completion=client.chat.completions.create(
83
- messages=messages,
84
- model=model
85
- )
86
-
87
- return chat_completion.choices[0].message.content
88
-
89
- # def main():
90
- # """
91
- # Main function to run the image analysis.
92
- # """
93
- # # Get the image path from the environment variable
94
- # # image_path = get_image_path()
95
-
96
- # # Get the image
97
- # # image = get_image()
98
-
99
- # # Encode the image to base64
100
- # encoded_image = encode_image_to_base64(image_path)
101
-
102
- # # Analyze the image with the query
103
- # response = analyze_image_with_query(query, model, encoded_image)
104
-
105
- # # Print the response
106
- # print(response)
107
-
108
- # if __name__ == "__main__":
109
- # main()
 
1
+
2
+ import base64
3
+ from groq import Groq
4
+
5
+ model = "meta-llama/llama-4-scout-17b-16e-instruct"
6
+ query = "What is the diagnosis of this image?"
7
+ # query = "What is the diagnosis of this image? Please provide a detailed explanation of the findings and any relevant differential diagnoses."
8
+
9
+ def encode_image_to_base64(image_path):
10
+ with open(image_path, "rb") as image_file:
11
+ return base64.b64encode(image_file.read()).decode("utf-8")
12
+
13
+ def analyze_image_with_query(query, model, encoded_image):
14
+ client = Groq()
15
+ messages = [
16
+ {
17
+ "role": "user",
18
+ "content": [
19
+ {"type": "text", "text": query},
20
+ {
21
+ "type": "image_url",
22
+ "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}
23
+ },
24
+ ],
25
+ }
26
+ ]
27
+ chat_completion = client.chat.completions.create(
28
+ messages=messages,
29
+ model=model
30
+ )
31
+ return chat_completion.choices[0].message.content