Update api.py
Browse files
api.py
CHANGED
|
@@ -10,47 +10,33 @@ from deep_translator import GoogleTranslator
|
|
| 10 |
|
| 11 |
# ---------- Fix OpenAI version issue ----------
|
| 12 |
try:
|
| 13 |
-
# Ensure modern OpenAI SDK is installed and loaded
|
| 14 |
subprocess.run(
|
| 15 |
["pip", "install", "--no-cache-dir", "--upgrade", "openai==1.12.0"],
|
| 16 |
check=True,
|
| 17 |
)
|
| 18 |
import openai
|
| 19 |
importlib.reload(openai)
|
| 20 |
-
from openai import OpenAI
|
| 21 |
print("✅ Using OpenAI SDK version 1.12.0")
|
| 22 |
except Exception as e:
|
| 23 |
print("⚠️ Could not ensure correct OpenAI version:", e)
|
| 24 |
-
|
| 25 |
|
| 26 |
-
# Patch huggingface_hub
|
| 27 |
try:
|
| 28 |
import huggingface_hub
|
| 29 |
if not hasattr(huggingface_hub, "cached_download"):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
"pip",
|
| 33 |
-
"install",
|
| 34 |
-
"--no-cache-dir",
|
| 35 |
-
"huggingface-hub==0.24.5",
|
| 36 |
-
"transformers==4.30.2",
|
| 37 |
-
"sentence-transformers==2.2.2",
|
| 38 |
-
],
|
| 39 |
-
check=True,
|
| 40 |
-
)
|
| 41 |
-
print("✅ Downgraded huggingface-hub for sentence-transformers compatibility.")
|
| 42 |
except Exception as e:
|
| 43 |
-
print("⚠️ Could not
|
| 44 |
|
| 45 |
from sentence_transformers import SentenceTransformer
|
| 46 |
from pinecone import Pinecone
|
| 47 |
|
| 48 |
# ---------- Config ----------
|
| 49 |
DATASET_PATH = "data/coaching_millionaer_dataset.json"
|
| 50 |
-
# Load .env (for local dev), but also check Hugging Face environment
|
| 51 |
load_dotenv(override=True)
|
| 52 |
|
| 53 |
-
# Ensure environment variables are loaded even if running on Hugging Face
|
| 54 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") or os.environ.get("OPENAI_API_KEY")
|
| 55 |
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY") or os.environ.get("PINECONE_API_KEY")
|
| 56 |
|
|
@@ -61,29 +47,22 @@ app = Flask(__name__)
|
|
| 61 |
CORS(app, resources={r"/ask": {"origins": "*"}})
|
| 62 |
|
| 63 |
# ---------- OpenAI Client ----------
|
| 64 |
-
import openai
|
| 65 |
-
|
| 66 |
client = None
|
| 67 |
try:
|
| 68 |
if not OPENAI_API_KEY or OPENAI_API_KEY.strip() == "":
|
| 69 |
raise ValueError("Missing OPENAI_API_KEY in environment variables")
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# Fallback to legacy interface
|
| 78 |
-
openai.api_key = OPENAI_API_KEY
|
| 79 |
-
client = openai
|
| 80 |
-
print("✅ Using legacy OpenAI interface.")
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
client = None
|
| 84 |
print(f"❌ Failed to initialize OpenAI client: {e}")
|
| 85 |
|
| 86 |
-
|
| 87 |
# ---------- Retriever ----------
|
| 88 |
retriever = None
|
| 89 |
try:
|
|
@@ -123,7 +102,6 @@ except Exception as e:
|
|
| 123 |
|
| 124 |
# ---------- Translator ----------
|
| 125 |
def translate_text(text: str, target_lang: str) -> str:
|
| 126 |
-
"""Translate text using deep-translator (GoogleTranslator)."""
|
| 127 |
try:
|
| 128 |
return GoogleTranslator(source="auto", target=target_lang).translate(text)
|
| 129 |
except Exception:
|
|
@@ -131,14 +109,12 @@ def translate_text(text: str, target_lang: str) -> str:
|
|
| 131 |
|
| 132 |
# ---------- Helpers ----------
|
| 133 |
def detect_language(question: str) -> str:
|
| 134 |
-
"""Detect the user's language without translation."""
|
| 135 |
try:
|
| 136 |
return detect(question)
|
| 137 |
except Exception:
|
| 138 |
return "unknown"
|
| 139 |
|
| 140 |
def normalize_language(lang: str, text: str) -> str:
|
| 141 |
-
"""Fix incorrect language detection like 'wer is' → German."""
|
| 142 |
if lang == "nl" and any(
|
| 143 |
word in text.lower() for word in ["wer", "was", "wie", "javid", "coaching"]
|
| 144 |
):
|
|
@@ -202,15 +178,13 @@ def ask():
|
|
| 202 |
|
| 203 |
print(f"\n--- User Question ---\n{question}")
|
| 204 |
|
| 205 |
-
# Detect and normalize language
|
| 206 |
user_lang = normalize_language(detect_language(question), question)
|
| 207 |
print(f"Detected language: {user_lang}")
|
| 208 |
|
| 209 |
-
# Retrieve context
|
| 210 |
context, results = "", []
|
| 211 |
try:
|
| 212 |
raw_results = retriever.retrieve(question)
|
| 213 |
-
MIN_SCORE = 0.10
|
| 214 |
results = [r for r in raw_results if r.get("score", 0) >= MIN_SCORE]
|
| 215 |
if results:
|
| 216 |
context = "\n\n---\n\n".join(
|
|
@@ -220,7 +194,6 @@ def ask():
|
|
| 220 |
traceback.print_exc()
|
| 221 |
return jsonify(format_answers(question, f"Retriever error: {e}", [])), 200
|
| 222 |
|
| 223 |
-
# Build prompts
|
| 224 |
if context:
|
| 225 |
sys_prompt = system_prompt_book_only()
|
| 226 |
user_content = f"Question: {question}\n\nBook context:\n{context}"
|
|
@@ -228,17 +201,31 @@ def ask():
|
|
| 228 |
sys_prompt = system_prompt_fallback()
|
| 229 |
user_content = question
|
| 230 |
|
| 231 |
-
# Query
|
| 232 |
try:
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
except Exception as e:
|
| 243 |
traceback.print_exc()
|
| 244 |
return jsonify(format_answers(question, f"⚠️ OpenAI call failed: {e}", [])), 200
|
|
|
|
| 10 |
|
| 11 |
# ---------- Fix OpenAI version issue ----------
|
| 12 |
try:
|
|
|
|
| 13 |
subprocess.run(
|
| 14 |
["pip", "install", "--no-cache-dir", "--upgrade", "openai==1.12.0"],
|
| 15 |
check=True,
|
| 16 |
)
|
| 17 |
import openai
|
| 18 |
importlib.reload(openai)
|
|
|
|
| 19 |
print("✅ Using OpenAI SDK version 1.12.0")
|
| 20 |
except Exception as e:
|
| 21 |
print("⚠️ Could not ensure correct OpenAI version:", e)
|
| 22 |
+
import openai
|
| 23 |
|
| 24 |
+
# ---------- Patch huggingface_hub dynamically ----------
|
| 25 |
try:
|
| 26 |
import huggingface_hub
|
| 27 |
if not hasattr(huggingface_hub, "cached_download"):
|
| 28 |
+
print("⚙️ Adding compatibility patch for huggingface_hub.cached_download()")
|
| 29 |
+
huggingface_hub.cached_download = huggingface_hub.hf_hub_download
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
+
print("⚠️ Could not patch huggingface_hub:", e)
|
| 32 |
|
| 33 |
from sentence_transformers import SentenceTransformer
|
| 34 |
from pinecone import Pinecone
|
| 35 |
|
| 36 |
# ---------- Config ----------
|
| 37 |
DATASET_PATH = "data/coaching_millionaer_dataset.json"
|
|
|
|
| 38 |
load_dotenv(override=True)
|
| 39 |
|
|
|
|
| 40 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") or os.environ.get("OPENAI_API_KEY")
|
| 41 |
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY") or os.environ.get("PINECONE_API_KEY")
|
| 42 |
|
|
|
|
| 47 |
CORS(app, resources={r"/ask": {"origins": "*"}})
|
| 48 |
|
| 49 |
# ---------- OpenAI Client ----------
|
|
|
|
|
|
|
| 50 |
client = None
|
| 51 |
try:
|
| 52 |
if not OPENAI_API_KEY or OPENAI_API_KEY.strip() == "":
|
| 53 |
raise ValueError("Missing OPENAI_API_KEY in environment variables")
|
| 54 |
|
| 55 |
+
# Always use global API style (avoids proxies bug)
|
| 56 |
+
import openai
|
| 57 |
+
openai.api_key = OPENAI_API_KEY
|
| 58 |
+
client = openai
|
| 59 |
+
print("✅ OpenAI client initialized using legacy API style.")
|
| 60 |
+
print(f"✅ Using OpenAI SDK version {openai.__version__}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
client = None
|
| 64 |
print(f"❌ Failed to initialize OpenAI client: {e}")
|
| 65 |
|
|
|
|
| 66 |
# ---------- Retriever ----------
|
| 67 |
retriever = None
|
| 68 |
try:
|
|
|
|
| 102 |
|
| 103 |
# ---------- Translator ----------
|
| 104 |
def translate_text(text: str, target_lang: str) -> str:
|
|
|
|
| 105 |
try:
|
| 106 |
return GoogleTranslator(source="auto", target=target_lang).translate(text)
|
| 107 |
except Exception:
|
|
|
|
| 109 |
|
| 110 |
# ---------- Helpers ----------
|
| 111 |
def detect_language(question: str) -> str:
|
|
|
|
| 112 |
try:
|
| 113 |
return detect(question)
|
| 114 |
except Exception:
|
| 115 |
return "unknown"
|
| 116 |
|
| 117 |
def normalize_language(lang: str, text: str) -> str:
|
|
|
|
| 118 |
if lang == "nl" and any(
|
| 119 |
word in text.lower() for word in ["wer", "was", "wie", "javid", "coaching"]
|
| 120 |
):
|
|
|
|
| 178 |
|
| 179 |
print(f"\n--- User Question ---\n{question}")
|
| 180 |
|
|
|
|
| 181 |
user_lang = normalize_language(detect_language(question), question)
|
| 182 |
print(f"Detected language: {user_lang}")
|
| 183 |
|
|
|
|
| 184 |
context, results = "", []
|
| 185 |
try:
|
| 186 |
raw_results = retriever.retrieve(question)
|
| 187 |
+
MIN_SCORE = 0.10
|
| 188 |
results = [r for r in raw_results if r.get("score", 0) >= MIN_SCORE]
|
| 189 |
if results:
|
| 190 |
context = "\n\n---\n\n".join(
|
|
|
|
| 194 |
traceback.print_exc()
|
| 195 |
return jsonify(format_answers(question, f"Retriever error: {e}", [])), 200
|
| 196 |
|
|
|
|
| 197 |
if context:
|
| 198 |
sys_prompt = system_prompt_book_only()
|
| 199 |
user_content = f"Question: {question}\n\nBook context:\n{context}"
|
|
|
|
| 201 |
sys_prompt = system_prompt_fallback()
|
| 202 |
user_content = question
|
| 203 |
|
| 204 |
+
# ---------- OpenAI Query ----------
|
| 205 |
try:
|
| 206 |
+
if hasattr(client, "ChatCompletion"):
|
| 207 |
+
# Legacy API (works in SDK <=1.12.0)
|
| 208 |
+
response = client.ChatCompletion.create(
|
| 209 |
+
model="gpt-4o-mini",
|
| 210 |
+
messages=[
|
| 211 |
+
{"role": "system", "content": sys_prompt},
|
| 212 |
+
{"role": "user", "content": user_content},
|
| 213 |
+
],
|
| 214 |
+
max_tokens=700,
|
| 215 |
+
)
|
| 216 |
+
answer = response["choices"][0]["message"]["content"].strip()
|
| 217 |
+
else:
|
| 218 |
+
# New API (for future SDKs)
|
| 219 |
+
response = client.chat.completions.create(
|
| 220 |
+
model="gpt-4o-mini",
|
| 221 |
+
messages=[
|
| 222 |
+
{"role": "system", "content": sys_prompt},
|
| 223 |
+
{"role": "user", "content": user_content},
|
| 224 |
+
],
|
| 225 |
+
max_tokens=700,
|
| 226 |
+
)
|
| 227 |
+
answer = response.choices[0].message.content.strip()
|
| 228 |
+
|
| 229 |
except Exception as e:
|
| 230 |
traceback.print_exc()
|
| 231 |
return jsonify(format_answers(question, f"⚠️ OpenAI call failed: {e}", [])), 200
|