khankashif commited on
Commit
3c91d73
·
verified ·
1 Parent(s): 331f75e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+
3
+ # Step 1: Connect to the database (or create it if it doesn't exist)
4
+ conn = sqlite3.connect('huggingface_app.db')
5
+ cursor = conn.cursor()
6
+
7
+ # Step 2: Create tables
8
+ def create_tables():
9
+ cursor.execute('''
10
+ CREATE TABLE IF NOT EXISTS users (
11
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
12
+ username TEXT NOT NULL,
13
+ email TEXT UNIQUE
14
+ )
15
+ ''')
16
+ cursor.execute('''
17
+ CREATE TABLE IF NOT EXISTS model_interactions (
18
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
19
+ user_id INTEGER,
20
+ query TEXT NOT NULL,
21
+ response TEXT NOT NULL,
22
+ model_name TEXT NOT NULL,
23
+ timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
24
+ FOREIGN KEY (user_id) REFERENCES users(id)
25
+ )
26
+ ''')
27
+ print("Tables created successfully!")
28
+
29
+ # Step 3: Insert Data
30
+ def insert_user(username, email):
31
+ cursor.execute('''
32
+ INSERT INTO users (username, email)
33
+ VALUES (?, ?)
34
+ ''', (username, email))
35
+ conn.commit()
36
+ print(f"User {username} added.")
37
+
38
+ def log_interaction(user_id, query, response, model_name):
39
+ cursor.execute('''
40
+ INSERT INTO model_interactions (user_id, query, response, model_name)
41
+ VALUES (?, ?, ?, ?)
42
+ ''', (user_id, query, response, model_name))
43
+ conn.commit()
44
+ print("Interaction logged.")
45
+
46
+ # Step 4: Query Data
47
+ def get_user_interactions(user_id):
48
+ cursor.execute('''
49
+ SELECT * FROM model_interactions WHERE user_id = ?
50
+ ''', (user_id,))
51
+ return cursor.fetchall()
52
+
53
+ # Step 5: Close connection
54
+ def close_connection():
55
+ conn.close()
56
+ print("Database connection closed.")
57
+
58
+ # Initialize the database
59
+ if __name__ == "__main__":
60
+ create_tables()
61
+
62
+ # Example usage
63
+ insert_user("test_user", "test@example.com")
64
+ log_interaction(1, "What is AI?", "AI stands for Artificial Intelligence.", "gpt-4")
65
+
66
+ # Fetch user interactions
67
+ interactions = get_user_interactions(1)
68
+ for interaction in interactions:
69
+ print(interaction)
70
+
71
+ close_connection()