File size: 3,755 Bytes
1f004f6
 
a556cd0
1f004f6
50d3cd7
 
 
1f004f6
 
 
a556cd0
 
 
 
1f004f6
a556cd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d3cd7
a556cd0
 
 
 
1f004f6
 
 
a556cd0
 
 
1f004f6
a556cd0
 
 
 
 
 
 
 
 
 
 
 
 
1f004f6
a556cd0
 
 
 
1f004f6
 
a556cd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f004f6
 
a556cd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sqlite3
import os
import time

# Use a non-hidden filename in the current directory
DB_NAME = "model_tests.db"  # No leading dot
DB_PATH = os.path.join(os.getcwd(), DB_NAME)  # Use absolute path with getcwd()

def init_db():
    """Initialize the SQLite database with required tables if they don't exist"""
    try:
        print(f"Initializing database at {DB_PATH}")
        # Ensure the directory exists
        os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
        
        with sqlite3.connect(DB_PATH) as conn:
            cursor = conn.cursor()
            
            # Create table for model inputs and outputs
            cursor.execute('''
            CREATE TABLE IF NOT EXISTS model_tests (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                analysis_mode TEXT NOT NULL,
                system_prompt TEXT NOT NULL,
                input_content TEXT,
                model_response TEXT NOT NULL,
                generation_time REAL NOT NULL,
                tokens_generated INTEGER NOT NULL,
                temperature REAL NOT NULL,
                top_p REAL NOT NULL,
                max_length INTEGER NOT NULL,
                timestamp TEXT NOT NULL
            )
            ''')
            
            conn.commit()
        print(f"Test database initialized successfully at {DB_PATH}")
        return True
    except Exception as e:
        print(f"Error initializing database: {e}")
        return False

def save_test_result(analysis_mode, system_prompt, input_content, model_response, 
                    generation_time, tokens_generated, temperature, top_p, max_length):
    """Save a test result to the database"""
    try:
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        
        with sqlite3.connect(DB_PATH) as conn:
            cursor = conn.cursor()
            
            cursor.execute('''
            INSERT INTO model_tests 
            (analysis_mode, system_prompt, input_content, model_response, 
            generation_time, tokens_generated, temperature, top_p, max_length, timestamp)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            ''', (analysis_mode, system_prompt, input_content, model_response, 
                generation_time, tokens_generated, temperature, top_p, max_length, timestamp))
            
            test_id = cursor.lastrowid
            conn.commit()
        
        return test_id
    except Exception as e:
        print(f"Error saving test result: {e}")
        return None

def get_test_history(limit=10):
    """Get recent test history"""
    try:
        with sqlite3.connect(DB_PATH) as conn:
            cursor = conn.cursor()
            cursor.execute('''
            SELECT id, analysis_mode, timestamp, generation_time, tokens_generated 
            FROM model_tests 
            ORDER BY id DESC LIMIT ?
            ''', (limit,))
            history = cursor.fetchall()
        
        return history
    except Exception as e:
        print(f"Error getting test history: {e}")
        return []

def get_test_details(test_id):
    """Get details for a specific test"""
    try:
        with sqlite3.connect(DB_PATH) as conn:
            cursor = conn.cursor()
            cursor.execute('''
            SELECT * FROM model_tests WHERE id = ?
            ''', (test_id,))
            test = cursor.fetchone()
            
            if test:
                # Convert to dictionary
                columns = [col[0] for col in cursor.description]
                test_dict = {columns[i]: test[i] for i in range(len(columns))}
                return test_dict
            else:
                return None
    except Exception as e:
        print(f"Error getting test details: {e}")
        return None