|
|
|
""" |
|
Lightweight Gemini-Pro helper (text in → text out). |
|
Requires env var GEMINI_KEY. |
|
""" |
|
|
|
import os, asyncio, google.generativeai as genai |
|
|
|
GEN_KEY = os.getenv("GEMINI_KEY") |
|
if GEN_KEY: |
|
genai.configure(api_key=GEN_KEY) |
|
|
|
_MODEL = None |
|
def _model(): |
|
global _MODEL |
|
if _MODEL is None: |
|
_MODEL = genai.GenerativeModel("gemini-pro") |
|
return _MODEL |
|
|
|
|
|
async def gemini_summarize(text: str, words: int = 150) -> str: |
|
prompt = f"Summarize in ≤{words} words:\n{text[:12000]}" |
|
rsp = await asyncio.to_thread(_model().generate_content, prompt) |
|
return rsp.text |
|
|
|
async def gemini_qa(question: str, context: str = "") -> str: |
|
prompt = f"Answer briefly.\nContext:\n{context[:10000]}\n\nQ: {question}\nA:" |
|
rsp = await asyncio.to_thread(_model().generate_content, prompt) |
|
return rsp.text |
|
|