Sync from GitHub Actions: c7cceacf14c15ea3744077704d4a1b0dd12b3b11
Browse files- api/core/chatbot.py +16 -8
- api/core/nlp_handler.py +12 -9
- api/requirements.txt +1 -1
- package-lock.json +15 -0
api/core/chatbot.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# api/core/chatbot.py
|
| 2 |
import os
|
| 3 |
-
|
| 4 |
from .nlp_handler import MBTI_EXPLANATIONS
|
| 5 |
|
| 6 |
class MBTIChatbot:
|
|
@@ -12,14 +12,16 @@ class MBTIChatbot:
|
|
| 12 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 13 |
if not api_key:
|
| 14 |
print("[WARN] GEMINI_API_KEY not found in .env.")
|
|
|
|
| 15 |
else:
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
except Exception:
|
| 21 |
-
print("[WARN] 2.0 Flash failed, fallback to Lite")
|
| 22 |
-
self.model = genai.GenerativeModel('gemini-2.0-flash-lite')
|
| 23 |
|
| 24 |
def generate_response(self, user_query, lang="en"):
|
| 25 |
lang_instruction = "Answer in English Slang." if lang == "en" else "Jawab dalam Bahasa Indonesia gaul (Slang Jakarta/Lo-Gue), maskulin, santai, dan to the point. Panggil user 'bro' atau 'bre'. JANGAN panggil 'bestie', 'kak', atau 'gan'. Gaya bicara tongkrongan cowok tapi tetap edukatif soal MBTI."
|
|
@@ -51,7 +53,13 @@ INSTRUCTIONS:
|
|
| 51 |
"""
|
| 52 |
|
| 53 |
try:
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
return response.text
|
| 56 |
except Exception as e:
|
| 57 |
return f"Maaf, saya sedang mengalami gangguan koneksi ke otak AI saya. (Error: {str(e)})"
|
|
|
|
| 1 |
# api/core/chatbot.py
|
| 2 |
import os
|
| 3 |
+
from google import genai
|
| 4 |
from .nlp_handler import MBTI_EXPLANATIONS
|
| 5 |
|
| 6 |
class MBTIChatbot:
|
|
|
|
| 12 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 13 |
if not api_key:
|
| 14 |
print("[WARN] GEMINI_API_KEY not found in .env.")
|
| 15 |
+
self.client = None
|
| 16 |
else:
|
| 17 |
+
try:
|
| 18 |
+
self.client = genai.Client(api_key=api_key)
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"[ERR] Gemini Client Init Failed: {e}")
|
| 21 |
+
self.client = None
|
| 22 |
|
| 23 |
+
# Model Configuration
|
| 24 |
+
self.model_name = 'gemini-2.0-flash'
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def generate_response(self, user_query, lang="en"):
|
| 27 |
lang_instruction = "Answer in English Slang." if lang == "en" else "Jawab dalam Bahasa Indonesia gaul (Slang Jakarta/Lo-Gue), maskulin, santai, dan to the point. Panggil user 'bro' atau 'bre'. JANGAN panggil 'bestie', 'kak', atau 'gan'. Gaya bicara tongkrongan cowok tapi tetap edukatif soal MBTI."
|
|
|
|
| 53 |
"""
|
| 54 |
|
| 55 |
try:
|
| 56 |
+
if not self.client:
|
| 57 |
+
return "Maaf, kunci otak saya (API Key) belum dipasang atau salah."
|
| 58 |
+
|
| 59 |
+
response = self.client.models.generate_content(
|
| 60 |
+
model=self.model_name,
|
| 61 |
+
contents=system_prompt
|
| 62 |
+
)
|
| 63 |
return response.text
|
| 64 |
except Exception as e:
|
| 65 |
return f"Maaf, saya sedang mengalami gangguan koneksi ke otak AI saya. (Error: {str(e)})"
|
api/core/nlp_handler.py
CHANGED
|
@@ -6,7 +6,7 @@ import numpy as np
|
|
| 6 |
import html
|
| 7 |
from deep_translator import GoogleTranslator
|
| 8 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 9 |
-
|
| 10 |
import time
|
| 11 |
|
| 12 |
# --- CONFIG PATH ---
|
|
@@ -106,21 +106,21 @@ class NLPHandler:
|
|
| 106 |
except Exception as e: print(f"Emotion 2 Load Error: {e}")
|
| 107 |
|
| 108 |
# --- GEMINI VALIDATOR SETUP ---
|
| 109 |
-
|
| 110 |
|
| 111 |
@staticmethod
|
| 112 |
def _init_gemini():
|
| 113 |
-
"""Initialize Gemini
|
| 114 |
-
if NLPHandler.
|
| 115 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 116 |
if api_key:
|
| 117 |
try:
|
| 118 |
-
genai
|
| 119 |
-
NLPHandler.
|
| 120 |
-
print("Gemini Validator Ready")
|
| 121 |
except Exception as e:
|
| 122 |
print(f"Gemini Init Failed: {e}")
|
| 123 |
-
return NLPHandler.
|
| 124 |
|
| 125 |
@staticmethod
|
| 126 |
def _validate_with_gemini(text, ml_prediction):
|
|
@@ -174,7 +174,10 @@ REASON: Explicit mentions of networking, leading teams, and structured planning
|
|
| 174 |
"""
|
| 175 |
|
| 176 |
try:
|
| 177 |
-
response = NLPHandler.
|
|
|
|
|
|
|
|
|
|
| 178 |
result_text = response.text.strip()
|
| 179 |
|
| 180 |
# Parse response
|
|
|
|
| 6 |
import html
|
| 7 |
from deep_translator import GoogleTranslator
|
| 8 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 9 |
+
|
| 10 |
import time
|
| 11 |
|
| 12 |
# --- CONFIG PATH ---
|
|
|
|
| 106 |
except Exception as e: print(f"Emotion 2 Load Error: {e}")
|
| 107 |
|
| 108 |
# --- GEMINI VALIDATOR SETUP ---
|
| 109 |
+
_gemini_client = None
|
| 110 |
|
| 111 |
@staticmethod
|
| 112 |
def _init_gemini():
|
| 113 |
+
"""Initialize Gemini Client for validation (lazy loading)"""
|
| 114 |
+
if NLPHandler._gemini_client is None:
|
| 115 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 116 |
if api_key:
|
| 117 |
try:
|
| 118 |
+
from google import genai
|
| 119 |
+
NLPHandler._gemini_client = genai.Client(api_key=api_key)
|
| 120 |
+
print("Gemini Validator Ready (google-genai SDK)")
|
| 121 |
except Exception as e:
|
| 122 |
print(f"Gemini Init Failed: {e}")
|
| 123 |
+
return NLPHandler._gemini_client is not None
|
| 124 |
|
| 125 |
@staticmethod
|
| 126 |
def _validate_with_gemini(text, ml_prediction):
|
|
|
|
| 174 |
"""
|
| 175 |
|
| 176 |
try:
|
| 177 |
+
response = NLPHandler._gemini_client.models.generate_content(
|
| 178 |
+
model='gemini-2.0-flash',
|
| 179 |
+
contents=prompt
|
| 180 |
+
)
|
| 181 |
result_text = response.text.strip()
|
| 182 |
|
| 183 |
# Parse response
|
api/requirements.txt
CHANGED
|
@@ -8,6 +8,6 @@ joblib
|
|
| 8 |
deep-translator
|
| 9 |
requests
|
| 10 |
youtube-transcript-api
|
| 11 |
-
google-
|
| 12 |
transformers
|
| 13 |
torch
|
|
|
|
| 8 |
deep-translator
|
| 9 |
requests
|
| 10 |
youtube-transcript-api
|
| 11 |
+
google-genai
|
| 12 |
transformers
|
| 13 |
torch
|
package-lock.json
CHANGED
|
@@ -8162,6 +8162,21 @@
|
|
| 8162 |
"type": "github",
|
| 8163 |
"url": "https://github.com/sponsors/wooorm"
|
| 8164 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8165 |
}
|
| 8166 |
}
|
| 8167 |
}
|
|
|
|
| 8162 |
"type": "github",
|
| 8163 |
"url": "https://github.com/sponsors/wooorm"
|
| 8164 |
}
|
| 8165 |
+
},
|
| 8166 |
+
"node_modules/@next/swc-win32-x64-msvc": {
|
| 8167 |
+
"version": "16.1.0",
|
| 8168 |
+
"resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-16.1.0.tgz",
|
| 8169 |
+
"integrity": "sha512-Tr0j94MphimCCks+1rtYPzQFK+faJuhHWCegU9S9gDlgyOk8Y3kPmO64UcjyzZAlligeBtYZ/2bEyrKq0d2wqQ==",
|
| 8170 |
+
"cpu": [
|
| 8171 |
+
"x64"
|
| 8172 |
+
],
|
| 8173 |
+
"optional": true,
|
| 8174 |
+
"os": [
|
| 8175 |
+
"win32"
|
| 8176 |
+
],
|
| 8177 |
+
"engines": {
|
| 8178 |
+
"node": ">= 10"
|
| 8179 |
+
}
|
| 8180 |
}
|
| 8181 |
}
|
| 8182 |
}
|