Mahmous commited on
Commit
9189233
·
verified ·
1 Parent(s): de75945

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +23 -4
api.py CHANGED
@@ -5,7 +5,20 @@ from flask_cors import CORS
5
  from dotenv import load_dotenv
6
  from openai import OpenAI
7
  from langdetect import detect
8
- from googletrans import Translator
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  from sentence_transformers import SentenceTransformer
10
  from pinecone import Pinecone
11
 
@@ -14,7 +27,7 @@ DATASET_PATH = "data/coaching_millionaer_dataset.json"
14
  load_dotenv()
15
 
16
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
17
- PINECONE_API_KEY = os.getenv("PINECONE_API_KEY") # add this to your .env
18
  PINECONE_INDEX_NAME = "ebook"
19
 
20
  # ---------- App ----------
@@ -63,7 +76,13 @@ except Exception as e:
63
  print("❌ Retriever initialization failed:", e)
64
  traceback.print_exc()
65
 
66
- translator = Translator()
 
 
 
 
 
 
67
 
68
  # ---------- Helpers ----------
69
  def detect_language(question: str) -> str:
@@ -175,6 +194,6 @@ def ask():
175
 
176
  # ---------- Run ----------
177
  if __name__ == "__main__":
178
- port = int(os.environ.get("PORT", 5000))
179
  print(f"🚀 Server started on port {port}")
180
  app.run(host="0.0.0.0", port=port)
 
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
 
 
27
  load_dotenv()
28
 
29
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
30
+ PINECONE_API_KEY = os.getenv("PINECONE_API_KEY") # Add this to your .env
31
  PINECONE_INDEX_NAME = "ebook"
32
 
33
  # ---------- App ----------
 
76
  print("❌ Retriever initialization failed:", e)
77
  traceback.print_exc()
78
 
79
+ # ---------- Translator ----------
80
+ def translate_text(text: str, target_lang: str) -> str:
81
+ """Translate text using deep-translator (GoogleTranslator)."""
82
+ try:
83
+ return GoogleTranslator(source="auto", target=target_lang).translate(text)
84
+ except Exception:
85
+ return text
86
 
87
  # ---------- Helpers ----------
88
  def detect_language(question: str) -> str:
 
194
 
195
  # ---------- Run ----------
196
  if __name__ == "__main__":
197
+ port = int(os.environ.get("PORT", 7860))
198
  print(f"🚀 Server started on port {port}")
199
  app.run(host="0.0.0.0", port=port)