Add files via upload
This commit is contained in:
1 parent
b4d6ae9bc7
commit
363a3bdfbb
22 files changed
+1807
-365
No files matched your search
@@ -0,0 +1,253 @@
|
||||
"""
|
||||
JARVIS plugin — Calorie Counter (webcam vision).
|
||||
|
||||
Hold food up to the camera and ask "how many calories is this?" —
|
||||
JARVIS switches the HUD to the live camera with an animated scan bar,
|
||||
photographs the food, analyzes it with Gemini, speaks a short summary
|
||||
in your language and shows the full nutrition breakdown in the
|
||||
content panel.
|
||||
|
||||
Visuals rely on MainWindow's camera signals (_cam_stream_sig /
|
||||
_cam_frame_sig, present since Mark LI). If they're ever missing the
|
||||
plugin still works — just without the camera view.
|
||||
"""
|
||||
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
PLUGIN = {
|
||||
"name": "calorie_counter",
|
||||
"description": (
|
||||
"Analyzes FOOD through the WEBCAM and reports calories and nutrition "
|
||||
"(carbs, sugar, fiber, protein, fat). Use whenever the user asks about "
|
||||
"the calories or nutritional value of food they are physically holding "
|
||||
"or showing right now — e.g. 'bu elimdeki tabak kaç kalori', 'how many "
|
||||
"calories is this', 'şunun besin değeri ne'. This tool takes its OWN "
|
||||
"camera photo — NEVER use screen_process for food-calorie questions. "
|
||||
"Pass the user's exact spoken words in 'query'."
|
||||
),
|
||||
"parameters": {
|
||||
"type": "OBJECT",
|
||||
"properties": {
|
||||
"query": {
|
||||
"type": "STRING",
|
||||
"description": "The user's exact request, verbatim, in their own language.",
|
||||
}
|
||||
},
|
||||
"required": ["query"],
|
||||
},
|
||||
}
|
||||
|
||||
_MODEL = "gemini-flash-latest"
|
||||
_LIVE_SCAN_SECONDS = 1.8 # live preview before the photo is taken
|
||||
_FPS = 25
|
||||
_ANIM_MAX_SECONDS = 25 # animator safety stop
|
||||
_SCAN_COLOR = (255, 190, 40) # JARVIS cyan-blue (BGR)
|
||||
_SCAN_CORE = (255, 235, 130) # bright core line (BGR)
|
||||
|
||||
|
||||
# ── config helpers (same pattern as actions/web_search.py) ──────────────────
|
||||
|
||||
def _config() -> dict:
|
||||
try:
|
||||
return json.loads(
|
||||
(BASE_DIR / "config" / "api_keys.json").read_text(encoding="utf-8")
|
||||
)
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def _open_camera():
|
||||
import platform
|
||||
try:
|
||||
backend = cv2.CAP_DSHOW if platform.system() == "Windows" else cv2.CAP_ANY
|
||||
except AttributeError:
|
||||
backend = 0
|
||||
cap = cv2.VideoCapture(int(_config().get("camera_index", 0)), backend)
|
||||
if not cap.isOpened():
|
||||
cap = cv2.VideoCapture(0)
|
||||
if not cap.isOpened():
|
||||
return None
|
||||
for _ in range(6): # warm-up frames
|
||||
cap.read()
|
||||
return cap
|
||||
|
||||
|
||||
# ── scan-bar rendering ───────────────────────────────────────────────────────
|
||||
|
||||
def _draw_scan_bar(frame: np.ndarray, phase: float) -> np.ndarray:
|
||||
"""Return a copy of the BGR frame with an animated scan bar sweeping
|
||||
bottom → top → bottom (triangle wave on `phase`)."""
|
||||
h, w = frame.shape[:2]
|
||||
pos = phase % 2.0
|
||||
pos = pos if pos <= 1.0 else 2.0 - pos
|
||||
y = int((1.0 - pos) * (h - 1))
|
||||
band = max(6, h // 14)
|
||||
|
||||
out = frame.copy()
|
||||
y0, y1 = max(0, y - band), min(h, y + band)
|
||||
if y1 > y0:
|
||||
region = out[y0:y1].astype(np.float32)
|
||||
falloff = 1.0 - (np.abs(np.arange(y0, y1) - y).astype(np.float32) / band)
|
||||
alpha = falloff[:, None, None] * 0.55
|
||||
glow = np.empty_like(region)
|
||||
glow[:] = _SCAN_COLOR
|
||||
out[y0:y1] = np.clip(region * (1 - alpha) + glow * alpha, 0, 255).astype(np.uint8)
|
||||
cv2.line(out, (0, y), (w, y), _SCAN_CORE, 2)
|
||||
return out
|
||||
|
||||
|
||||
def _emit_frame(frame_sig, frame: np.ndarray) -> None:
|
||||
ok, buf = cv2.imencode(".jpg", frame, [cv2.IMWRITE_JPEG_QUALITY, 70])
|
||||
if ok:
|
||||
frame_sig.emit(buf.tobytes())
|
||||
|
||||
|
||||
# ── Gemini ───────────────────────────────────────────────────────────────────
|
||||
|
||||
def _analyze(photo: np.ndarray, query: str, api_key: str) -> dict:
|
||||
from google import genai
|
||||
from google.genai import types as gtypes
|
||||
|
||||
# match screen_processor's upload size: max 1280 wide
|
||||
h, w = photo.shape[:2]
|
||||
if w > 1280:
|
||||
photo = cv2.resize(photo, (1280, int(h * 1280 / w)), interpolation=cv2.INTER_AREA)
|
||||
ok, jpg = cv2.imencode(".jpg", photo, [cv2.IMWRITE_JPEG_QUALITY, 85])
|
||||
if not ok:
|
||||
raise RuntimeError("could not encode photo")
|
||||
|
||||
prompt = (
|
||||
"You are a nutrition analysis engine. Look at the food in this photo.\n"
|
||||
f'User\'s request (respond in the SAME language as this request): "{query}"\n'
|
||||
"Return ONLY minified JSON — no markdown fences, no extra text — with keys:\n"
|
||||
' "food": short name of the dish/foods (null if NO food is visible),\n'
|
||||
' "portion": estimated portion size as short text,\n'
|
||||
' "calories_kcal": number (total estimate),\n'
|
||||
' "carbs_g", "sugar_g", "fiber_g", "protein_g", "fat_g": numbers,\n'
|
||||
' "panel_text": multi-line plain-text nutrition breakdown in the user\'s'
|
||||
" language — food name, portion, total calories, then one line per macro"
|
||||
" (carbs, sugar, fiber, protein, fat) with units; if several foods are"
|
||||
" visible add a short per-item calorie list; max 25 lines,\n"
|
||||
' "spoken_summary": 1-2 conversational sentences in the user\'s language'
|
||||
" naming the food and total calories, mentioning it is an estimate."
|
||||
" If no food is visible, politely say so instead."
|
||||
)
|
||||
|
||||
client = genai.Client(api_key=api_key)
|
||||
resp = client.models.generate_content(
|
||||
model=_MODEL,
|
||||
contents=[
|
||||
gtypes.Part.from_bytes(data=jpg.tobytes(), mime_type="image/jpeg"),
|
||||
prompt,
|
||||
],
|
||||
)
|
||||
text = (resp.text or "").strip()
|
||||
|
||||
# tolerate accidental fences / prose around the JSON
|
||||
if "{" in text and "}" in text:
|
||||
text = text[text.find("{"): text.rfind("}") + 1]
|
||||
return json.loads(text)
|
||||
|
||||
|
||||
# ── entry point ──────────────────────────────────────────────────────────────
|
||||
|
||||
def run(parameters: dict, player=None, session_memory=None) -> str:
|
||||
query = (parameters.get("query") or "").strip() or "How many calories is this food?"
|
||||
|
||||
api_key = _config().get("gemini_api_key")
|
||||
if not api_key:
|
||||
return "I can't run the nutrition scan — no API key is configured."
|
||||
|
||||
def _log(msg: str) -> None:
|
||||
if player:
|
||||
try:
|
||||
player.write_log(msg)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# HUD camera signals (graceful if unavailable)
|
||||
win = getattr(player, "_win", None) if player else None
|
||||
frame_sig = getattr(win, "_cam_frame_sig", None)
|
||||
stream_sig = getattr(win, "_cam_stream_sig", None)
|
||||
|
||||
cap = _open_camera()
|
||||
if cap is None:
|
||||
return ("I couldn't access the camera — it may be in use by another "
|
||||
"feature or application.")
|
||||
|
||||
photo = None
|
||||
stop_anim = threading.Event()
|
||||
animator = None
|
||||
view_open = False
|
||||
try:
|
||||
if stream_sig:
|
||||
stream_sig.emit(True) # HUD logo → live camera view
|
||||
view_open = True
|
||||
_log("JARVIS: Nutrition scan started.")
|
||||
|
||||
# Phase 1 — live preview with scan bar (user positions the food)
|
||||
t0 = time.time()
|
||||
last = None
|
||||
while time.time() - t0 < _LIVE_SCAN_SECONDS:
|
||||
ok, frm = cap.read()
|
||||
if ok and frm is not None:
|
||||
last = frm
|
||||
if frame_sig:
|
||||
_emit_frame(frame_sig, _draw_scan_bar(frm, (time.time() - t0) * 1.2))
|
||||
time.sleep(1.0 / _FPS)
|
||||
ok, frm = cap.read()
|
||||
photo = frm if ok and frm is not None else last
|
||||
finally:
|
||||
try:
|
||||
cap.release() # release BEFORE anything else — avoid device conflicts
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if photo is None:
|
||||
if view_open:
|
||||
stream_sig.emit(False)
|
||||
return "I couldn't capture a picture of the food, sorry."
|
||||
|
||||
# Phase 2 — freeze frame, keep the scan bar sweeping while Gemini analyzes
|
||||
if frame_sig:
|
||||
def _animate():
|
||||
a0 = time.time()
|
||||
while (not stop_anim.wait(1.0 / _FPS)
|
||||
and time.time() - a0 < _ANIM_MAX_SECONDS):
|
||||
_emit_frame(frame_sig, _draw_scan_bar(photo, (time.time() - a0) * 1.2))
|
||||
animator = threading.Thread(target=_animate, daemon=True,
|
||||
name="calorie-scan-anim")
|
||||
animator.start()
|
||||
|
||||
try:
|
||||
data = _analyze(photo, query, api_key)
|
||||
except Exception as e:
|
||||
return f"The nutrition analysis failed: {e}"
|
||||
finally:
|
||||
stop_anim.set()
|
||||
if animator:
|
||||
animator.join(timeout=1)
|
||||
if view_open:
|
||||
stream_sig.emit(False) # back to the JARVIS HUD
|
||||
|
||||
spoken = (data.get("spoken_summary") or "").strip()
|
||||
|
||||
if data.get("food"):
|
||||
panel = (data.get("panel_text") or "").strip()
|
||||
if player and panel:
|
||||
try:
|
||||
player.show_content("🍽 NUTRITION SCAN", panel)
|
||||
except Exception:
|
||||
pass
|
||||
_log(f"JARVIS: Nutrition scan complete — {data.get('food')}"
|
||||
f" ≈ {data.get('calories_kcal')} kcal.")
|
||||
|
||||
return spoken or "The scan finished, but I couldn't read the result."
|
||||
@@ -15,7 +15,7 @@ BASE_DIR = get_base_dir()
|
||||
API_CONFIG_PATH = BASE_DIR / "config" / "api_keys.json"
|
||||
DESKTOP = Path.home() / "Desktop"
|
||||
MAX_BUILD_ATTEMPTS = 3
|
||||
GEMINI_MODEL = "gemini-2.5-flash"
|
||||
GEMINI_MODEL = "gemini-flash-latest"
|
||||
|
||||
|
||||
def _get_api_key() -> str:
|
||||
@@ -488,7 +488,7 @@ Be specific and actionable. If you see an error message, quote it exactly."""
|
||||
]
|
||||
|
||||
response = client.models.generate_content(
|
||||
model="gemini-2.5-flash",
|
||||
model="gemini-flash-latest",
|
||||
contents=contents,
|
||||
)
|
||||
|
||||
|
||||
@@ -336,7 +336,7 @@ def _screen_find(description: str) -> tuple[int, int] | None:
|
||||
)
|
||||
|
||||
response = client.models.generate_content(
|
||||
model="gemini-2.5-flash-lite",
|
||||
model="gemini-flash-lite-latest",
|
||||
contents=[
|
||||
gtypes.Part.from_bytes(data=image_bytes, mime_type="image/png"),
|
||||
prompt,
|
||||
|
||||
@@ -613,7 +613,7 @@ Rules:
|
||||
- Return ONLY the JSON, no explanation, no markdown."""
|
||||
|
||||
try:
|
||||
resp = _client.models.generate_content(model="gemini-2.5-flash-lite", contents=prompt)
|
||||
resp = _client.models.generate_content(model="gemini-flash-lite-latest", contents=prompt)
|
||||
text = re.sub(r"```(?:json)?", "", resp.text).strip().rstrip("`").strip()
|
||||
return json.loads(text)
|
||||
except Exception as e:
|
||||
|
||||
+1
-1
@@ -142,7 +142,7 @@ Output ONLY the Python code. No explanation, no markdown, no backticks.
|
||||
Task: {task}"""
|
||||
|
||||
try:
|
||||
response = _client.models.generate_content(model="gemini-2.5-flash", contents=prompt)
|
||||
response = _client.models.generate_content(model="gemini-flash-latest", contents=prompt)
|
||||
code = response.text.strip()
|
||||
if code.startswith("```"):
|
||||
lines = code.split("\n")
|
||||
|
||||
@@ -16,8 +16,8 @@ BASE_DIR = get_base_dir()
|
||||
API_CONFIG_PATH = BASE_DIR / "config" / "api_keys.json"
|
||||
PROJECTS_DIR = Path.home() / "Desktop" / "JarvisProjects"
|
||||
MAX_FIX_ATTEMPTS = 5
|
||||
MODEL_PLANNER = "gemini-2.5-flash"
|
||||
MODEL_WRITER = "gemini-2.5-flash"
|
||||
MODEL_PLANNER = "gemini-flash-latest"
|
||||
MODEL_WRITER = "gemini-flash-latest"
|
||||
|
||||
def _get_api_key() -> str:
|
||||
with open(API_CONFIG_PATH, "r", encoding="utf-8") as f:
|
||||
|
||||
@@ -37,7 +37,7 @@ def _gemini_client():
|
||||
|
||||
class _W:
|
||||
def generate_content(self, contents):
|
||||
return _c.models.generate_content(model="gemini-2.5-flash", contents=contents)
|
||||
return _c.models.generate_content(model="gemini-flash-latest", contents=contents)
|
||||
|
||||
return _W()
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ def _parse_date(raw: str) -> str:
|
||||
from google import genai as _genai
|
||||
_client = _genai.Client(api_key=_get_api_key())
|
||||
response = _client.models.generate_content(
|
||||
model="gemini-2.5-flash-lite",
|
||||
model="gemini-flash-lite-latest",
|
||||
contents=(
|
||||
f"Today is {today.strftime('%Y-%m-%d')}. "
|
||||
f"Convert this date expression to YYYY-MM-DD: '{raw}'. "
|
||||
@@ -169,7 +169,7 @@ def _parse_flights_with_gemini(
|
||||
|
||||
try:
|
||||
response = _client.models.generate_content(
|
||||
model="gemini-2.5-flash",
|
||||
model="gemini-flash-latest",
|
||||
contents=prompt,
|
||||
config=types.GenerateContentConfig(
|
||||
system_instruction=(
|
||||
|
||||
@@ -0,0 +1,377 @@
|
||||
"""
|
||||
JARVIS plugin — Pushup Counter (webcam vision).
|
||||
|
||||
Say "Jarvis, count my pushups" (any language) — the HUD switches to the
|
||||
live camera, a 5-second countdown lets you get into position, then JARVIS
|
||||
counts your reps live on screen and finally delivers the result with
|
||||
playful, teasing motivational commentary in your language.
|
||||
|
||||
Detection (two-tier):
|
||||
• If `mediapipe` is installed (pip install mediapipe): elbow-angle
|
||||
tracking — accurate from front or side view.
|
||||
• Otherwise: face-motion tracking with OpenCV only (zero extra
|
||||
dependencies) — place the camera on the floor in front of you so your
|
||||
head moves toward/away from it with each rep.
|
||||
|
||||
Session ends automatically: target reached, ~20 s without a rep, or the
|
||||
3-minute safety cap. Just stand up when you're done.
|
||||
|
||||
Sessions are saved to memory/long_term.json ("pushup_sessions") so future
|
||||
versions can track progress over time.
|
||||
"""
|
||||
|
||||
import json
|
||||
import time
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
PLUGIN = {
|
||||
"name": "pushup_counter",
|
||||
"description": (
|
||||
"Counts the user's pushups live through the WEBCAM. Use whenever the "
|
||||
"user says they are about to do pushups and wants them counted — e.g. "
|
||||
"'şınav çekeceğim say', 'count my pushups', 'pushup challenge'. "
|
||||
"If the user mentions a goal ('20 şınav çekeceğim'), pass it as "
|
||||
"'target'. The tool runs the WHOLE workout and only returns when the "
|
||||
"session ends (this can take a few minutes) — do not call other tools "
|
||||
"meanwhile. When the result comes back, deliver it in the user's "
|
||||
"language with playful, lightly TEASING motivational commentary "
|
||||
"(e.g. for 10 reps: congratulate them for being 'a perfectly average "
|
||||
"human'). Never use screen_process for pushup counting."
|
||||
),
|
||||
"parameters": {
|
||||
"type": "OBJECT",
|
||||
"properties": {
|
||||
"query": {
|
||||
"type": "STRING",
|
||||
"description": "The user's exact request, verbatim, in their own language.",
|
||||
},
|
||||
"target": {
|
||||
"type": "NUMBER",
|
||||
"description": "Rep goal if the user stated one (e.g. 20). Omit otherwise.",
|
||||
},
|
||||
},
|
||||
"required": ["query"],
|
||||
},
|
||||
}
|
||||
|
||||
_COUNTDOWN_SECONDS = 5
|
||||
_MAX_SESSION_SECONDS = 180
|
||||
_IDLE_END_SECONDS = 20 # end after this long without a rep (once started)
|
||||
_IDLE_ABORT_SECONDS = 40 # abort if NO rep ever happens
|
||||
_FPS = 20
|
||||
_CYAN = (255, 190, 40) # JARVIS cyan-blue (BGR)
|
||||
_BRIGHT = (255, 235, 130)
|
||||
|
||||
|
||||
# ── config / camera (same pattern as calorie_counter) ───────────────────────
|
||||
|
||||
def _config() -> dict:
|
||||
try:
|
||||
return json.loads(
|
||||
(BASE_DIR / "config" / "api_keys.json").read_text(encoding="utf-8")
|
||||
)
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def _open_camera():
|
||||
import platform
|
||||
try:
|
||||
backend = cv2.CAP_DSHOW if platform.system() == "Windows" else cv2.CAP_ANY
|
||||
except AttributeError:
|
||||
backend = 0
|
||||
cap = cv2.VideoCapture(int(_config().get("camera_index", 0)), backend)
|
||||
if not cap.isOpened():
|
||||
cap = cv2.VideoCapture(0)
|
||||
if not cap.isOpened():
|
||||
return None
|
||||
for _ in range(6):
|
||||
cap.read()
|
||||
return cap
|
||||
|
||||
|
||||
def _emit_frame(frame_sig, frame: np.ndarray) -> None:
|
||||
ok, buf = cv2.imencode(".jpg", frame, [cv2.IMWRITE_JPEG_QUALITY, 70])
|
||||
if ok:
|
||||
frame_sig.emit(buf.tobytes())
|
||||
|
||||
|
||||
# ── rep counting state machine ───────────────────────────────────────────────
|
||||
|
||||
class _RepCounter:
|
||||
"""Counts full down→up cycles of a raw 'down-ness' signal (bigger =
|
||||
lower). Tracks the signal's own min/max range (with slow decay) so it
|
||||
self-calibrates to any camera distance or body size."""
|
||||
|
||||
def __init__(self, min_rel_amplitude: float = 0.18):
|
||||
self._min_rel = min_rel_amplitude
|
||||
self.lo = None
|
||||
self.hi = None
|
||||
self.state = "up"
|
||||
self.reps = 0
|
||||
|
||||
def update(self, v: float) -> bool:
|
||||
if self.lo is None:
|
||||
self.lo = self.hi = float(v)
|
||||
return False
|
||||
# decay range toward the current value, then expand to include it
|
||||
self.lo = min(self.lo + (v - self.lo) * 0.002, v)
|
||||
self.hi = max(self.hi + (v - self.hi) * 0.002, v)
|
||||
rng = self.hi - self.lo
|
||||
if rng < max(abs(self.hi), 1e-6) * self._min_rel:
|
||||
return False # not enough movement observed yet
|
||||
pos = (v - self.lo) / rng
|
||||
if self.state == "up" and pos > 0.70:
|
||||
self.state = "down"
|
||||
elif self.state == "down" and pos < 0.30:
|
||||
self.state = "up"
|
||||
self.reps += 1
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
# ── detectors: return a 'down-ness' signal (bigger = lower), or None ────────
|
||||
|
||||
def _make_pose_detector():
|
||||
"""MediaPipe elbow-angle tracking. Returns fn(frame)->float|None,
|
||||
or None if mediapipe isn't installed."""
|
||||
try:
|
||||
import mediapipe as mp
|
||||
pose = mp.solutions.pose.Pose(model_complexity=0,
|
||||
min_detection_confidence=0.5,
|
||||
min_tracking_confidence=0.5)
|
||||
L = mp.solutions.pose.PoseLandmark
|
||||
except Exception:
|
||||
# mediapipe missing, or an incompatible version without the
|
||||
# legacy solutions API — face tracking takes over either way
|
||||
return None
|
||||
|
||||
def _angle(a, b, c) -> float:
|
||||
ba = np.array([a.x - b.x, a.y - b.y])
|
||||
bc = np.array([c.x - b.x, c.y - b.y])
|
||||
cos = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc) + 1e-9)
|
||||
return float(np.degrees(np.arccos(np.clip(cos, -1.0, 1.0))))
|
||||
|
||||
def _signal(frame: np.ndarray):
|
||||
res = pose.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
||||
lm = getattr(res, "pose_landmarks", None)
|
||||
if not lm:
|
||||
return None
|
||||
pts = lm.landmark
|
||||
best = None
|
||||
for s, e, w in ((L.LEFT_SHOULDER, L.LEFT_ELBOW, L.LEFT_WRIST),
|
||||
(L.RIGHT_SHOULDER, L.RIGHT_ELBOW, L.RIGHT_WRIST)):
|
||||
vis = min(pts[s].visibility, pts[e].visibility, pts[w].visibility)
|
||||
if vis < 0.5:
|
||||
continue
|
||||
ang = _angle(pts[s], pts[e], pts[w]) # 180 = arm straight
|
||||
if best is None or vis > best[1]:
|
||||
best = (ang, vis)
|
||||
if best is None:
|
||||
return None
|
||||
# straight arm (170°) -> 0.0 (up) … bent arm (60°) -> 1.0 (down)
|
||||
return float(np.clip((170.0 - best[0]) / 110.0, 0.0, 1.0))
|
||||
|
||||
return _signal
|
||||
|
||||
|
||||
def _make_face_detector():
|
||||
"""OpenCV-only fallback: face area grows as the user lowers toward a
|
||||
floor-placed camera. Returns fn(frame)->float|None."""
|
||||
cascade = cv2.CascadeClassifier(
|
||||
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
|
||||
)
|
||||
|
||||
def _signal(frame: np.ndarray):
|
||||
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
||||
small = cv2.resize(gray, (0, 0), fx=0.5, fy=0.5)
|
||||
faces = cascade.detectMultiScale(small, 1.15, 4, minSize=(30, 30))
|
||||
if len(faces) == 0:
|
||||
return None
|
||||
x, y, w, h = max(faces, key=lambda f: f[2] * f[3])
|
||||
return float(w * h) # bigger face = closer = down
|
||||
|
||||
return _signal
|
||||
|
||||
|
||||
# ── on-frame overlay (ASCII/digits only — cv2.putText can't render more) ────
|
||||
|
||||
def _draw_overlay(frame, reps, target, state, flash_until, countdown=None):
|
||||
out = frame.copy()
|
||||
h, w = out.shape[:2]
|
||||
if countdown is not None:
|
||||
txt = str(countdown)
|
||||
size = cv2.getTextSize(txt, cv2.FONT_HERSHEY_DUPLEX, 6.0, 8)[0]
|
||||
cv2.putText(out, txt, ((w - size[0]) // 2, (h + size[1]) // 2),
|
||||
cv2.FONT_HERSHEY_DUPLEX, 6.0, _BRIGHT, 8, cv2.LINE_AA)
|
||||
return out
|
||||
big = time.time() < flash_until
|
||||
scale = 4.0 if big else 2.8
|
||||
color = _BRIGHT if big else _CYAN
|
||||
label = f"{reps}" + (f"/{int(target)}" if target else "")
|
||||
cv2.putText(out, label, (24, 90), cv2.FONT_HERSHEY_DUPLEX,
|
||||
scale, color, 6 if big else 4, cv2.LINE_AA)
|
||||
# state hint bar at the bottom: fills while DOWN
|
||||
bar_w = int(w * (0.9 if state == "down" else 0.15))
|
||||
cv2.rectangle(out, (0, h - 10), (bar_w, h), _CYAN, -1)
|
||||
return out
|
||||
|
||||
|
||||
# ── persistence (memory/long_term.json, additive — other keys preserved) ────
|
||||
|
||||
def _record_session(reps: int, seconds: float) -> int:
|
||||
"""Append session, return today's total reps. Never raises."""
|
||||
try:
|
||||
path = BASE_DIR / "memory" / "long_term.json"
|
||||
data = {}
|
||||
if path.exists():
|
||||
try:
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
except Exception:
|
||||
data = {}
|
||||
sessions = data.get("pushup_sessions")
|
||||
if not isinstance(sessions, list):
|
||||
sessions = []
|
||||
today = date.today().isoformat()
|
||||
sessions.append({"date": today, "reps": reps, "seconds": round(seconds)})
|
||||
data["pushup_sessions"] = sessions[-100:]
|
||||
path.write_text(json.dumps(data, indent=2, ensure_ascii=False),
|
||||
encoding="utf-8")
|
||||
return sum(s.get("reps", 0) for s in sessions if s.get("date") == today)
|
||||
except Exception:
|
||||
return reps
|
||||
|
||||
|
||||
# ── entry point ──────────────────────────────────────────────────────────────
|
||||
|
||||
def run(parameters: dict, player=None, session_memory=None) -> str:
|
||||
try:
|
||||
target = int(parameters.get("target") or 0)
|
||||
except Exception:
|
||||
target = 0
|
||||
|
||||
def _log(msg: str) -> None:
|
||||
if player:
|
||||
try:
|
||||
player.write_log(msg)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
win = getattr(player, "_win", None) if player else None
|
||||
frame_sig = getattr(win, "_cam_frame_sig", None)
|
||||
stream_sig = getattr(win, "_cam_stream_sig", None)
|
||||
|
||||
detector = _make_pose_detector()
|
||||
mode = "elbow tracking (mediapipe)"
|
||||
if detector is None:
|
||||
detector = _make_face_detector()
|
||||
mode = "face-motion tracking (place camera on the floor in front of you)"
|
||||
|
||||
cap = _open_camera()
|
||||
if cap is None:
|
||||
return ("I couldn't access the camera — it may be in use by another "
|
||||
"feature or application.")
|
||||
|
||||
counter = _RepCounter()
|
||||
flash_until = 0.0
|
||||
view_open = False
|
||||
started = time.time()
|
||||
last_rep_time = started
|
||||
end_reason = "finished"
|
||||
try:
|
||||
if stream_sig:
|
||||
stream_sig.emit(True)
|
||||
view_open = True
|
||||
_log(f"JARVIS: Pushup session started — {mode}.")
|
||||
|
||||
# Phase 1 — countdown so the user can get into position
|
||||
t0 = time.time()
|
||||
while time.time() - t0 < _COUNTDOWN_SECONDS:
|
||||
ok, frm = cap.read()
|
||||
if ok and frm is not None and frame_sig:
|
||||
remaining = _COUNTDOWN_SECONDS - int(time.time() - t0)
|
||||
_emit_frame(frame_sig,
|
||||
_draw_overlay(frm, 0, target, "up", 0,
|
||||
countdown=max(1, remaining)))
|
||||
time.sleep(1.0 / _FPS)
|
||||
|
||||
# Phase 2 — count
|
||||
started = time.time()
|
||||
last_rep_time = started
|
||||
while True:
|
||||
now = time.time()
|
||||
if now - started > _MAX_SESSION_SECONDS:
|
||||
end_reason = "time limit"
|
||||
break
|
||||
idle = now - last_rep_time
|
||||
if counter.reps > 0 and idle > _IDLE_END_SECONDS:
|
||||
end_reason = "user stopped"
|
||||
break
|
||||
if counter.reps == 0 and idle > _IDLE_ABORT_SECONDS:
|
||||
end_reason = "no pushups detected"
|
||||
break
|
||||
if target and counter.reps >= target:
|
||||
end_reason = "target reached"
|
||||
break
|
||||
|
||||
ok, frm = cap.read()
|
||||
if not ok or frm is None:
|
||||
time.sleep(1.0 / _FPS)
|
||||
continue
|
||||
sig = detector(frm)
|
||||
if sig is not None and counter.update(sig):
|
||||
last_rep_time = time.time()
|
||||
flash_until = last_rep_time + 0.6
|
||||
if counter.reps % 10 == 0 or (target and counter.reps == target):
|
||||
_log(f"JARVIS: Pushup {counter.reps} 💪")
|
||||
if frame_sig:
|
||||
_emit_frame(frame_sig,
|
||||
_draw_overlay(frm, counter.reps, target,
|
||||
counter.state, flash_until))
|
||||
time.sleep(1.0 / _FPS)
|
||||
finally:
|
||||
try:
|
||||
cap.release()
|
||||
except Exception:
|
||||
pass
|
||||
if view_open:
|
||||
stream_sig.emit(False)
|
||||
|
||||
reps = counter.reps
|
||||
seconds = max(1.0, time.time() - started)
|
||||
|
||||
if reps == 0:
|
||||
return ("The session ended without a single detected pushup "
|
||||
f"({end_reason}). Tell the user — with gentle teasing — that "
|
||||
"zero pushups were counted, and remind them the camera must "
|
||||
"clearly see them (or their face, in face-tracking mode).")
|
||||
|
||||
today_total = _record_session(reps, seconds)
|
||||
pace = reps / (seconds / 60.0)
|
||||
|
||||
if player:
|
||||
try:
|
||||
player.show_content(
|
||||
"💪 PUSHUP SESSION",
|
||||
(f"Reps: {reps}" + (f" / {target}" if target else "") + "\n"
|
||||
f"Duration: {int(seconds)} s\n"
|
||||
f"Pace: {pace:.1f} reps/min\n"
|
||||
f"Today's total: {today_total}\n"
|
||||
f"Ended: {end_reason}\n"
|
||||
f"Mode: {mode}"),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return (f"Pushup session finished ({end_reason}). Reps: {reps}"
|
||||
+ (f" of the {target} targeted" if target else "")
|
||||
+ f". Duration: {int(seconds)} seconds ({pace:.1f} reps/min)."
|
||||
f" Today's total: {today_total}. Deliver this to the user in "
|
||||
"their language with playful, lightly teasing motivational "
|
||||
"commentary about the number.")
|
||||
@@ -0,0 +1,344 @@
|
||||
#upload_video.py
|
||||
"""
|
||||
Hardcoded automation: uploads the video sitting on the Desktop (named 'video')
|
||||
to TikTok Studio, using a fixed, always-identical sequence of steps — same
|
||||
order, every single time, no improvisation.
|
||||
|
||||
The user gives only a rough caption BRIEF ("write that I made a plugin system
|
||||
in the new version") — this plugin expands it with Gemini into a full,
|
||||
SEO-keyword-rich TikTok caption in the user's own language before the
|
||||
automation starts, so no Gemini latency lands in the middle of the click
|
||||
sequence.
|
||||
|
||||
Opera GX is launched the normal way (Win key, type, Enter) — exactly like a
|
||||
human opening it — so it comes up as the user's real, already-logged-in
|
||||
browser instead of a separate automated instance with a blank profile.
|
||||
|
||||
Flow (fixed): Opera GX → new tab → tiktok.com/tiktokstudio/upload → click
|
||||
screen center → Tab → Enter (file picker opens) → Ctrl+F → type 'video' →
|
||||
open the found file → Tab → Ctrl+A → paste caption → 16×Tab → Enter. Done.
|
||||
|
||||
pyautogui.FAILSAFE stays on: slamming the mouse into the top-left corner
|
||||
aborts the automation instantly.
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
import pyautogui
|
||||
pyautogui.FAILSAFE = True
|
||||
pyautogui.PAUSE = 0.05
|
||||
_PYAUTOGUI = True
|
||||
except ImportError:
|
||||
_PYAUTOGUI = False
|
||||
|
||||
try:
|
||||
import pyperclip
|
||||
_PYPERCLIP = True
|
||||
except ImportError:
|
||||
_PYPERCLIP = False
|
||||
|
||||
|
||||
PLUGIN = {
|
||||
"name": "upload_video",
|
||||
"description": (
|
||||
"Uploads the video file named 'video' on the user's Desktop to TIKTOK "
|
||||
"with a fully automated browser flow (Opera GX + TikTok Studio). Use "
|
||||
"when the user asks to upload/post/share their video to TikTok — e.g. "
|
||||
"'upload this video to TikTok', 'videoyu tiktok'a yükle'. "
|
||||
"CAPTION RULE — STRICT: 'description' must contain the user's OWN "
|
||||
"caption idea, passed VERBATIM in their language. NEVER invent, "
|
||||
"guess, or auto-fill it yourself. If the user's request contains no "
|
||||
"caption topic, DO NOT call this tool yet — first ASK the user what "
|
||||
"the caption should be about, and call only after they answer. The "
|
||||
"single exception: if the user explicitly delegates it ('sen yaz', "
|
||||
"'default olsun', 'you decide'), you may then pass your own brief. "
|
||||
"The tool itself expands the brief into a full SEO caption — do NOT "
|
||||
"expand it yourself. Call this tool EXACTLY ONCE per request. "
|
||||
"The tool returns IMMEDIATELY while the upload keeps running in the "
|
||||
"background — when the result arrives, announce the start exactly as "
|
||||
"it instructs (uploading now + hands off mouse/keyboard), then wait: "
|
||||
"progress remarks and the completion announcement arrive by "
|
||||
"themselves. Do NOT call the tool again to check status. Never use "
|
||||
"browser_control or computer_control for TikTok uploads."
|
||||
),
|
||||
"parameters": {
|
||||
"type": "OBJECT",
|
||||
"properties": {
|
||||
"description": {
|
||||
"type": "STRING",
|
||||
"description": ("The user's rough caption brief, verbatim, in "
|
||||
"their own language."),
|
||||
}
|
||||
},
|
||||
"required": ["description"],
|
||||
},
|
||||
}
|
||||
|
||||
_POST_COOLDOWN_SEC = 20.0 # ignore repeat calls for this long after a successful post
|
||||
_last_post_at = 0.0
|
||||
_upload_running = False # a background upload is in progress right now
|
||||
|
||||
|
||||
def _get_base_dir() -> Path:
|
||||
if getattr(sys, "frozen", False):
|
||||
return Path(sys.executable).parent
|
||||
return Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
BASE_DIR = _get_base_dir()
|
||||
API_CONFIG_PATH = BASE_DIR / "config" / "api_keys.json"
|
||||
|
||||
|
||||
def _get_api_key() -> str:
|
||||
with open(API_CONFIG_PATH, "r", encoding="utf-8") as f:
|
||||
return json.load(f)["gemini_api_key"]
|
||||
|
||||
|
||||
def _require_pyautogui() -> None:
|
||||
if not _PYAUTOGUI:
|
||||
raise RuntimeError("PyAutoGUI not installed. Run: pip install pyautogui")
|
||||
|
||||
|
||||
def _paste(text: str) -> None:
|
||||
"""Types text via clipboard paste so non-ASCII characters and emoji survive."""
|
||||
_require_pyautogui()
|
||||
if _PYPERCLIP:
|
||||
pyperclip.copy(text)
|
||||
time.sleep(0.15)
|
||||
pyautogui.hotkey("ctrl", "v")
|
||||
else:
|
||||
pyautogui.typewrite(text, interval=0.02)
|
||||
time.sleep(0.2)
|
||||
|
||||
|
||||
def _generate_caption(brief: str) -> str:
|
||||
"""Expands the user's rough brief into a LONG, SEO-heavy TikTok caption
|
||||
with Gemini, in the same language as the brief, keyword-targeted at
|
||||
Tier-1 audiences (US/UK/CA/AU/DE...). Falls back to the brief itself if
|
||||
the call fails — the upload must never be blocked by a caption error."""
|
||||
try:
|
||||
from google import genai
|
||||
client = genai.Client(api_key=_get_api_key())
|
||||
prompt = (
|
||||
"You are a TikTok SEO copywriter. The user gives you only a rough "
|
||||
"TITLE/BRIEF; you turn it into ONE ready-to-post, LONG caption in "
|
||||
"the SAME language as the brief:\n"
|
||||
f'BRIEF: "{brief}"\n'
|
||||
"Rules:\n"
|
||||
"- LONG and substantial: at least 1000 characters of body text "
|
||||
"(not counting hashtags). Expand the brief into a detailed, "
|
||||
"engaging story/description — this is for search ranking, not a "
|
||||
"one-liner.\n"
|
||||
"- Strong scroll-stopping hook as the first line.\n"
|
||||
"- Weave in plenty of high-search-volume SEO keywords and phrases "
|
||||
"naturally throughout the body, chosen to rank with TIER-1 "
|
||||
"audiences (US, UK, Canada, Australia, Germany...). Keywords may "
|
||||
"be in English even if the body language differs.\n"
|
||||
"- End with a call-to-action (follow/comment/share), then 15-25 "
|
||||
"hashtags: mix broad high-volume Tier-1 English hashtags with "
|
||||
"niche topic hashtags.\n"
|
||||
"- Plain text only — no quotes, no markdown, no explanations.\n"
|
||||
"- Maximum 3500 characters total."
|
||||
)
|
||||
r = client.models.generate_content(model="gemini-flash-latest", contents=prompt)
|
||||
caption = (r.text or "").strip()
|
||||
return caption[:3900] if caption else brief
|
||||
except Exception as e:
|
||||
print(f"[UploadVideo] Caption generation failed, using brief as-is: {e}")
|
||||
return brief
|
||||
|
||||
|
||||
def _run_upload_flow(brief: str, caption_holder: dict,
|
||||
caption_ready: threading.Event) -> str:
|
||||
_require_pyautogui()
|
||||
w, h = pyautogui.size()
|
||||
|
||||
# Open Opera GX exactly like a human would (Win key -> type -> Enter)
|
||||
pyautogui.press("win")
|
||||
time.sleep(0.7)
|
||||
pyautogui.write("opera gx", interval=0.05)
|
||||
time.sleep(0.9)
|
||||
pyautogui.press("enter")
|
||||
time.sleep(3.0) # give Opera GX time to come to the foreground
|
||||
|
||||
# New tab -> TikTok Studio upload page
|
||||
pyautogui.hotkey("ctrl", "t")
|
||||
time.sleep(0.6)
|
||||
_paste("https://www.tiktok.com/tiktokstudio/upload")
|
||||
time.sleep(0.2)
|
||||
pyautogui.press("enter")
|
||||
time.sleep(5.0) # page load
|
||||
|
||||
# Click dead center of the screen (upload dropzone area), then Tab + Enter
|
||||
# to activate the 'Select video' control -> native file picker opens
|
||||
pyautogui.click(w // 2, h // 2)
|
||||
time.sleep(0.8)
|
||||
pyautogui.press("tab")
|
||||
time.sleep(0.3)
|
||||
pyautogui.press("enter")
|
||||
time.sleep(2.0)
|
||||
|
||||
# Ctrl+F is a fixed OS-level shortcut that always opens the Open dialog's
|
||||
# search box regardless of screen/resolution -- no lookup needed.
|
||||
pyautogui.hotkey("ctrl", "f")
|
||||
time.sleep(0.6)
|
||||
pyautogui.write("video", interval=0.03)
|
||||
time.sleep(1.5) # let Explorer filter the results down to the matching file
|
||||
|
||||
# Open the found file. 180x120 on the reference 1920x1080 layout, scaled
|
||||
# to the actual resolution. Double-click = select + open in one motion
|
||||
# (same proven pattern as the old Instagram flow's file pick).
|
||||
pyautogui.doubleClick(int(w * 240 / 1920), int(h * 135 / 1080))
|
||||
time.sleep(4.0) # TikTok starts processing the upload; caption box appears
|
||||
|
||||
# Caption: Tab once into the field, select the auto-filled text, replace it.
|
||||
# The SEO caption has been generating in a background thread since the very
|
||||
# first keystroke — by now (~20s in) it's almost always ready; wait max 15s
|
||||
# more, then fall back to the raw brief so the flow never stalls.
|
||||
caption_ready.wait(timeout=15)
|
||||
caption = (caption_holder.get("caption") or brief).strip()
|
||||
pyautogui.press("tab")
|
||||
pyautogui.press("tab")
|
||||
pyautogui.hotkey("ctrl", "a")
|
||||
time.sleep(0.3)
|
||||
_paste(caption)
|
||||
time.sleep(3)
|
||||
|
||||
# 16 Tabs down to the Post button, Enter -> published
|
||||
time.sleep(6)
|
||||
for _ in range(17):
|
||||
pyautogui.press("tab")
|
||||
time.sleep(0.12)
|
||||
time.sleep(1)
|
||||
pyautogui.press("enter")
|
||||
time.sleep(1)
|
||||
pyautogui.click(int(w * 1089 / 1920), int(h * 653 / 1080))
|
||||
pyautogui.click(int(w * 1200 / 1920), int(h * 655 / 1080))
|
||||
|
||||
return "OK"
|
||||
|
||||
|
||||
def run(parameters: dict, player=None, session_memory=None) -> str:
|
||||
global _last_post_at, _upload_running
|
||||
|
||||
if _upload_running:
|
||||
return ("The upload is STILL RUNNING right now — this call was "
|
||||
"ignored. Do not call the tool again; briefly tell the user, "
|
||||
"in their language, that it is still in progress.")
|
||||
|
||||
since_last = time.monotonic() - _last_post_at
|
||||
if since_last < _POST_COOLDOWN_SEC:
|
||||
print(f"[UploadVideo] Ignoring duplicate call ({since_last:.0f}s after last post)")
|
||||
return ("The video was ALREADY posted successfully — this duplicate "
|
||||
"call was ignored. Do not repeat any status message. Simply "
|
||||
"confirm ONCE, in the user's language and in past tense, that "
|
||||
"the video is posted (e.g. 'I posted the video, sir — you're "
|
||||
"welcome.').")
|
||||
|
||||
brief = ((parameters or {}).get("description") or "").strip()
|
||||
print(f"[UploadVideo] brief={'<empty>' if not brief else brief[:40]!r}")
|
||||
if not brief:
|
||||
return ("I need a caption idea before I share the video, sir. What "
|
||||
"should it be about, or shall I write one myself?")
|
||||
|
||||
# Kick off SEO caption generation in the BACKGROUND — the click sequence
|
||||
# starts immediately (within ~1s); the caption is only needed ~20s later
|
||||
# at the paste step, by which time it's ready.
|
||||
caption_holder: dict = {}
|
||||
caption_ready = threading.Event()
|
||||
|
||||
def _gen():
|
||||
try:
|
||||
caption_holder["caption"] = _generate_caption(brief)
|
||||
finally:
|
||||
caption_ready.set()
|
||||
|
||||
threading.Thread(target=_gen, daemon=True, name="tiktok-caption").start()
|
||||
|
||||
def _log(msg: str) -> None:
|
||||
if player:
|
||||
try:
|
||||
player.write_log(msg)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
_log("JARVIS: TikTok upload starting — please don't touch the mouse "
|
||||
"or keyboard.")
|
||||
|
||||
say = getattr(player, "request_say", None) if player else None
|
||||
|
||||
# ── No speech channel (older core): fall back to the blocking flow ──────
|
||||
if not say:
|
||||
try:
|
||||
result = _run_upload_flow(brief, caption_holder, caption_ready)
|
||||
except Exception as e:
|
||||
print(f"[UploadVideo] Error: {e}")
|
||||
return f"An error occurred while sharing the video, sir: {e}"
|
||||
if result != "OK":
|
||||
return result
|
||||
_last_post_at = time.monotonic()
|
||||
return ("The video has been posted to TikTok successfully — announce "
|
||||
"it in the user's language, in PAST tense, warm and brief. "
|
||||
"Do NOT call upload_video again.")
|
||||
|
||||
# ── Two-phase mode (same trick as instant vision acknowledgment): the
|
||||
# tool returns IMMEDIATELY so Gemini can speak right away; the actual
|
||||
# automation, the mid-upload small talk and the completion announcement
|
||||
# all run in background threads through the plugin speech channel. ────
|
||||
stop_chatter = threading.Event()
|
||||
|
||||
def _chatter():
|
||||
remarks = [
|
||||
("[UPLOAD STATUS — speak now] The TikTok upload is underway in "
|
||||
"the background. Make ONE warm, personal small-talk remark to "
|
||||
"the user in their language — for example, kindly remind them "
|
||||
"not to spend too much time on social media and to keep some "
|
||||
"time for themselves, or a similar caring thought. One or two "
|
||||
"sentences, natural tone, do not mention tools or steps."),
|
||||
("[UPLOAD STATUS — speak now] Still uploading, almost done. Make "
|
||||
"ONE more brief, warm remark to the user in their language — a "
|
||||
"different caring or encouraging thought than before, never "
|
||||
"repeat earlier phrasing. One sentence, do not mention tools "
|
||||
"or steps."),
|
||||
]
|
||||
for delay, remark in zip((10.0, 12.0), remarks):
|
||||
if stop_chatter.wait(delay):
|
||||
return
|
||||
try:
|
||||
say(remark)
|
||||
except Exception:
|
||||
return
|
||||
|
||||
def _worker():
|
||||
global _last_post_at, _upload_running
|
||||
try:
|
||||
result = _run_upload_flow(brief, caption_holder, caption_ready)
|
||||
stop_chatter.set()
|
||||
if result == "OK":
|
||||
_last_post_at = time.monotonic()
|
||||
posted = (caption_holder.get("caption") or brief)[:60]
|
||||
_log(f"[UploadVideo] Posted to TikTok: \"{posted}...\"")
|
||||
say("[UPLOAD FINISHED — speak now] The video has just been "
|
||||
"posted to TikTok successfully. Announce it to the user "
|
||||
"in their language, PAST tense, warm and brief — e.g. "
|
||||
"'I posted the video, sir — you're welcome.'")
|
||||
else:
|
||||
say(f"[UPLOAD PROBLEM — speak now] The upload could not be "
|
||||
f"completed: {result}. Tell the user in their language, "
|
||||
"briefly.")
|
||||
except Exception as e:
|
||||
print(f"[UploadVideo] Error: {e}")
|
||||
stop_chatter.set()
|
||||
say(f"[UPLOAD FAILED — speak now] The TikTok upload failed: {e}. "
|
||||
"Briefly apologize and tell the user in their language.")
|
||||
finally:
|
||||
stop_chatter.set()
|
||||
_upload_running = False
|
||||
|
||||
_upload_running = True
|
||||
threading.Thread(target=_worker, daemon=True, name="tiktok-upload").start()
|
||||
threading.Thread(target=_chatter, daemon=True, name="tiktok-chatter").start()
|
||||
+308
-308
@@ -1,308 +1,308 @@
|
||||
#web_search.py
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
def _get_base_dir() -> Path:
|
||||
if getattr(sys, "frozen", False):
|
||||
return Path(sys.executable).parent
|
||||
return Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
BASE_DIR = _get_base_dir()
|
||||
API_CONFIG_PATH = BASE_DIR / "config" / "api_keys.json"
|
||||
|
||||
|
||||
def _get_api_key() -> str:
|
||||
with open(API_CONFIG_PATH, "r", encoding="utf-8") as f:
|
||||
return json.load(f)["gemini_api_key"]
|
||||
|
||||
|
||||
def _gemini_search(query: str) -> str:
|
||||
from google import genai
|
||||
|
||||
client = genai.Client(api_key=_get_api_key())
|
||||
response = client.models.generate_content(
|
||||
model="gemini-2.5-flash",
|
||||
contents=query,
|
||||
config={"tools": [{"google_search": {}}]},
|
||||
)
|
||||
|
||||
text = ""
|
||||
for part in response.candidates[0].content.parts:
|
||||
if hasattr(part, "text") and part.text:
|
||||
text += part.text
|
||||
|
||||
text = text.strip()
|
||||
if not text:
|
||||
raise ValueError("Gemini returned an empty response.")
|
||||
return text
|
||||
|
||||
|
||||
def _ddg_search(query: str, max_results: int = 6) -> list[dict]:
|
||||
try:
|
||||
from ddgs import DDGS
|
||||
except ImportError:
|
||||
from duckduckgo_search import DDGS
|
||||
|
||||
results = []
|
||||
with DDGS() as ddgs:
|
||||
for r in ddgs.text(query, max_results=max_results):
|
||||
results.append({
|
||||
"title": r.get("title", ""),
|
||||
"snippet": r.get("body", ""),
|
||||
"url": r.get("href", ""),
|
||||
})
|
||||
return results
|
||||
|
||||
|
||||
def _ddg_news(query: str, max_results: int = 8) -> list[dict]:
|
||||
"""DDG news search — returns actual articles, not website homepages."""
|
||||
try:
|
||||
from ddgs import DDGS
|
||||
except ImportError:
|
||||
from duckduckgo_search import DDGS
|
||||
|
||||
results = []
|
||||
try:
|
||||
with DDGS() as ddgs:
|
||||
for r in ddgs.news(query, max_results=max_results):
|
||||
results.append({
|
||||
"title": r.get("title", ""),
|
||||
"snippet": r.get("body", ""),
|
||||
"url": r.get("url", ""),
|
||||
"source": r.get("source", ""),
|
||||
})
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ DDG news() failed ({e}) — falling back to text search")
|
||||
results = _ddg_search(query, max_results=max_results)
|
||||
return results
|
||||
|
||||
|
||||
def _format_ddg(query: str, results: list[dict]) -> str:
|
||||
if not results:
|
||||
return f"No results found for: {query}"
|
||||
|
||||
lines = [f"Search results for: {query}\n"]
|
||||
for i, r in enumerate(results, 1):
|
||||
if r.get("title"): lines.append(f"{i}. {r['title']}")
|
||||
if r.get("snippet"): lines.append(f" {r['snippet']}")
|
||||
if r.get("url"): lines.append(f" Source: {r['url']}")
|
||||
lines.append("")
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
|
||||
def _format_news(query: str, results: list[dict]) -> str:
|
||||
if not results:
|
||||
return f"No news found for: {query}"
|
||||
|
||||
lines = [f"Latest news: {query}\n"]
|
||||
for i, r in enumerate(results, 1):
|
||||
title = r.get("title", "")
|
||||
if not title:
|
||||
continue
|
||||
src = f" [{r['source']}]" if r.get("source") else ""
|
||||
lines.append(f"{i}. {title}{src}")
|
||||
if r.get("snippet"):
|
||||
lines.append(f" {r['snippet'][:140]}")
|
||||
if r.get("url"):
|
||||
lines.append(f" {r['url']}")
|
||||
lines.append("")
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
|
||||
# ── Briefing helper ────────────────────────────────────────────────────────────
|
||||
|
||||
def _gemini_headlines(n: int = 5) -> tuple[list[str], str]:
|
||||
"""
|
||||
Fetches current headlines via Gemini grounded search.
|
||||
Optimised for speed: minimal prompt + strict token cap.
|
||||
Returns (headline_list, raw_text_for_display).
|
||||
"""
|
||||
import re
|
||||
from google import genai
|
||||
|
||||
client = genai.Client(api_key=_get_api_key())
|
||||
response = client.models.generate_content(
|
||||
model="gemini-2.5-flash",
|
||||
contents=f"Current world news: {n} headlines. Numbered list, titles only.",
|
||||
config={"tools": [{"google_search": {}}]},
|
||||
)
|
||||
|
||||
raw = ""
|
||||
for part in response.candidates[0].content.parts:
|
||||
if hasattr(part, "text") and part.text:
|
||||
raw += part.text
|
||||
|
||||
headlines = []
|
||||
for line in raw.strip().split("\n"):
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
# Only accept lines that begin with a number — skips preamble/closing sentences
|
||||
if not re.match(r'^[\d]+[.\)\-]', line):
|
||||
continue
|
||||
clean = re.sub(r'^[\d]+[.\)\-]\s*', '', line)
|
||||
clean = re.sub(r'^\*+\s*', '', clean).strip()
|
||||
if clean and len(clean) > 10:
|
||||
headlines.append(clean)
|
||||
|
||||
return headlines[:n], raw.strip()
|
||||
|
||||
|
||||
# ── Modes ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
def _search(query: str) -> str:
|
||||
"""Default search — Gemini grounded, DDG fallback."""
|
||||
try:
|
||||
return _gemini_search(query)
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ Gemini failed ({e}) — trying DDG...")
|
||||
results = _ddg_search(query)
|
||||
return _format_ddg(query, results)
|
||||
|
||||
|
||||
def _news(query: str) -> str:
|
||||
"""
|
||||
Runs Gemini grounded search AND DDG news in parallel.
|
||||
Returns whichever delivers a valid result first; cancels the other.
|
||||
"""
|
||||
import threading
|
||||
|
||||
gemini_query = f"latest news today: {query}" if query else "top world news today"
|
||||
ddg_query = query if query else "world news today"
|
||||
|
||||
result_box = [None] # first valid result lands here
|
||||
lock = threading.Lock()
|
||||
done_evt = threading.Event()
|
||||
failures = [0]
|
||||
|
||||
def _store(r: str) -> None:
|
||||
if r and len(r) > 60:
|
||||
with lock:
|
||||
if result_box[0] is None:
|
||||
result_box[0] = r
|
||||
done_evt.set()
|
||||
else:
|
||||
with lock:
|
||||
failures[0] += 1
|
||||
if failures[0] >= 2: # both failed — unblock caller
|
||||
done_evt.set()
|
||||
|
||||
def _try_gemini():
|
||||
try:
|
||||
_store(_gemini_search(gemini_query))
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ Gemini news failed ({e})")
|
||||
_store("")
|
||||
|
||||
def _try_ddg():
|
||||
try:
|
||||
results = _ddg_news(ddg_query, max_results=8)
|
||||
_store(_format_news(ddg_query, results))
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ DDG news failed ({e})")
|
||||
_store("")
|
||||
|
||||
threading.Thread(target=_try_gemini, daemon=True).start()
|
||||
threading.Thread(target=_try_ddg, daemon=True).start()
|
||||
|
||||
done_evt.wait(timeout=10.0)
|
||||
return result_box[0] or f"No news found for: {query}"
|
||||
|
||||
|
||||
def _research(query: str) -> str:
|
||||
"""
|
||||
Deep dive — asks Gemini for a comprehensive answer with context.
|
||||
Falls back to a wider DDG fetch.
|
||||
"""
|
||||
research_query = (
|
||||
f"Comprehensive, detailed explanation of: {query}. "
|
||||
"Include background context, key facts, current state, and important nuances."
|
||||
)
|
||||
try:
|
||||
return _gemini_search(research_query)
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ Research Gemini failed ({e}) — DDG fallback...")
|
||||
results = _ddg_search(query, max_results=10)
|
||||
return _format_ddg(query, results)
|
||||
|
||||
|
||||
def _price(query: str) -> str:
|
||||
"""Product price lookup — searches for current market prices."""
|
||||
price_query = f"current price of {query} — how much does it cost today"
|
||||
try:
|
||||
return _gemini_search(price_query)
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ Price Gemini failed ({e}) — DDG fallback...")
|
||||
results = _ddg_search(f"{query} price buy", max_results=6)
|
||||
return _format_ddg(query, results)
|
||||
|
||||
|
||||
def _compare(items: list[str], aspect: str) -> str:
|
||||
query = (
|
||||
f"Compare {', '.join(items)} in terms of {aspect}. "
|
||||
"Give specific facts and data."
|
||||
)
|
||||
try:
|
||||
return _gemini_search(query)
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ Gemini compare failed: {e} — falling back to DDG")
|
||||
|
||||
all_results: dict[str, list] = {}
|
||||
for item in items:
|
||||
try:
|
||||
all_results[item] = _ddg_search(f"{item} {aspect}", max_results=3)
|
||||
except Exception:
|
||||
all_results[item] = []
|
||||
|
||||
lines = [f"Comparison — {aspect.upper()}", "─" * 40]
|
||||
for item in items:
|
||||
lines.append(f"\n▸ {item}")
|
||||
for r in all_results.get(item, [])[:2]:
|
||||
if r.get("snippet"):
|
||||
lines.append(f" • {r['snippet']}")
|
||||
if r.get("url"):
|
||||
lines.append(f" {r['url']}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
# ── Public entry point ─────────────────────────────────────────────────────────
|
||||
|
||||
def web_search(
|
||||
parameters: dict,
|
||||
response=None,
|
||||
player=None,
|
||||
session_memory=None,
|
||||
) -> str:
|
||||
params = parameters or {}
|
||||
query = params.get("query", "").strip()
|
||||
mode = params.get("mode", "search").lower().strip()
|
||||
items = params.get("items", [])
|
||||
aspect = params.get("aspect", "general").strip() or "general"
|
||||
|
||||
if not query and not items:
|
||||
return "Please provide a search query."
|
||||
|
||||
if items and mode not in ("compare",):
|
||||
mode = "compare"
|
||||
|
||||
if player:
|
||||
player.write_log(f"[Search:{mode}] {query or ', '.join(items)}")
|
||||
|
||||
print(f"[WebSearch] 🔍 mode={mode!r} query={query!r}")
|
||||
|
||||
try:
|
||||
if mode == "compare" and items:
|
||||
return _compare(items, aspect)
|
||||
if mode == "news":
|
||||
return _news(query)
|
||||
if mode == "research":
|
||||
return _research(query)
|
||||
if mode == "price":
|
||||
return _price(query)
|
||||
return _search(query)
|
||||
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ❌ All backends failed: {e}")
|
||||
return f"Search failed: {e}"
|
||||
#web_search.py
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
def _get_base_dir() -> Path:
|
||||
if getattr(sys, "frozen", False):
|
||||
return Path(sys.executable).parent
|
||||
return Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
BASE_DIR = _get_base_dir()
|
||||
API_CONFIG_PATH = BASE_DIR / "config" / "api_keys.json"
|
||||
|
||||
|
||||
def _get_api_key() -> str:
|
||||
with open(API_CONFIG_PATH, "r", encoding="utf-8") as f:
|
||||
return json.load(f)["gemini_api_key"]
|
||||
|
||||
|
||||
def _gemini_search(query: str) -> str:
|
||||
from google import genai
|
||||
|
||||
client = genai.Client(api_key=_get_api_key())
|
||||
response = client.models.generate_content(
|
||||
model="gemini-flash-latest",
|
||||
contents=query,
|
||||
config={"tools": [{"google_search": {}}]},
|
||||
)
|
||||
|
||||
text = ""
|
||||
for part in response.candidates[0].content.parts:
|
||||
if hasattr(part, "text") and part.text:
|
||||
text += part.text
|
||||
|
||||
text = text.strip()
|
||||
if not text:
|
||||
raise ValueError("Gemini returned an empty response.")
|
||||
return text
|
||||
|
||||
|
||||
def _ddg_search(query: str, max_results: int = 6) -> list[dict]:
|
||||
try:
|
||||
from ddgs import DDGS
|
||||
except ImportError:
|
||||
from duckduckgo_search import DDGS
|
||||
|
||||
results = []
|
||||
with DDGS() as ddgs:
|
||||
for r in ddgs.text(query, max_results=max_results):
|
||||
results.append({
|
||||
"title": r.get("title", ""),
|
||||
"snippet": r.get("body", ""),
|
||||
"url": r.get("href", ""),
|
||||
})
|
||||
return results
|
||||
|
||||
|
||||
def _ddg_news(query: str, max_results: int = 8) -> list[dict]:
|
||||
"""DDG news search — returns actual articles, not website homepages."""
|
||||
try:
|
||||
from ddgs import DDGS
|
||||
except ImportError:
|
||||
from duckduckgo_search import DDGS
|
||||
|
||||
results = []
|
||||
try:
|
||||
with DDGS() as ddgs:
|
||||
for r in ddgs.news(query, max_results=max_results):
|
||||
results.append({
|
||||
"title": r.get("title", ""),
|
||||
"snippet": r.get("body", ""),
|
||||
"url": r.get("url", ""),
|
||||
"source": r.get("source", ""),
|
||||
})
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ DDG news() failed ({e}) — falling back to text search")
|
||||
results = _ddg_search(query, max_results=max_results)
|
||||
return results
|
||||
|
||||
|
||||
def _format_ddg(query: str, results: list[dict]) -> str:
|
||||
if not results:
|
||||
return f"No results found for: {query}"
|
||||
|
||||
lines = [f"Search results for: {query}\n"]
|
||||
for i, r in enumerate(results, 1):
|
||||
if r.get("title"): lines.append(f"{i}. {r['title']}")
|
||||
if r.get("snippet"): lines.append(f" {r['snippet']}")
|
||||
if r.get("url"): lines.append(f" Source: {r['url']}")
|
||||
lines.append("")
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
|
||||
def _format_news(query: str, results: list[dict]) -> str:
|
||||
if not results:
|
||||
return f"No news found for: {query}"
|
||||
|
||||
lines = [f"Latest news: {query}\n"]
|
||||
for i, r in enumerate(results, 1):
|
||||
title = r.get("title", "")
|
||||
if not title:
|
||||
continue
|
||||
src = f" [{r['source']}]" if r.get("source") else ""
|
||||
lines.append(f"{i}. {title}{src}")
|
||||
if r.get("snippet"):
|
||||
lines.append(f" {r['snippet'][:140]}")
|
||||
if r.get("url"):
|
||||
lines.append(f" {r['url']}")
|
||||
lines.append("")
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
|
||||
# ── Briefing helper ────────────────────────────────────────────────────────────
|
||||
|
||||
def _gemini_headlines(n: int = 5) -> tuple[list[str], str]:
|
||||
"""
|
||||
Fetches current headlines via Gemini grounded search.
|
||||
Optimised for speed: minimal prompt + strict token cap.
|
||||
Returns (headline_list, raw_text_for_display).
|
||||
"""
|
||||
import re
|
||||
from google import genai
|
||||
|
||||
client = genai.Client(api_key=_get_api_key())
|
||||
response = client.models.generate_content(
|
||||
model="gemini-flash-latest",
|
||||
contents=f"Current world news: {n} headlines. Numbered list, titles only.",
|
||||
config={"tools": [{"google_search": {}}]},
|
||||
)
|
||||
|
||||
raw = ""
|
||||
for part in response.candidates[0].content.parts:
|
||||
if hasattr(part, "text") and part.text:
|
||||
raw += part.text
|
||||
|
||||
headlines = []
|
||||
for line in raw.strip().split("\n"):
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
# Only accept lines that begin with a number — skips preamble/closing sentences
|
||||
if not re.match(r'^[\d]+[.\)\-]', line):
|
||||
continue
|
||||
clean = re.sub(r'^[\d]+[.\)\-]\s*', '', line)
|
||||
clean = re.sub(r'^\*+\s*', '', clean).strip()
|
||||
if clean and len(clean) > 10:
|
||||
headlines.append(clean)
|
||||
|
||||
return headlines[:n], raw.strip()
|
||||
|
||||
|
||||
# ── Modes ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
def _search(query: str) -> str:
|
||||
"""Default search — Gemini grounded, DDG fallback."""
|
||||
try:
|
||||
return _gemini_search(query)
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ Gemini failed ({e}) — trying DDG...")
|
||||
results = _ddg_search(query)
|
||||
return _format_ddg(query, results)
|
||||
|
||||
|
||||
def _news(query: str) -> str:
|
||||
"""
|
||||
Runs Gemini grounded search AND DDG news in parallel.
|
||||
Returns whichever delivers a valid result first; cancels the other.
|
||||
"""
|
||||
import threading
|
||||
|
||||
gemini_query = f"latest news today: {query}" if query else "top world news today"
|
||||
ddg_query = query if query else "world news today"
|
||||
|
||||
result_box = [None] # first valid result lands here
|
||||
lock = threading.Lock()
|
||||
done_evt = threading.Event()
|
||||
failures = [0]
|
||||
|
||||
def _store(r: str) -> None:
|
||||
if r and len(r) > 60:
|
||||
with lock:
|
||||
if result_box[0] is None:
|
||||
result_box[0] = r
|
||||
done_evt.set()
|
||||
else:
|
||||
with lock:
|
||||
failures[0] += 1
|
||||
if failures[0] >= 2: # both failed — unblock caller
|
||||
done_evt.set()
|
||||
|
||||
def _try_gemini():
|
||||
try:
|
||||
_store(_gemini_search(gemini_query))
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ Gemini news failed ({e})")
|
||||
_store("")
|
||||
|
||||
def _try_ddg():
|
||||
try:
|
||||
results = _ddg_news(ddg_query, max_results=8)
|
||||
_store(_format_news(ddg_query, results))
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ DDG news failed ({e})")
|
||||
_store("")
|
||||
|
||||
threading.Thread(target=_try_gemini, daemon=True).start()
|
||||
threading.Thread(target=_try_ddg, daemon=True).start()
|
||||
|
||||
done_evt.wait(timeout=10.0)
|
||||
return result_box[0] or f"No news found for: {query}"
|
||||
|
||||
|
||||
def _research(query: str) -> str:
|
||||
"""
|
||||
Deep dive — asks Gemini for a comprehensive answer with context.
|
||||
Falls back to a wider DDG fetch.
|
||||
"""
|
||||
research_query = (
|
||||
f"Comprehensive, detailed explanation of: {query}. "
|
||||
"Include background context, key facts, current state, and important nuances."
|
||||
)
|
||||
try:
|
||||
return _gemini_search(research_query)
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ Research Gemini failed ({e}) — DDG fallback...")
|
||||
results = _ddg_search(query, max_results=10)
|
||||
return _format_ddg(query, results)
|
||||
|
||||
|
||||
def _price(query: str) -> str:
|
||||
"""Product price lookup — searches for current market prices."""
|
||||
price_query = f"current price of {query} — how much does it cost today"
|
||||
try:
|
||||
return _gemini_search(price_query)
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ Price Gemini failed ({e}) — DDG fallback...")
|
||||
results = _ddg_search(f"{query} price buy", max_results=6)
|
||||
return _format_ddg(query, results)
|
||||
|
||||
|
||||
def _compare(items: list[str], aspect: str) -> str:
|
||||
query = (
|
||||
f"Compare {', '.join(items)} in terms of {aspect}. "
|
||||
"Give specific facts and data."
|
||||
)
|
||||
try:
|
||||
return _gemini_search(query)
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ⚠️ Gemini compare failed: {e} — falling back to DDG")
|
||||
|
||||
all_results: dict[str, list] = {}
|
||||
for item in items:
|
||||
try:
|
||||
all_results[item] = _ddg_search(f"{item} {aspect}", max_results=3)
|
||||
except Exception:
|
||||
all_results[item] = []
|
||||
|
||||
lines = [f"Comparison — {aspect.upper()}", "─" * 40]
|
||||
for item in items:
|
||||
lines.append(f"\n▸ {item}")
|
||||
for r in all_results.get(item, [])[:2]:
|
||||
if r.get("snippet"):
|
||||
lines.append(f" • {r['snippet']}")
|
||||
if r.get("url"):
|
||||
lines.append(f" {r['url']}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
# ── Public entry point ─────────────────────────────────────────────────────────
|
||||
|
||||
def web_search(
|
||||
parameters: dict,
|
||||
response=None,
|
||||
player=None,
|
||||
session_memory=None,
|
||||
) -> str:
|
||||
params = parameters or {}
|
||||
query = params.get("query", "").strip()
|
||||
mode = params.get("mode", "search").lower().strip()
|
||||
items = params.get("items", [])
|
||||
aspect = params.get("aspect", "general").strip() or "general"
|
||||
|
||||
if not query and not items:
|
||||
return "Please provide a search query."
|
||||
|
||||
if items and mode not in ("compare",):
|
||||
mode = "compare"
|
||||
|
||||
if player:
|
||||
player.write_log(f"[Search:{mode}] {query or ', '.join(items)}")
|
||||
|
||||
print(f"[WebSearch] 🔍 mode={mode!r} query={query!r}")
|
||||
|
||||
try:
|
||||
if mode == "compare" and items:
|
||||
return _compare(items, aspect)
|
||||
if mode == "news":
|
||||
return _news(query)
|
||||
if mode == "research":
|
||||
return _research(query)
|
||||
if mode == "price":
|
||||
return _price(query)
|
||||
return _search(query)
|
||||
|
||||
except Exception as e:
|
||||
print(f"[WebSearch] ❌ All backends failed: {e}")
|
||||
return f"Search failed: {e}"
|
||||
@@ -174,7 +174,7 @@ def _summarize_with_gemini(transcript: str, video_url: str) -> str:
|
||||
max_chars = 80000
|
||||
truncated = transcript[:max_chars] + ("..." if len(transcript) > max_chars else "")
|
||||
response = _client.models.generate_content(
|
||||
model="gemini-2.5-flash",
|
||||
model="gemini-flash-latest",
|
||||
contents=f"Please summarize this YouTube video transcript:\n\n{truncated}",
|
||||
config=types.GenerateContentConfig(
|
||||
system_instruction=(
|
||||
|
||||
Reference in new issue
Block a user