AnesKAM commited on
Commit
d39e2e6
Β·
verified Β·
1 Parent(s): bf8d887

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -1
app.py CHANGED
@@ -1,12 +1,116 @@
1
- from flask import Flask, send_from_directory
 
2
  import os
 
 
 
3
 
4
  app = Flask(__name__, static_folder='.')
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  @app.route('/')
7
  def index():
8
  return send_from_directory('.', 'index.html')
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  @app.route('/<path:path>')
11
  def static_files(path):
12
  return send_from_directory('.', path)
 
1
+ from flask import Flask, send_from_directory, request, jsonify
2
+ import requests
3
  import os
4
+ import json
5
+ import base64
6
+ from datetime import datetime, timezone
7
 
8
  app = Flask(__name__, static_folder='.')
9
 
10
+ TAVILY_KEY = os.environ.get("TAVILY_KEY", "")
11
+ HF_TOKEN = os.environ.get("HF_TOKEN", "")
12
+ HF_DATASET = os.environ.get("HF_DATASET", "") # e.g. "AnesNT/surfgo-cache"
13
+
14
+ # ─── HuggingFace Dataset helpers ────────────────────────────────────────────
15
+
16
+ def hf_headers():
17
+ return {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
18
+
19
+ def hf_get_cache():
20
+ """Download cache.json from HF dataset. Returns dict."""
21
+ url = f"https://huggingface.co/datasets/{HF_DATASET}/resolve/main/cache.json"
22
+ r = requests.get(url, headers={"Authorization": f"Bearer {HF_TOKEN}"}, timeout=10)
23
+ if r.status_code == 200:
24
+ return r.json()
25
+ return {}
26
+
27
+ def hf_put_cache(cache: dict):
28
+ """Upload updated cache.json to HF dataset."""
29
+ content = json.dumps(cache, ensure_ascii=False, indent=2)
30
+ encoded = base64.b64encode(content.encode()).decode()
31
+
32
+ # Get current file SHA for update
33
+ sha = None
34
+ r = requests.get(
35
+ f"https://huggingface.co/api/datasets/{HF_DATASET}/tree/main",
36
+ headers=hf_headers(), timeout=10
37
+ )
38
+ if r.status_code == 200:
39
+ for f in r.json():
40
+ if f.get("path") == "cache.json":
41
+ sha = f.get("oid")
42
+ break
43
+
44
+ payload = {
45
+ "message": f"surfgo cache β€” {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M UTC')}",
46
+ "content": encoded,
47
+ }
48
+ if sha:
49
+ payload["sha"] = sha
50
+
51
+ requests.put(
52
+ f"https://huggingface.co/api/datasets/{HF_DATASET}/upload/main/cache.json",
53
+ headers=hf_headers(), json=payload, timeout=15
54
+ )
55
+
56
+ def normalize(q: str) -> str:
57
+ return q.strip().lower()
58
+
59
+ # ─── Routes ─────────────────────────────────────────────────────────────────
60
+
61
  @app.route('/')
62
  def index():
63
  return send_from_directory('.', 'index.html')
64
 
65
+ @app.route('/search', methods=['POST'])
66
+ def search():
67
+ if not TAVILY_KEY:
68
+ return jsonify({"error": "TAVILY_KEY not set"}), 500
69
+
70
+ body = request.get_json()
71
+ query = body.get("query", "").strip()
72
+ key = normalize(query)
73
+ use_hf = bool(HF_TOKEN and HF_DATASET)
74
+
75
+ # ── 1. Check cache first ─────────────────────────────────────────────────
76
+ if use_hf:
77
+ try:
78
+ cache = hf_get_cache()
79
+ if key in cache:
80
+ entry = cache[key]
81
+ # Bump hit counter
82
+ cache[key]["hits"] = entry.get("hits", 0) + 1
83
+ try: hf_put_cache(cache)
84
+ except: pass
85
+ result = entry["result"]
86
+ result["_cached"] = True
87
+ result["_hits"] = cache[key]["hits"]
88
+ result["_stored_at"] = entry.get("stored_at", "")
89
+ return jsonify(result)
90
+ except Exception as e:
91
+ print(f"[cache read error] {e}")
92
+
93
+ # ── 2. Cache miss β†’ call Tavily ──────────────────────────────────────────
94
+ payload = {**body, "api_key": TAVILY_KEY}
95
+ resp = requests.post("https://api.tavily.com/search", json=payload, timeout=20)
96
+ data = resp.json()
97
+
98
+ # ── 3. Store new result in HF ────────────────────────────────────────────
99
+ if use_hf and resp.status_code == 200:
100
+ try:
101
+ cache = hf_get_cache()
102
+ cache[key] = {
103
+ "query": query,
104
+ "result": data,
105
+ "hits": 1,
106
+ "stored_at": datetime.now(timezone.utc).isoformat(),
107
+ }
108
+ hf_put_cache(cache)
109
+ except Exception as e:
110
+ print(f"[cache write error] {e}")
111
+
112
+ return jsonify(data), resp.status_code
113
+
114
  @app.route('/<path:path>')
115
  def static_files(path):
116
  return send_from_directory('.', path)