Tim Luka Horstmann commited on
Commit
01067c4
·
1 Parent(s): 61697c0

Removed the embeddings/FAISS path from the app

Browse files
Files changed (2) hide show
  1. app.py +2 -39
  2. requirements.txt +1 -5
app.py CHANGED
@@ -1,8 +1,6 @@
1
  from datetime import datetime
2
  import json
3
  import time
4
- import numpy as np
5
- from sentence_transformers import SentenceTransformer
6
  from fastapi import FastAPI, HTTPException, BackgroundTasks, Request
7
  from fastapi.responses import StreamingResponse, Response
8
  from fastapi.middleware.cors import CORSMiddleware
@@ -12,7 +10,6 @@ from llama_cpp import Llama
12
  from huggingface_hub import login, hf_hub_download
13
  import logging
14
  import os
15
- import faiss
16
  import asyncio
17
  import psutil # Added for RAM tracking
18
  from google import genai
@@ -77,7 +74,6 @@ login(token=hf_token)
77
 
78
  # Models Configuration
79
  USE_GEMINI = os.getenv("USE_GEMINI", "false").lower() == "true"
80
- sentence_transformer_model = "all-MiniLM-L6-v2"
81
  repo_id = "unsloth/Qwen3-1.7B-GGUF" # "bartowski/deepcogito_cogito-v1-preview-llama-3B-GGUF" # "bartowski/deepcogito_cogito-v1-preview-llama-8B-GGUF"
82
  filename = "Qwen3-1.7B-Q4_K_M.gguf" # "deepcogito_cogito-v1-preview-llama-3B-Q4_K_M.gguf"
83
 
@@ -123,27 +119,6 @@ faqs = [
123
  ]
124
 
125
  try:
126
- # Load CV embeddings and build FAISS index
127
- logger.info("Loading CV embeddings from cv_embeddings.json")
128
- with open("cv_embeddings.json", "r", encoding="utf-8") as f:
129
- cv_data = json.load(f)
130
- cv_chunks = [item["chunk"] for item in cv_data]
131
- cv_embeddings = np.array([item["embedding"] for item in cv_data]).astype('float32')
132
- faiss.normalize_L2(cv_embeddings)
133
- faiss_index = faiss.IndexFlatIP(cv_embeddings.shape[1])
134
- faiss_index.add(cv_embeddings)
135
- logger.info("FAISS index built successfully")
136
-
137
- # Load embedding model
138
- logger.info("Loading SentenceTransformer model")
139
- embedder = SentenceTransformer(sentence_transformer_model, device="cpu")
140
- logger.info("SentenceTransformer model loaded")
141
-
142
- # Compute FAQ embeddings
143
- faq_questions = [faq["question"] for faq in faqs]
144
- faq_embeddings = embedder.encode(faq_questions, convert_to_numpy=True).astype("float32")
145
- faiss.normalize_L2(faq_embeddings)
146
-
147
  # Load the local model only if not using Gemini
148
  if not USE_GEMINI:
149
  logger.info(f"Loading {filename} model")
@@ -174,17 +149,6 @@ except Exception as e:
174
  logger.error(f"Startup error: {str(e)}", exc_info=True)
175
  raise
176
 
177
- def retrieve_context(query, top_k=2):
178
- try:
179
- query_embedding = embedder.encode(query, convert_to_numpy=True).astype("float32")
180
- query_embedding = query_embedding.reshape(1, -1)
181
- faiss.normalize_L2(query_embedding)
182
- distances, indices = faiss_index.search(query_embedding, top_k)
183
- return "\n".join([cv_chunks[i] for i in indices[0]])
184
- except Exception as e:
185
- logger.error(f"Error in retrieve_context: {str(e)}")
186
- raise
187
-
188
  # Load the full CV at startup with explicit UTF-8 handling
189
  try:
190
  with open("cv_text.txt", "r", encoding="utf-8") as f:
@@ -656,9 +620,8 @@ async def health_check(request: Request):
656
  @limiter.limit("10/minute") # Limit model info requests
657
  async def model_info(request: Request):
658
  base_info = {
659
- "embedding_model": sentence_transformer_model,
660
- "faiss_index_size": len(cv_chunks),
661
- "faiss_index_dim": cv_embeddings.shape[1],
662
  "tts_available": elevenlabs_client is not None,
663
  }
664
 
 
1
  from datetime import datetime
2
  import json
3
  import time
 
 
4
  from fastapi import FastAPI, HTTPException, BackgroundTasks, Request
5
  from fastapi.responses import StreamingResponse, Response
6
  from fastapi.middleware.cors import CORSMiddleware
 
10
  from huggingface_hub import login, hf_hub_download
11
  import logging
12
  import os
 
13
  import asyncio
14
  import psutil # Added for RAM tracking
15
  from google import genai
 
74
 
75
  # Models Configuration
76
  USE_GEMINI = os.getenv("USE_GEMINI", "false").lower() == "true"
 
77
  repo_id = "unsloth/Qwen3-1.7B-GGUF" # "bartowski/deepcogito_cogito-v1-preview-llama-3B-GGUF" # "bartowski/deepcogito_cogito-v1-preview-llama-8B-GGUF"
78
  filename = "Qwen3-1.7B-Q4_K_M.gguf" # "deepcogito_cogito-v1-preview-llama-3B-Q4_K_M.gguf"
79
 
 
119
  ]
120
 
121
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  # Load the local model only if not using Gemini
123
  if not USE_GEMINI:
124
  logger.info(f"Loading {filename} model")
 
149
  logger.error(f"Startup error: {str(e)}", exc_info=True)
150
  raise
151
 
 
 
 
 
 
 
 
 
 
 
 
152
  # Load the full CV at startup with explicit UTF-8 handling
153
  try:
154
  with open("cv_text.txt", "r", encoding="utf-8") as f:
 
620
  @limiter.limit("10/minute") # Limit model info requests
621
  async def model_info(request: Request):
622
  base_info = {
623
+ "context_source": "cv_text.txt",
624
+ "retrieval": "disabled",
 
625
  "tts_available": elevenlabs_client is not None,
626
  }
627
 
requirements.txt CHANGED
@@ -1,14 +1,10 @@
1
  fastapi==0.115.0
2
  uvicorn==0.31.0
3
- sentence-transformers==3.1.1
4
- torch==2.4.1
5
- numpy==1.26.4
6
  huggingface_hub==0.30.1
7
- faiss-cpu==1.8.0
8
  psutil
9
  google-genai
10
  asyncio
11
  elevenlabs
12
  httpx
13
  llama-cpp-python==0.2.85
14
- slowapi==0.1.9
 
1
  fastapi==0.115.0
2
  uvicorn==0.31.0
 
 
 
3
  huggingface_hub==0.30.1
 
4
  psutil
5
  google-genai
6
  asyncio
7
  elevenlabs
8
  httpx
9
  llama-cpp-python==0.2.85
10
+ slowapi==0.1.9