bigbossmonster commited on
Commit
c24bff4
·
verified ·
1 Parent(s): 2fdba39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -9,14 +9,15 @@ import base64
9
  from concurrent.futures import ThreadPoolExecutor
10
  from PIL import Image, ImageOps
11
 
12
-
13
  from fastapi import FastAPI, UploadFile, File, Form, HTTPException
14
  from fastapi.staticfiles import StaticFiles
15
  from fastapi.middleware.cors import CORSMiddleware
16
- from PIL import Image
17
  import rarfile
18
  import zipfile
19
- import google.generativeai as genai
 
 
 
20
 
21
  # Configure logging
22
  logging.basicConfig(level=logging.INFO)
@@ -88,12 +89,9 @@ def parse_srt(content: str):
88
  return parsed
89
 
90
 
91
- logger = logging.getLogger(__name__)
92
-
93
  def compress_image(image_bytes, max_width=800, quality=80):
94
  """
95
  Compresses an image to WebP (best) or optimized JPEG.
96
- Renamed back to 'compress_image' to fix your error.
97
  """
98
  try:
99
  img = Image.open(io.BytesIO(image_bytes))
@@ -104,7 +102,6 @@ def compress_image(image_bytes, max_width=800, quality=80):
104
  buffer = io.BytesIO()
105
 
106
  # 2. Try WebP first (Best quality/size ratio)
107
- # If you strictly need JPEG, change use_webp to False
108
  use_webp = True
109
 
110
  if use_webp:
@@ -136,14 +133,14 @@ def compress_image(image_bytes, max_width=800, quality=80):
136
 
137
  except Exception as e:
138
  logger.error(f"Image compression failed: {e}")
139
- # If logging isn't setup, print the error so you can see it
140
- print(f"Error: {e}")
141
  return None
142
 
 
143
  def process_batch_gemini(api_key, items, model_name):
144
  try:
145
- genai.configure(api_key=api_key)
146
- model = genai.GenerativeModel(model_name)
 
147
 
148
  prompt_parts = [
149
  "You are a Subtitle Quality Control (QC) bot.",
@@ -158,13 +155,18 @@ def process_batch_gemini(api_key, items, model_name):
158
  prompt_parts.append(f"Index: {item['index']}")
159
  prompt_parts.append(f"Expected Text: \"{item['expected_text']}\"")
160
  prompt_parts.append(f"Image:")
 
 
161
  img = Image.open(io.BytesIO(item['image_data']))
162
  prompt_parts.append(img)
163
 
164
- # Enforce JSON mode
165
- response = model.generate_content(
166
- prompt_parts,
167
- generation_config={"response_mime_type": "application/json"}
 
 
 
168
  )
169
 
170
  text = response.text.replace("```json", "").replace("```", "").strip()
@@ -173,7 +175,6 @@ def process_batch_gemini(api_key, items, model_name):
173
  return json.loads(text)
174
  except json.JSONDecodeError as e:
175
  # Handle Truncated JSON (Output Token Limit Exceeded)
176
- # This happens if the batch size is too large for the model's output window
177
  logger.warning(f"JSON Parse Error (likely truncated response): {e}. Attempting repair...")
178
 
179
  # Repair Strategy: Find the last closing brace '}', discard everything after, and close the array ']'
@@ -201,7 +202,7 @@ async def analyze_subtitles(
201
  media_files: list[UploadFile] = File(...),
202
  api_keys: str = Form(...),
203
  batch_size: int = Form(20),
204
- model_name: str = Form("gemini-3-flash-preview"),
205
  compression_quality: float = Form(0.7)
206
  ):
207
  temp_dir = tempfile.mkdtemp()
 
9
  from concurrent.futures import ThreadPoolExecutor
10
  from PIL import Image, ImageOps
11
 
 
12
  from fastapi import FastAPI, UploadFile, File, Form, HTTPException
13
  from fastapi.staticfiles import StaticFiles
14
  from fastapi.middleware.cors import CORSMiddleware
 
15
  import rarfile
16
  import zipfile
17
+
18
+ # --- MIGRATION: New SDK Imports ---
19
+ from google import genai
20
+ from google.genai import types
21
 
22
  # Configure logging
23
  logging.basicConfig(level=logging.INFO)
 
89
  return parsed
90
 
91
 
 
 
92
  def compress_image(image_bytes, max_width=800, quality=80):
93
  """
94
  Compresses an image to WebP (best) or optimized JPEG.
 
95
  """
96
  try:
97
  img = Image.open(io.BytesIO(image_bytes))
 
102
  buffer = io.BytesIO()
103
 
104
  # 2. Try WebP first (Best quality/size ratio)
 
105
  use_webp = True
106
 
107
  if use_webp:
 
133
 
134
  except Exception as e:
135
  logger.error(f"Image compression failed: {e}")
 
 
136
  return None
137
 
138
+ # --- MIGRATION: Updated Gemini Processing Function ---
139
  def process_batch_gemini(api_key, items, model_name):
140
  try:
141
+ # 1. Instantiate the Client (New SDK pattern)
142
+ # This replaces genai.configure()
143
+ client = genai.Client(api_key=api_key)
144
 
145
  prompt_parts = [
146
  "You are a Subtitle Quality Control (QC) bot.",
 
155
  prompt_parts.append(f"Index: {item['index']}")
156
  prompt_parts.append(f"Expected Text: \"{item['expected_text']}\"")
157
  prompt_parts.append(f"Image:")
158
+
159
+ # The new SDK handles PIL images directly in the contents list just like the old one
160
  img = Image.open(io.BytesIO(item['image_data']))
161
  prompt_parts.append(img)
162
 
163
+ # 2. Call generate_content via the client
164
+ response = client.models.generate_content(
165
+ model=model_name,
166
+ contents=prompt_parts,
167
+ config=types.GenerateContentConfig(
168
+ response_mime_type="application/json"
169
+ )
170
  )
171
 
172
  text = response.text.replace("```json", "").replace("```", "").strip()
 
175
  return json.loads(text)
176
  except json.JSONDecodeError as e:
177
  # Handle Truncated JSON (Output Token Limit Exceeded)
 
178
  logger.warning(f"JSON Parse Error (likely truncated response): {e}. Attempting repair...")
179
 
180
  # Repair Strategy: Find the last closing brace '}', discard everything after, and close the array ']'
 
202
  media_files: list[UploadFile] = File(...),
203
  api_keys: str = Form(...),
204
  batch_size: int = Form(20),
205
+ model_name: str = Form("gemini-2.0-flash"), # Updated default model hint
206
  compression_quality: float = Form(0.7)
207
  ):
208
  temp_dir = tempfile.mkdtemp()