Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,109 +1,31 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 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 |
-
# 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|