wangzhibo
2025-08-04 f5106b912f1261fe633aa6bcdf10a4b7e74345aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import random
import google.generativeai as genai
from auto_media_publisher.config.conf_base import GOOGLE_API_KEY,GOOGLE_MODEL
from auto_media_publisher.config.conf_base import GOOGLE_GEMINI_TEMPERATURE_MIN,GOOGLE_GEMINI_TEMPERATURE_MAX,GOOGLE_GEMINI_TOPN_MIN,GOOGLE_GEMINI_TOPN_MAX
 
def init_gemini_model(proxy=None):
    if isinstance(proxy, str) and proxy.strip().lower().startswith(('http://', 'https://')):
        import os
        os.environ['HTTP_PROXY'] = proxy
        os.environ['HTTPS_PROXY'] = proxy
 
    genai.configure(api_key=GOOGLE_API_KEY)
    temperature = round(random.uniform(GOOGLE_GEMINI_TEMPERATURE_MIN, GOOGLE_GEMINI_TEMPERATURE_MAX), 2)
    top_n = round(random.uniform(GOOGLE_GEMINI_TOPN_MIN, GOOGLE_GEMINI_TOPN_MAX), 2)
    model = genai.GenerativeModel(GOOGLE_MODEL,
            generation_config=genai.types.GenerationConfig(
                temperature=temperature,
                top_p=top_n,
                top_k=random.choice([40, 50, 80, 100])
            )
        )
    return model