Mahmous commited on
Commit
92b13cd
·
verified ·
1 Parent(s): 7e04d51

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +28 -25
api.py CHANGED
@@ -7,44 +7,47 @@ from langdetect import detect
7
  from deep_translator import GoogleTranslator
8
  from sentence_transformers import SentenceTransformer
9
  from pinecone import Pinecone
 
10
 
11
  # ---------- Config ----------
12
  DATASET_PATH = "data/coaching_millionaer_dataset.json"
13
  load_dotenv(override=True)
14
 
15
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") or os.environ.get("OPENAI_API_KEY")
16
- PINECONE_API_KEY = os.getenv("PINECONE_API_KEY") or os.environ.get("PINECONE_API_KEY")
 
 
17
  PINECONE_INDEX_NAME = "ebook"
18
 
19
  # ---------- App ----------
20
  app = Flask(__name__)
21
  CORS(app, resources={r"/ask": {"origins": "*"}})
22
 
23
- # ---------- OpenAI Client ----------
24
- from openai import OpenAI
25
- import inspect
26
-
27
  client = None
28
  try:
29
- if not OPENAI_API_KEY or OPENAI_API_KEY.strip() == "":
30
- raise ValueError("Missing OPENAI_API_KEY in environment variables")
31
-
32
- # Handle Hugging Face’s automatic proxy injection safely
33
- init_params = {"api_key": OPENAI_API_KEY}
34
- if "proxies" in inspect.signature(OpenAI.__init__).parameters:
35
- init_params["proxies"] = None
36
-
37
- client = OpenAI(**init_params)
38
- print(f"✅ Using OpenAI SDK version {client.__module__.split('.')[0]} (modern client)")
 
 
 
39
  except Exception as e:
 
40
  client = None
41
- print(f"❌ Failed to initialize OpenAI client: {e}")
42
 
43
  # ---------- Retriever ----------
44
  retriever = None
45
  try:
46
  if not PINECONE_API_KEY:
47
- raise ValueError("PINECONE_API_KEY missing in .env")
48
 
49
  pc = Pinecone(api_key=PINECONE_API_KEY)
50
  index = pc.Index(PINECONE_INDEX_NAME)
@@ -105,14 +108,14 @@ def system_prompt_book_only() -> str:
105
  "If the user asks about people like Javid Niazi-Hoffmann, describe them factually using the book content. "
106
  "Mention page numbers where possible. "
107
  "If the context is not relevant, say you don’t have that information in the book and provide a general, helpful answer. "
108
- "Always respond in the same language as the user's question, even if the book content is in another language."
109
  )
110
 
111
  def system_prompt_fallback() -> str:
112
  return (
113
  "You are CoachingBot, a helpful business and life mentor. "
114
  "The question cannot be answered from the book, so answer using your general coaching knowledge. "
115
- "Always respond in the same language as the user's question, even if the book content is in another language. "
116
  "Do not invent book citations."
117
  )
118
 
@@ -138,7 +141,7 @@ def health():
138
  {
139
  "status": "running",
140
  "retriever_ready": bool(retriever),
141
- "openai_key_loaded": bool(OPENAI_API_KEY),
142
  "pinecone_key_loaded": bool(PINECONE_API_KEY),
143
  "index_name": PINECONE_INDEX_NAME,
144
  }
@@ -185,13 +188,13 @@ def ask():
185
  sys_prompt = system_prompt_fallback()
186
  user_content = question
187
 
188
- # ---------- OpenAI Query ----------
189
  if client is None:
190
- return jsonify(format_answers(question, "⚠️ OpenAI client not initialized.", results)), 200
191
 
 
192
  try:
