Mahmous commited on
Commit
01a4b04
·
verified ·
1 Parent(s): e473cf9

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +57 -20
api.py CHANGED
@@ -1,24 +1,47 @@
1
  import os
2
  import traceback
 
 
3
  from flask import Flask, request, jsonify
4
  from flask_cors import CORS
5
  from dotenv import load_dotenv
6
- from openai import OpenAI
7
  from langdetect import detect
8
  from deep_translator import GoogleTranslator
9
- import subprocess
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  # Patch huggingface_hub automatically if Gradio overwrote it
12
  try:
13
  import huggingface_hub
14
  if not hasattr(huggingface_hub, "cached_download"):
15
  subprocess.run(
16
- ["pip", "install", "--no-cache-dir", "huggingface-hub==0.24.5", "transformers==4.30.2", "sentence-transformers==2.2.2"],
17
- check=True
 
 
 
 
 
 
 
18
  )
19
  print("✅ Downgraded huggingface-hub for sentence-transformers compatibility.")
20
  except Exception as e:
21
  print("⚠️ Could not auto-patch huggingface_hub:", e)
 
22
  from sentence_transformers import SentenceTransformer
23
  from pinecone import Pinecone
24
 
@@ -37,7 +60,11 @@ CORS(app, resources={r"/ask": {"origins": "*"}})
37
  # ---------- OpenAI Client ----------
38
  client = None
39
  if OPENAI_API_KEY:
40
- client = OpenAI(api_key=OPENAI_API_KEY)
 
 
 
 
41
  else:
42
  print("⚠️ OPENAI_API_KEY is missing in .env")
43
 
@@ -63,11 +90,13 @@ try:
63
  results = []
64
  for match in matches:
65
  meta = match.get("metadata", {})
66
- results.append({
67
- "context": meta.get("context", ""),
68
- "page": meta.get("page"),
69
- "score": match.get("score", 0)
70
- })
 
 
71
  return results
72
 
73
  retriever = PineconeRetriever(index, embedder)
@@ -94,7 +123,9 @@ def detect_language(question: str) -> str:
94
 
95
  def normalize_language(lang: str, text: str) -> str:
96
  """Fix incorrect language detection like 'wer is' → German."""
97
- if lang == "nl" and any(word in text.lower() for word in ["wer", "was", "wie", "javid", "coaching"]):
 
 
98
  return "de"
99
  return lang
100
 
@@ -120,18 +151,24 @@ def format_answers(question: str, answer: str, results):
120
  pages = [f"Seite {r.get('page', '')}" for r in results if r.get("page")]
121
  source = ", ".join(pages) if pages else "No source"
122
  top_score = max([r.get("score", 0.0) for r in results], default=0.0)
123
- return {"answers": [{"question": question, "answer": answer, "source": source, "bm25_score": top_score}]}
 
 
 
 
124
 
125
  # ---------- Routes ----------
126
  @app.route("/", methods=["GET"])
127
  def health():
128
- return jsonify({
129
- "status": "running",
130
- "retriever_ready": bool(retriever),
131
- "openai_key_loaded": bool(OPENAI_API_KEY),
132
- "pinecone_key_loaded": bool(PINECONE_API_KEY),
133
- "index_name": PINECONE_INDEX_NAME
134
- })
 
 
135
 
136
  @app.route("/ask", methods=["POST", "OPTIONS"])
137
  def ask():
@@ -181,7 +218,7 @@ def ask():
181
  model="gpt-4o-mini",
182
  messages=[
183
  {"role": "system", "content": sys_prompt},
184
- {"role": "user", "content": user_content}
185
  ],
186
  max_tokens=700,
187
  )
 
1
  import os
2
  import traceback
3
+ import subprocess
4
+ import importlib
5
  from flask import Flask, request, jsonify
6
  from flask_cors import CORS
7
  from dotenv import load_dotenv
 
8
  from langdetect import detect
9
  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
+ from openai import OpenAI # fallback import
25
 
26
  # Patch huggingface_hub automatically if Gradio overwrote it
27
  try:
28
  import huggingface_hub
29
  if not hasattr(huggingface_hub, "cached_download"):
30
  subprocess.run(
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 auto-patch huggingface_hub:", e)
44
+
45
  from sentence_transformers import SentenceTransformer
46
  from pinecone import Pinecone
47
 
 
60
  # ---------- OpenAI Client ----------
61
  client = None
62
  if OPENAI_API_KEY:
63
+ try:
64
+ client = OpenAI(api_key=OPENAI_API_KEY)
65
+ print("✅ OpenAI client initialized successfully.")
66
+ except Exception as e:
67
+ print("❌ Failed to initialize OpenAI client:", e)
68
  else:
69
  print("⚠️ OPENAI_API_KEY is missing in .env")
70
 
 
90
  results = []
91
  for match in matches:
92
  meta = match.get("metadata", {})
93
+ results.append(
94
+ {
95
+ "context": meta.get("context", ""),
96
+ "page": meta.get("page"),
97
+ "score": match.get("score", 0),
98
+ }
99
+ )
100
  return results
101
 
102
  retriever = PineconeRetriever(index, embedder)
 
123
 
124
  def normalize_language(lang: str, text: str) -> str:
125
  """Fix incorrect language detection like 'wer is' → German."""
126
+ if lang == "nl" and any(
127
+ word in text.lower() for word in ["wer", "was", "wie", "javid", "coaching"]
128
+ ):
129
  return "de"
130
  return lang
131
 
 
151
  pages = [f"Seite {r.get('page', '')}" for r in results if r.get("page")]
152
  source = ", ".join(pages) if pages else "No source"
153
  top_score = max([r.get("score", 0.0) for r in results], default=0.0)
154
+ return {
155
+ "answers": [
156
+ {"question": question, "answer": answer, "source": source, "bm25_score": top_score}
157
+ ]
158
+ }
159
 
160
  # ---------- Routes ----------
161
  @app.route("/", methods=["GET"])
162
  def health():
163
+ return jsonify(
164
+ {
165
+ "status": "running",
166
+ "retriever_ready": bool(retriever),
167
+ "openai_key_loaded": bool(OPENAI_API_KEY),
168
+ "pinecone_key_loaded": bool(PINECONE_API_KEY),
169
+ "index_name": PINECONE_INDEX_NAME,
170
+ }
171
+ )
172
 
173
  @app.route("/ask", methods=["POST", "OPTIONS"])
174
  def ask():
 
218
  model="gpt-4o-mini",
219
  messages=[
220
  {"role": "system", "content": sys_prompt},
221
+ {"role": "user", "content": user_content},
222
  ],
223
  max_tokens=700,
224
  )