Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,10 +7,19 @@ from dotenv import load_dotenv
|
|
7 |
import google.generativeai as genai
|
8 |
import os
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
def gemini_response_vision(input_texts, image):
|
16 |
try:
|
@@ -59,8 +68,26 @@ def shot(image, labels_text, model_name, hypothesis_template_prefix, hypothesis_
|
|
59 |
domains = [domain.strip(" ") for domain in domains_text.strip(" ").split(",")]
|
60 |
else:
|
61 |
#img = Image.open(image)
|
62 |
-
input_text = "Please describe the image from six dimensions, including weather (clear, sandstorm, foggy, rainy, snowy), angle (front, left, top), time (daytime, night), occlusion (unoccluded, lightly-occluded, partially-occluded, moderately-occluded, heavily-occluded), season (spring-summer, autumn, winter). Each dimension should be described in no more than 4 words and should match the image content. Please try to output from the options in the previous brackets. If there is no suitable result, output N/A."# Please also output a probability of your inference."# If there is no information in a certain dimension, you can directly output no information.
|
63 |
-
domains = gemini_response_vision(input_texts=input_text, image=image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
print(domains)
|
65 |
|
66 |
hypothesis_template = hypothesis_template_prefix + ' ' + hypothesis_template_suffix.format(*domains)
|
|
|
7 |
import google.generativeai as genai
|
8 |
import os
|
9 |
|
10 |
+
from openai import OpenAI
|
11 |
+
client = OpenAI(api_key="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")
|
12 |
+
import base64
|
13 |
+
|
14 |
+
# Open the image file and encode it as a base64 string
|
15 |
+
def encode_image(image_path):
|
16 |
+
with open(image_path, "rb") as image_file:
|
17 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
18 |
+
|
19 |
+
# load_dotenv()
|
20 |
+
# GOOGLE_API_KEY = os.getenv("GOOGLE_API")
|
21 |
+
# genai.configure(api_key=GOOGLE_API_KEY)
|
22 |
+
# model_vision = genai.GenerativeModel('gemini-pro-vision')
|
23 |
|
24 |
def gemini_response_vision(input_texts, image):
|
25 |
try:
|
|
|
68 |
domains = [domain.strip(" ") for domain in domains_text.strip(" ").split(",")]
|
69 |
else:
|
70 |
#img = Image.open(image)
|
71 |
+
#input_text = "Please describe the image from six dimensions, including weather (clear, sandstorm, foggy, rainy, snowy), angle (front, left, top), time (daytime, night), occlusion (unoccluded, lightly-occluded, partially-occluded, moderately-occluded, heavily-occluded), season (spring-summer, autumn, winter). Each dimension should be described in no more than 4 words and should match the image content. Please try to output from the options in the previous brackets. If there is no suitable result, output N/A."# Please also output a probability of your inference."# If there is no information in a certain dimension, you can directly output no information.
|
72 |
+
#domains = gemini_response_vision(input_texts=input_text, image=image)
|
73 |
+
#IMAGE_PATH = './reasoning_xy.jpg'
|
74 |
+
base64_image = encode_image(image)
|
75 |
+
prompt = "Please describe the image from six dimensions, including weather (clear, sandstorm, foggy, rainy, snowy), angle (front, left, top), time (daytime, night), occlusion (unoccluded, lightly-occluded, partially-occluded, moderately-occluded, heavily-occluded), season (spring-summer, autumn, winter). Each dimension should be described in no more than 4 words and should match the image content. Please try to output from the options in the previous brackets. If there is no suitable result, output N/A."# Please also output a probability of your inference."# If there is no information in a certain dimension, you can directly output no information."
|
76 |
+
|
77 |
+
response = client.chat.completions.create(
|
78 |
+
model="o1-preview",
|
79 |
+
messages=[
|
80 |
+
# {"role": "system", "content": "You are a helpful assistant that responds in Markdown. Help me with my math homework!"},
|
81 |
+
{"role": "user", "content": [
|
82 |
+
{"type": "text", "text": prompt},
|
83 |
+
{"type": "image_url", "image_url": {
|
84 |
+
"url": f"data:image/png;base64,{base64_image}"}
|
85 |
+
}
|
86 |
+
]}
|
87 |
+
],
|
88 |
+
temperature=0.0,
|
89 |
+
)
|
90 |
+
domains = response.choices[0].message.content
|
91 |
print(domains)
|
92 |
|
93 |
hypothesis_template = hypothesis_template_prefix + ' ' + hypothesis_template_suffix.format(*domains)
|