Rthur2003 commited on
Commit
c6a816d
·
1 Parent(s): 9bf9e37

refactor: Migrate from deprecated google.generativeai to google.genai package

Browse files

- Update gemini_service.py to use new google.genai Client API
- Upgrade model to gemini-2.0-flash
- Update requirements.txt to use google-genai>=1.0.0

app/routes/commend/gemini_service.py CHANGED
@@ -1,6 +1,7 @@
1
  """
2
  Gemini AI Service for Crown Commend
3
  Generates contextual YouTube comments using Google's Gemini API.
 
4
  """
5
 
6
  from __future__ import annotations
@@ -8,32 +9,32 @@ from __future__ import annotations
8
  import os
9
  from typing import Optional, Tuple, List, Dict, Any
10
 
11
- import google.generativeai as genai
 
12
 
13
  from ...services.logging_config import get_logger
14
 
15
  logger = get_logger(__name__)
16
 
17
- # Gemini model configuration
18
- _model = None
19
 
20
 
21
- def _get_model():
22
- """Lazy-load Gemini model."""
23
- global _model
24
 
25
- if _model is not None:
26
- return _model
27
 
28
  api_key = os.getenv('COMMEND_GEMINI_API_KEY')
29
  if not api_key:
30
  raise ValueError("COMMEND_GEMINI_API_KEY environment variable is required")
31
 
32
  try:
33
- genai.configure(api_key=api_key)
34
- _model = genai.GenerativeModel('gemini-1.5-flash')
35
- logger.info("Gemini model initialized successfully")
36
- return _model
37
  except Exception as e:
38
  logger.error(f"Failed to initialize Gemini: {e}")
39
  raise ValueError(f"Gemini initialization failed: {e}")
@@ -42,7 +43,7 @@ def _get_model():
42
  def summarize_transcript(transcript_text: str, language: str) -> Tuple[Optional[str], Optional[str]]:
43
  """Video transcript'ini özetler."""
44
  try:
45
- model = _get_model()
46
 
47
  if not transcript_text or len(transcript_text.strip()) < 50:
48
  return None, "Insufficient transcript data"
@@ -55,7 +56,10 @@ Bu video transcript'ini {language} dilinde 2-3 cümlelik kısa bir özet haline
55
  ONEMLI: Sadece özetin kendisini yaz, başka açıklama yapma.
56
  """
57
 
58
- response = model.generate_content(prompt)
 
 
 
59
  return response.text.strip(), None
60
 
61
  except Exception as e:
@@ -84,7 +88,7 @@ def generate_comment(
84
  (generated_comment, error_message)
85
  """
86
  try:
87
- model = _get_model()
88
 
89
  # Format existing comments
90
  comment_section = "No comments available to analyze."
@@ -145,7 +149,10 @@ Iyi: "3:45'teki o detay gercekten onemli bir noktaydi, bu konuya hic bu acidan b
145
  **Yorumun:**
146
  """
147
 
148
- response = model.generate_content(prompt)
 
 
 
149
  generated_text = response.text.strip()
150
 
151
  # Clean up the response if needed
 
1
  """
2
  Gemini AI Service for Crown Commend
3
  Generates contextual YouTube comments using Google's Gemini API.
4
+ Uses the new google.genai package (replacing deprecated google.generativeai).
5
  """
6
 
7
  from __future__ import annotations
 
9
  import os
10
  from typing import Optional, Tuple, List, Dict, Any
11
 
12
+ from google import genai
13
+ from google.genai import types
14
 
15
  from ...services.logging_config import get_logger
16
 
17
  logger = get_logger(__name__)
18
 
19
+ # Gemini client configuration
20
+ _client = None
21
 
22
 
23
+ def _get_client():
24
+ """Lazy-load Gemini client."""
25
+ global _client
26
 
27
+ if _client is not None:
28
+ return _client
29
 
30
  api_key = os.getenv('COMMEND_GEMINI_API_KEY')
31
  if not api_key:
32
  raise ValueError("COMMEND_GEMINI_API_KEY environment variable is required")
33
 
34
  try:
35
+ _client = genai.Client(api_key=api_key)
36
+ logger.info("Gemini client initialized successfully")
37
+ return _client
 
38
  except Exception as e:
39
  logger.error(f"Failed to initialize Gemini: {e}")
40
  raise ValueError(f"Gemini initialization failed: {e}")
 
43
  def summarize_transcript(transcript_text: str, language: str) -> Tuple[Optional[str], Optional[str]]:
44
  """Video transcript'ini özetler."""
45
  try:
46
+ client = _get_client()
47
 
48
  if not transcript_text or len(transcript_text.strip()) < 50:
49
  return None, "Insufficient transcript data"
 
56
  ONEMLI: Sadece özetin kendisini yaz, başka açıklama yapma.
57
  """
58
 
59
+ response = client.models.generate_content(
60
+ model="gemini-2.0-flash",
61
+ contents=prompt
62
+ )
63
  return response.text.strip(), None
64
 
65
  except Exception as e:
 
88
  (generated_comment, error_message)
89
  """
90
  try:
91
+ client = _get_client()
92
 
93
  # Format existing comments
94
  comment_section = "No comments available to analyze."
 
149
  **Yorumun:**
150
  """
151
 
152
+ response = client.models.generate_content(
153
+ model="gemini-2.0-flash",
154
+ contents=prompt
155
+ )
156
  generated_text = response.text.strip()
157
 
158
  # Clean up the response if needed
requirements.txt CHANGED
@@ -46,7 +46,7 @@ yt-dlp>=2024.1.0
46
  spotdl>=4.2.4
47
 
48
  # === Crown Commend (YouTube Comment AI) ===
49
- google-generativeai>=0.8.3
50
  google-api-python-client>=2.134.0
51
  google-auth-httplib2>=0.2.0
52
  google-auth-oauthlib>=1.2.0
 
46
  spotdl>=4.2.4
47
 
48
  # === Crown Commend (YouTube Comment AI) ===
49
+ google-genai>=1.0.0
50
  google-api-python-client>=2.134.0
51
  google-auth-httplib2>=0.2.0
52
  google-auth-oauthlib>=1.2.0