Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,26 @@ import json
|
|
| 6 |
import requests
|
| 7 |
from datetime import datetime, timedelta, timezone
|
| 8 |
from flask import Flask, request, jsonify, Response
|
|
|
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
app.secret_key = os.getenv("FLASK_SECRET_KEY")
|
| 13 |
|
| 14 |
# ==== API KEYS ====
|
|
@@ -110,7 +127,7 @@ def serpapi_search(query: str, location=None, num_results=3):
|
|
| 110 |
lang = "en"
|
| 111 |
search_location = location or ""
|
| 112 |
|
| 113 |
-
url = "https://serpapi.com/search.json"
|
| 114 |
params = {
|
| 115 |
"q": query,
|
| 116 |
"location": search_location,
|
|
@@ -267,9 +284,7 @@ def chat():
|
|
| 267 |
has_keyword = any(k in user_text.lower() for k in keywords)
|
| 268 |
|
| 269 |
if has_keyword:
|
| 270 |
-
|
| 271 |
-
# Saya asumsikan Anda ingin memanggil stream_chat dengan konteks search.
|
| 272 |
-
serp_text = serpapi_search(user_text)
|
| 273 |
user_text_with_search = f"{user_text}\n\n{serp_text}\n\n🧠 Explain this search."
|
| 274 |
print(f"[CHAT] 💬 User Prompt (Voice Mode, with Search): {user_text_with_search[:100]}...")
|
| 275 |
ai = "".join(chunk for chunk in stream_chat(user_text_with_search))
|
|
|
|
| 6 |
import requests
|
| 7 |
from datetime import datetime, timedelta, timezone
|
| 8 |
from flask import Flask, request, jsonify, Response
|
| 9 |
+
from flask_cors import CORS # Import CORS di sini
|
| 10 |
from huggingface_hub import InferenceClient
|
| 11 |
|
| 12 |
app = Flask(__name__)
|
| 13 |
+
|
| 14 |
+
# ==================================
|
| 15 |
+
# 🔒 KONFIGURASI VALIDASI DOMAIN (CORS)
|
| 16 |
+
# Ganti dengan domain website Anda yang sebenarnya!
|
| 17 |
+
# ==================================
|
| 18 |
+
ALLOWED_ORIGINS = [
|
| 19 |
+
"https://talkgte.netlify.app" # Origin
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
# Terapkan CORS ke semua rute ('/*') dan batasi origins-nya.
|
| 23 |
+
CORS(app, resources={r"/*": {"origins": ALLOWED_ORIGINS}})
|
| 24 |
+
|
| 25 |
+
# ==================================
|
| 26 |
+
# Lanjutkan dengan sisa kode Anda
|
| 27 |
+
# ==================================
|
| 28 |
+
|
| 29 |
app.secret_key = os.getenv("FLASK_SECRET_KEY")
|
| 30 |
|
| 31 |
# ==== API KEYS ====
|
|
|
|
| 127 |
lang = "en"
|
| 128 |
search_location = location or ""
|
| 129 |
|
| 130 |
+
url = "[https://serpapi.com/search.json](https://serpapi.com/search.json)"
|
| 131 |
params = {
|
| 132 |
"q": query,
|
| 133 |
"location": search_location,
|
|
|
|
| 284 |
has_keyword = any(k in user_text.lower() for k in keywords)
|
| 285 |
|
| 286 |
if has_keyword:
|
| 287 |
+
serp_text = serpapi_search(user_text)
|
|
|
|
|
|
|
| 288 |
user_text_with_search = f"{user_text}\n\n{serp_text}\n\n🧠 Explain this search."
|
| 289 |
print(f"[CHAT] 💬 User Prompt (Voice Mode, with Search): {user_text_with_search[:100]}...")
|
| 290 |
ai = "".join(chunk for chunk in stream_chat(user_text_with_search))
|