google_ai_studio.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import os
  2. import time
  3. import uuid
  4. from typing import Optional
  5. import google.generativeai as genai
  6. import orjson
  7. import requests
  8. from google.generativeai.types import (HarmBlockThreshold, HarmCategory)
  9. from loguru import logger
  10. import traceback
  11. from utils.json_utils import parse_general_json
  12. CACHE_DIR = '/app/cache/'
  13. # CACHE_DIR = '/Users/z/Downloads/'
  14. # PROXY_ADDR = 'http://localhost:1081'
  15. # os.environ['http_proxy'] = PROXY_ADDR
  16. # os.environ['https_proxy'] = PROXY_ADDR
  17. class GoogleAI(object):
  18. @classmethod
  19. def download_video(cls, video_link: str) -> Optional[str]:
  20. file_path = os.path.join(CACHE_DIR, f'{str(uuid.uuid4())}.mp4')
  21. for _ in range(3):
  22. try:
  23. response = requests.get(url=video_link, timeout=60)
  24. if response.status_code == 200:
  25. with open(file_path, 'wb') as f:
  26. f.write(response.content)
  27. logger.info(f'[内容分析] 视频链接: {video_link}, 存储地址: {file_path}')
  28. return file_path
  29. except Exception:
  30. time.sleep(1)
  31. continue
  32. return
  33. @classmethod
  34. def run(cls, api_key, video_path):
  35. try:
  36. genai.configure(api_key=api_key)
  37. video = genai.upload_file(path=video_path, mime_type='video/mp4')
  38. while video.state.name == 'PROCESSING':
  39. time.sleep(1)
  40. video = genai.get_file(name=video.name)
  41. if video.state.name != 'ACTIVE':
  42. genai.delete_file(name=video.name)
  43. return
  44. model = genai.GenerativeModel(
  45. model_name='gemini-1.5-flash',
  46. generation_config=genai.GenerationConfig(response_mime_type='application/json'),
  47. safety_settings={
  48. HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
  49. },
  50. )
  51. response = model.generate_content(
  52. contents=[
  53. video,
  54. '你是一名专业的短视频分析师,请你输出这个视频的完整口播,只输出文字即可。使用以下JSON格式输出,不要包含任何额外的解释、注释或非JSON文本:{"text": string}',
  55. ],
  56. stream=False,
  57. request_options={
  58. 'timeout': 600,
  59. },
  60. )
  61. # 打印响应内容用于调试
  62. logger.info(f"[+] 响应内容: {response.text}")
  63. # 使用通用 JSON 解析函数解析响应
  64. text = parse_general_json(response.text, key='text')
  65. if text is None:
  66. return
  67. # text = orjson.loads(response.text.strip())['text']
  68. genai.delete_file(name=video.name)
  69. return text
  70. except Exception as e:
  71. logger.error(f"[内容分析] 处理异常, 异常类型: {type(e).__name__}, 异常信息: {e}\n{traceback.format_exc()}")
  72. return
  73. if __name__ == '__main__':
  74. GoogleAI.run("AIzaSyAwGqthDADh5NPVe3BMcOJBQkJaf0HWBuQ",
  75. "http://rescdn.yishihui.com/jq_oss/video/2025012215472528213")