import asyncio, json 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_mindmap(text: str) -> dict: if not _model: return {} prompt = f""" Bạn là chuyên gia tạo Sơ đồ tư duy. Hãy phân tích văn bản sau và tạo cấu trúc JSON Mindmap. Yêu cầu: 1. Xác định Ý chính làm Root. 2. Phân tách ý phụ thành nhánh con (tối đa 3 cấp). 3. Nhãn (label) ngắn gọn (< 7 từ). 4. Màu sắc (colorHex): Root="#6200EE", Con="#F59E2B", "#2ECF9A", "#2F9BFF". Cấu trúc JSON bắt buộc (Chỉ trả về JSON): {{ "root": {{ "label": "Chủ đề", "colorHex": "#6200EE", "children": [ {{ "label": "Ý 1", "colorHex": "#F59E2B", "children": [] }} ] }} }} Văn bản: {text} """ loop = asyncio.get_event_loop() def call(): r = _model.generate_content(prompt) return r.text raw = await loop.run_in_executor(None, call) start = raw.find("{") end = raw.rfind("}") if start != -1 and end != -1: return json.loads(raw[start:end+1]) return {}