NoteServicesAPI / app /services /summary_service.py
bichnhan2701's picture
Fix ModuleNotFoundError
983ffca
raw
history blame
1.09 kB
import asyncio
from app.config import GEMINI_API_KEY
import google.generativeai as genai
if GEMINI_API_KEY:
genai.configure(api_key=GEMINI_API_KEY)
_model = genai.GenerativeModel("gemini-pro")
else:
_model = None
async def generate_summary(text: str) -> str:
if not _model:
return ""
prompt = f"""
Bạn là chuyên gia tóm tắt. Hãy tóm tắt văn bản sau thành **một đoạn văn duy nhất**.
Yêu cầu:
1. Viết khoảng 3-5 câu, tổng hợp đầy đủ chủ đề và các ý chính.
2. Viết liền mạch, KHÔNG xuống dòng, KHÔNG dùng gạch đầu dòng hay đánh số.
3. Chỉ dựa trên thông tin được cung cấp, tuyệt đối KHÔNG tự thêm thông tin bên ngoài.
4. Trả về văn bản thuần (plain text).
Văn bản:
\"\"\"{text}\"\"\"
"""
loop = asyncio.get_event_loop()
def call():
r = _model.generate_content(prompt)
return r.text.strip()
result = await loop.run_in_executor(None, call)
return result.replace("```", "").strip()