jchen8000 commited on
Commit
c1918f6
·
verified ·
1 Parent(s): dc90582

Upload read_image.py

Browse files
Files changed (1) hide show
  1. read_image.py +81 -0
read_image.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import google.generativeai as genai
2
+ from groq import Groq
3
+ import base64
4
+ import os
5
+
6
+
7
+ # For better security practices, retrieve sensitive information like API keys from environment variables.
8
+
9
+ GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
10
+ genai.configure(api_key=GOOGLE_API_KEY)
11
+
12
+ # These codelines are just to verify if your api key is correct or not
13
+ # Use them when you clone the repo and build locally
14
+ #!curl \
15
+ #-H 'Content-Type: application/json' \
16
+ #-d '{ "prompt": { "text": "Write a very short story about a magic backpack"} }' \
17
+ #"https://generativelanguage.googleapis.com/v1beta3/models/text-bison-001:generateText?key=<enter-your-key-here>"
18
+
19
+ # Initialize genai models
20
+ # model = genai.GenerativeModel('gemini-1.5-pro')
21
+ # modelvis = genai.GenerativeModel('gemini-1.5-flash')
22
+
23
+ models = ["gemini-2.0-flash-exp",
24
+ "gemini-2.0-flash",
25
+ "gemini-1.5-flash-8b",
26
+ "gemini-1.5-flash",
27
+ "gemini-1.5-pro",
28
+ "llama-4-scout-17b-16e-instruct",
29
+ "llama-4-maverick-17b-128e-instruct"
30
+ ]
31
+
32
+
33
+ def encode_image(image_path):
34
+ with open(image_path, "rb") as image_file:
35
+ return base64.b64encode(image_file.read()).decode('utf-8')
36
+
37
+ def encode_image(image):
38
+ buffered = io.BytesIO()
39
+ image.save(buffered, format="JPEG")
40
+ return base64.b64encode(buffered.getvalue()).decode('utf-8')
41
+
42
+
43
+ def readimage(image, prompt, model_name):
44
+ base64_image = encode_image(image)
45
+ image_content = {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
46
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
47
+ try:
48
+ chat_completion = client.chat.completions.create(
49
+ messages=[
50
+ {
51
+ "role": "user",
52
+ "content": [
53
+ {"type": "text", "text": prompt}, image_content,
54
+ ],
55
+ }
56
+ ],
57
+ model=model,
58
+ )
59
+ return chat_completion.choices[0].message.content
60
+ except Exception as e:
61
+ return f"Error: {str(e)}"
62
+
63
+
64
+ # def readimage(image, prompt, model_name):
65
+ # """
66
+ # Function to handle gemini model and gemini vision model interactions.
67
+
68
+ # Parameters:
69
+ # image: the image to read.
70
+ # prompt (str): The input text.
71
+ # model_name (str): model name.
72
+ # Returns:
73
+ # The response from the model.
74
+ # """
75
+
76
+ # messages = []
77
+ # messages.append({'role': 'user', 'parts': [prompt, image]})
78
+ # model = genai.GenerativeModel(model_name)
79
+ # response = model.generate_content(messages)
80
+ # return response.text
81
+