Leonydis137 commited on
Commit
f075d09
·
verified ·
1 Parent(s): 13054d7

Upload 12 files

Browse files
Files changed (3) hide show
  1. auth.py +14 -0
  2. logger.py +13 -0
  3. storage.py +40 -0
auth.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from fastapi import Request, HTTPException
3
+ from starlette.middleware.base import BaseHTTPMiddleware
4
+
5
+ API_KEY = "super-secret-key" # TODO: Secure this
6
+
7
+ class APIKeyMiddleware(BaseHTTPMiddleware):
8
+ async def dispatch(self, request: Request, call_next):
9
+ if request.url.path.startswith("/"):
10
+ api_key = request.headers.get("x-api-key")
11
+ if api_key != API_KEY:
12
+ raise HTTPException(status_code=401, detail="Invalid API Key")
13
+ response = await call_next(request)
14
+ return response
logger.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import logging
3
+
4
+ def setup_logger():
5
+ logger = logging.getLogger("autonomous_ai")
6
+ logger.setLevel(logging.INFO)
7
+ handler = logging.FileHandler("system.log")
8
+ formatter = logging.Formatter("[%(asctime)s] %(levelname)s: %(message)s")
9
+ handler.setFormatter(formatter)
10
+ logger.addHandler(handler)
11
+ return logger
12
+
13
+ logger = setup_logger()
storage.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import sqlite3
3
+
4
+ def init_db():
5
+ conn = sqlite3.connect("ai_data.db")
6
+ cursor = conn.cursor()
7
+ cursor.execute("CREATE TABLE IF NOT EXISTS goals (id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT)")
8
+ cursor.execute("CREATE TABLE IF NOT EXISTS feedback (id INTEGER PRIMARY KEY AUTOINCREMENT, score INTEGER, note TEXT)")
9
+ conn.commit()
10
+ conn.close()
11
+
12
+ def save_goal(goal: str):
13
+ conn = sqlite3.connect("ai_data.db")
14
+ cursor = conn.cursor()
15
+ cursor.execute("INSERT INTO goals (text) VALUES (?)", (goal,))
16
+ conn.commit()
17
+ conn.close()
18
+
19
+ def list_goals():
20
+ conn = sqlite3.connect("ai_data.db")
21
+ cursor = conn.cursor()
22
+ cursor.execute("SELECT text FROM goals")
23
+ rows = cursor.fetchall()
24
+ conn.close()
25
+ return "\n".join(row[0] for row in rows)
26
+
27
+ def save_feedback(score: int, note: str = ""):
28
+ conn = sqlite3.connect("ai_data.db")
29
+ cursor = conn.cursor()
30
+ cursor.execute("INSERT INTO feedback (score, note) VALUES (?, ?)", (score, note))
31
+ conn.commit()
32
+ conn.close()
33
+
34
+ def list_feedback():
35
+ conn = sqlite3.connect("ai_data.db")
36
+ cursor = conn.cursor()
37
+ cursor.execute("SELECT score, note FROM feedback")
38
+ rows = cursor.fetchall()
39
+ conn.close()
40
+ return "\n".join(f"{score} - {note}" for score, note in rows)