AkashKumarave commited on
Commit
905b21d
·
verified ·
1 Parent(s): 28fcce4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -16,7 +16,8 @@ from pydantic import BaseModel
16
  from typing import Optional
17
  from PIL import Image
18
  from io import BytesIO
19
- from google import genai
 
20
 
21
  # Configure logging
22
  logging.basicConfig(level=logging.INFO)
@@ -44,6 +45,9 @@ app.add_middleware(
44
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "AIzaSyDL5Rilo7ptJpUOZdY6wy8PJYUcVcnDADs")
45
  GEMINI_MODEL = "gemini-2.5-flash-image-preview"
46
 
 
 
 
47
  # ===== RAZORPAY CONFIGURATION =====
48
  RAZORPAY_KEY_ID = os.getenv("RAZORPAY_KEY_ID")
49
  RAZORPAY_KEY_SECRET = os.getenv("RAZORPAY_KEY_SECRET")
@@ -100,24 +104,19 @@ def validate_image(image_content: bytes):
100
  except Exception as e:
101
  raise HTTPException(status_code=400, detail=f"Image validation error: {str(e)}")
102
 
103
- def generate_gemini_image(images: List[Image.Image], prompt: str, api_key: str):
104
  """Generate or edit image using Gemini API"""
105
  try:
106
- client = genai.Client(api_key=api_key)
107
  contents = images + [prompt]
108
-
109
- response = client.models.generate_content(
110
- model=GEMINI_MODEL,
111
- contents=contents,
112
- )
113
 
114
  text_response = ""
115
  image_path = None
116
 
117
  for part in response.candidates[0].content.parts:
118
- if part.text is not None:
119
  text_response += part.text + "\n"
120
- elif part.inline_data is not None:
121
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
122
  temp_path = tmp.name
123
  generated_image = Image.open(BytesIO(part.inline_data.data))
@@ -207,7 +206,7 @@ async def generate_image(images: List[bytes], prompt: str, user_id: Optional[str
207
  raise HTTPException(status_code=500, detail=f"User verification failed: {str(e)}")
208
 
209
  # Generate image using Gemini API
210
- image_path, text_response = generate_gemini_image(pil_images, prompt, GEMINI_API_KEY)
211
 
212
  if image_path:
213
  return image_path
 
16
  from typing import Optional
17
  from PIL import Image
18
  from io import BytesIO
19
+ import google.generativeai as genai
20
+ import tempfile
21
 
22
  # Configure logging
23
  logging.basicConfig(level=logging.INFO)
 
45
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "AIzaSyDL5Rilo7ptJpUOZdY6wy8PJYUcVcnDADs")
46
  GEMINI_MODEL = "gemini-2.5-flash-image-preview"
47
 
48
+ # Configure Gemini API
49
+ genai.configure(api_key=GEMINI_API_KEY)
50
+
51
  # ===== RAZORPAY CONFIGURATION =====
52
  RAZORPAY_KEY_ID = os.getenv("RAZORPAY_KEY_ID")
53
  RAZORPAY_KEY_SECRET = os.getenv("RAZORPAY_KEY_SECRET")
 
104
  except Exception as e:
105
  raise HTTPException(status_code=400, detail=f"Image validation error: {str(e)}")
106
 
107
+ def generate_gemini_image(images: List[Image.Image], prompt: str):
108
  """Generate or edit image using Gemini API"""
109
  try:
 
110
  contents = images + [prompt]
111
+ response = genai.GenerativeModel(GEMINI_MODEL).generate_content(contents)
 
 
 
 
112
 
113
  text_response = ""
114
  image_path = None
115
 
116
  for part in response.candidates[0].content.parts:
117
+ if part.text:
118
  text_response += part.text + "\n"
119
+ elif hasattr(part, 'inline_data') and part.inline_data:
120
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
121
  temp_path = tmp.name
122
  generated_image = Image.open(BytesIO(part.inline_data.data))
 
206
  raise HTTPException(status_code=500, detail=f"User verification failed: {str(e)}")
207
 
208
  # Generate image using Gemini API
209
+ image_path, text_response = generate_gemini_image(pil_images, prompt)
210
 
211
  if image_path:
212
  return image_path