-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
142 lines (117 loc) · 4.59 KB
/
main.py
1
2
3
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# main.py
import os
import base64
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List
import uvicorn
import json
from io import BytesIO
from PIL import Image
import google.generativeai as genai
import tempfile
app = FastAPI()
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
# Configure Gemini API
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
class JudgmentResponse(BaseModel):
judgments: List[dict]
ai_confidence_level: float
def judge_similarity(target_description: str, image_files: List[UploadFile]):
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
"response_mime_type": "application/json",
}
model = genai.GenerativeModel(
model_name="gemini-1.5-pro",
generation_config=generation_config,
)
def image_to_file(image_file):
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
temp_file.write(image_file.file.read())
return genai.upload_file(temp_file.name, mime_type="image/png")
files = [image_to_file(img) for img in image_files]
prompt = f"""
You are an AI image analyst tasked with judging the associative closeness between a given phrase and a set of images. Your goal is to evaluate how closely each image relates to the provided phrase and assign a score accordingly.
Here is the phrase you will be using for association:
<phrase>
{target_description}
</phrase>
Now, examine the attached images.
For each image, you will assign a score from 1 to 100, where:
1 = Extremely weak association, too many steps away
50 = Moderate association
100 = Extremely strong association
Your output should be a JSON list, with each item containing:
- "image_number": The index of the image (starting from 0)
- "explanation": A justification for your score, up to 5 sentences
- "score": Your assigned score from 1 to 100
Consider visual elements, themes, emotions, colors, and any other relevant factors. Anything can be associated with anything given enough intermediate steps. Be creative when the phrase is semantically very far from all the images, try to find any association which still lets you give one image higher score than others.
Here's an example of how your response should be structured:
[
{{
"image_number": 0,
"explanation": "The image strongly relates to the phrase due to [reason], but lacks [element] which prevents a higher score.",
"score": 75
}},
{{
"image_number": 1,
"explanation": "While the image contains [element] related to the phrase, the overall association is weak because [reason].",
"score": 30
}},
...
]
"""
chat_session = model.start_chat(history=[
{
"role": "user",
"parts": files + [prompt],
},
])
response = chat_session.send_message(
"Now, proceed with your analysis and provide your final answer in the format described above."
)
try:
result = json.loads(response.text)
return result
except json.JSONDecodeError:
raise ValueError("Failed to parse JSON response from Gemini")
finally:
# Clean up temporary image files
for file in files:
os.remove(file.local_file_path)
@app.post("/judge", response_model=JudgmentResponse)
async def judge_impression(
images: List[UploadFile] = File(...),
impression: str = Form(...),
):
try:
judgments = judge_similarity(impression, images)
# Calculate AI confidence level (average of all scores)
total_score = sum(judgment['score'] for judgment in judgments)
ai_confidence_level = total_score / (len(judgments) * 100) # Normalize to 0-1 range
return JudgmentResponse(
judgments=judgments,
ai_confidence_level=ai_confidence_level
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/health")
async def health_check():
return {"status": "healthy"}
# Render specific: bind to PORT environment variable
if __name__ == "__main__":
import uvicorn
port = int(os.environ.get("PORT", 10000))
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=True)