10
10
11
11
load_dotenv ()
12
12
13
- # API Keys
14
13
STABILITY_API_KEY = os .getenv ("STABILITY_API_KEY" )
15
14
GEMINI_API_KEY = os .getenv ("GEMINI_API_KEY" )
16
-
17
- # API Host and Engine Configuration
18
15
API_HOST = "https://api.stability.ai"
19
16
ENGINE_ID = "stable-diffusion-v1-6"
20
17
21
- # Gemini AI Configuration
22
18
genai .configure (api_key = GEMINI_API_KEY )
23
19
generation_config = {
24
20
"temperature" : 1 ,
33
29
system_instruction = SYSTEM_PROMPT ,
34
30
)
35
31
36
- # Get Text Response from Gemini Model
37
32
def get_response (conversation ):
38
33
try :
39
34
chat_session = model .start_chat (history = conversation )
@@ -43,21 +38,19 @@ def get_response(conversation):
43
38
print (f"Error in get_response: { e } " )
44
39
return "Uhg my brain hurts, can you say that again?"
45
40
46
- # Recognize Image and Generate Content Based on Prompt
47
41
async def recognize_image (image_data , prompt ):
48
42
try :
49
43
image_format = imghdr .what (None , image_data )
50
44
if not image_format :
51
- return "Failed to determine image format"
45
+ return "Failed to determine image format. "
52
46
53
47
image = Image .open (BytesIO (image_data ))
54
48
response = model .generate_content ([prompt , image ])
55
- return response .text if response and hasattr (response , 'text' ) else "Failed to generate a response from the Gemini model "
49
+ return response .text if response and hasattr (response , 'text' ) else "Failed to generate a response. "
56
50
except Exception as e :
57
51
print (f"Error in recognize_image: { e } " )
58
52
return None
59
53
60
- # Generate Image from Text Prompt
61
54
def get_image (text ):
62
55
try :
63
56
if not STABILITY_API_KEY :
@@ -86,7 +79,6 @@ def get_image(text):
86
79
print (f"Error in get_image: { e } " )
87
80
return None
88
81
89
- # Edit Image Based on Prompt
90
82
def edit_image (image_bytes , prompt ):
91
83
try :
92
84
response = requests .post (
0 commit comments