-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
142 lines (114 loc) · 4.11 KB
/
Copy pathapp.py
File metadata and controls
142 lines (114 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
from datetime import datetime
from flask import Flask, jsonify, render_template, request, session
from flask_session import Session
from dotenv import load_dotenv
from google import genai
from google.genai import types
# Import custom tools and configurations
from goodies.tools import (
cmd,
config_loader,
image_search,
open_site_or_file,
web_news_search,
web_search,
video_search,
clean_session,
)
# Initialize application and load configuration
load_dotenv()
config = config_loader()
# Initialize Home path
home_dir = os.path.expanduser("~")
# Mapping for dynamic tool loading
TOOL_MAPPING = {
"web_search": web_search,
"web_news_search": web_news_search,
"cmd": cmd,
"image_search": image_search,
"open_site_or_file": open_site_or_file,
"video_search": video_search
}
# Load active tools based on config
active_tools = [TOOL_MAPPING[t] for t in config.get("tools", []) if t in TOOL_MAPPING]
print(f"Using tools: {[t.__name__ for t in active_tools]}")
# Pre-session cleanup and initialization
clean_session()
app = Flask(__name__)
app.secret_key = os.environ.get("FLASK_SECRET_KEY", "super-secret-dev-key")
# Server-side session configuration
app.config["SESSION_TYPE"] = "filesystem"
app.config["SESSION_PERMANENT"] = False
Session(app)
# Initialize Gemini Client
client = genai.Client()
MODEL_ID = config.get("model", "gemini-2.0-flash")
# Terminal UI constants
GREEN = "\033[92m"
YELLOW = "\033[93m"
CYAN = "\033[96m"
RESET = "\033[0m"
def log_token_usage(meta):
"""Logs token usage statistics to the terminal."""
if not meta or not config.get("debugging_mode"):
return
p_tokens = getattr(meta, 'prompt_token_count', 0)
c_tokens = getattr(meta, 'candidates_token_count', 0)
t_tokens = getattr(meta, 'total_token_count', 0)
print(f"\n⚡ {CYAN}[Gemini Token Debugger]{RESET}")
print(f"├─ Input: {YELLOW}{p_tokens}{RESET}")
print(f"├─ Output: {GREEN}{c_tokens}{RESET}")
print(f"└─ Total: {CYAN}{t_tokens} / 1,048,576{RESET}\n")
@app.route('/')
def index():
"""Renders the chat interface."""
chat_history = session.get('chat_history', [])
return render_template('index.html', chat_history=chat_history)
@app.route('/api/chat', methods=['POST'])
def chat():
"""Processes incoming chat messages."""
data = request.get_json() or {}
user_message = data.get('message', '').strip()
if not user_message:
return jsonify({'error': 'Message content cannot be empty.'}), 400
if 'chat_history' not in session:
session['chat_history'] = []
try:
# Load system instructions
with open(config["instruction"], "r", encoding="utf-8") as f:
system_instruction_content = f.read()
full_instruction = system_instruction_content + home_dir
chat_session = client.chats.create(
model=MODEL_ID,
history=session['chat_history'],
config=types.GenerateContentConfig(
temperature=1.2,
system_instruction=full_instruction,
tools=active_tools,
)
)
response = chat_session.send_message(user_message)
# Debugging output
log_token_usage(response.usage_metadata)
# Update session history
updated_history = []
for content in chat_session.get_history():
parts = [{'text': part.text} for part in content.parts if part.text] # pyright: ignore[reportOptionalIterable]
updated_history.append({'role': content.role, 'parts': parts})
session['chat_history'] = updated_history
print(response.text)
return jsonify({'response': response.text})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/sw.js')
def serve_sw():
"""Serves service worker script."""
return app.send_static_file('sw.js')
@app.route('/api/clear', methods=['POST'])
def clear():
"""Clears the chat history session."""
session.pop('chat_history', None)
return jsonify({'status': 'cleared'})
if __name__ == '__main__':
app.run(debug=True)