193
  response = client.chat.completions.create(
194
- model="gpt-4o-mini",
195
  messages=[
196
  {"role": "system", "content": sys_prompt},
197
  {"role": "user", "content": user_content},
@@ -201,7 +204,7 @@ def ask():
201
  answer = response.choices[0].message.content.strip()
202
  except Exception as e:
203
  traceback.print_exc()
204
- return jsonify(format_answers(question, f"⚠️ OpenAI call failed: {e}", results)), 200
205
 
206
  return jsonify(format_answers(question, answer, results))
207
 
 
7
  from deep_translator import GoogleTranslator
8
  from sentence_transformers import SentenceTransformer
9
  from pinecone import Pinecone
10
+ from openai import OpenAI
11
 
12
  # ---------- Config ----------
13
  DATASET_PATH = "data/coaching_millionaer_dataset.json"
14
  load_dotenv(override=True)
15
 
16
+ # Load secrets from Hugging Face Space
17
+ HF_TOKEN = os.getenv("HF_TOKEN")
18
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
19
+ PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
20
  PINECONE_INDEX_NAME = "ebook"
21
 
22
  # ---------- App ----------
23
  app = Flask(__name__)
24
  CORS(app, resources={r"/ask": {"origins": "*"}})
25
 
26
+ # ---------- LLM Client ----------
 
 
 
27
  client = None
28
  try:
29
+ if HF_TOKEN:
30
+ # Use Hugging Face Inference Provider
31
+ client = OpenAI(
32
+ base_url="https://router.huggingface.co/v1",
33
+ api_key=HF_TOKEN,
34
+ )
35
+ print(" Using Hugging Face Inference Provider (OpenAI-compatible API)")
36
+ elif OPENAI_API_KEY:
37
+ # Fallback to OpenAI if provided
38
+ client = OpenAI(api_key=OPENAI_API_KEY)
39
+ print("✅ Using OpenAI client directly")
40
+ else:
41
+ raise ValueError("No valid API key found. Set HF_TOKEN or OPENAI_API_KEY.")
42
  except Exception as e:
43
+ print(f"❌ Failed to initialize LLM client: {e}")
44
  client = None
 
45
 
46
  # ---------- Retriever ----------
47
  retriever = None
48
  try:
49
  if not PINECONE_API_KEY:
50
+ raise ValueError("PINECONE_API_KEY missing in environment variables")
51
 
52
  pc = Pinecone(api_key=PINECONE_API_KEY)
53
  index = pc.Index(PINECONE_INDEX_NAME)
 
108
  "If the user asks about people like Javid Niazi-Hoffmann, describe them factually using the book content. "
109
  "Mention page numbers where possible. "
110
  "If the context is not relevant, say you don’t have that information in the book and provide a general, helpful answer. "
111
+ "Always respond in the same language as the user's question."
112
  )
113
 
114
  def system_prompt_fallback() -> str:
115
  return (
116
  "You are CoachingBot, a helpful business and life mentor. "
117
  "The question cannot be answered from the book, so answer using your general coaching knowledge. "
118
+ "Always respond in the same language as the user's question. "
119
  "Do not invent book citations."
120
  )
121
 
 
141
  {
142
  "status": "running",
143
  "retriever_ready": bool(retriever),
144
+ "hf_key_loaded": bool(HF_TOKEN),
145
  "pinecone_key_loaded": bool(PINECONE_API_KEY),
146
  "index_name": PINECONE_INDEX_NAME,
147
  }
 
188
  sys_prompt = system_prompt_fallback()
189
  user_content = question
190
 
 
191
  if client is None:
192
+ return jsonify(format_answers(question, "⚠️ No language model initialized.", results)), 200
193
 
194
+ # ---------- LLM Query ----------
195
  try:
196
  response = client.chat.completions.create(
197
+ model="openai/gpt-oss-120b:cerebras", # Hugging Face model
198
  messages=[
199
  {"role": "system", "content": sys_prompt},
200
  {"role": "user", "content": user_content},
 
204
  answer = response.choices[0].message.content.strip()
205
  except Exception as e:
206
  traceback.print_exc()
207
+ return jsonify(format_answers(question, f"⚠️ LLM call failed: {e}", results)), 200
208
 
209
  return jsonify(format_answers(question, answer, results))
210