CoolFace
Apppublic

helo-ayush/Diarization_VoiceFingerprinted

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
embedding_processor.py39 linesDownload Raw Back to utils
1# ==============================================================================
2# TEXT EMBEDDING PROCESSOR
3# Uses Google GenAI to map text summaries into a 768-dimensional Math Vector 
4# to be saved into MongoDB Atlas Vector Search.
5# ==============================================================================
6import os
7import asyncio
8from google import genai
9
10
11# Use Google GenAI SDK directly for embeddings
12client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
13
14
15async def generate_embedding(text: str) -> list[float]:
16    """
17    Generate a 768-dimensional embedding vector from text using Gemini.
18    Used for MongoDB Atlas Vector Search.
19    """
20    print("   ๐Ÿ”ข Generating embedding vector...")
21    try:
22        result = await asyncio.wait_for(
23            asyncio.to_thread(
24                client.models.embed_content,
25                model="gemini-embedding-001",
26                contents=text,
27            ),
28            timeout=30
29        )
30        vector = result.embeddings[0].values
31        print(f"   โœ… Embedding generated: {len(vector)} dimensions")
32        return vector
33    except asyncio.TimeoutError:
34        print("   โŒ Embedding generation timed out after 30s")
35        return []
36    except Exception as e:
37        print(f"   โŒ Embedding generation failed: {e}")
38        return []
39