roshnn24 commited on
Commit
5a2eb7b
·
verified ·
1 Parent(s): 8fae0f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -52
app.py CHANGED
@@ -38,6 +38,17 @@ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
38
  # Database configuration
39
  DATABASE_PATH = '/tmp/chat_database.db'
40
 
 
 
 
 
 
 
 
 
 
 
 
41
  def initialize_model():
42
  """Initialize the model with appropriate settings"""
43
  try:
@@ -78,57 +89,6 @@ def initialize_model():
78
  print(f"Error initializing model: {e}")
79
  raise
80
 
81
- # Initialize database
82
- def init_db():
83
- """Initialize the database"""
84
- try:
85
- with get_db_connection() as conn:
86
- conn.execute('''
87
- CREATE TABLE IF NOT EXISTS chats (
88
- id TEXT PRIMARY KEY,
89
- title TEXT,
90
- date TEXT,
91
- last_message TEXT
92
- )
93
- ''')
94
-
95
- conn.execute('''
96
- CREATE TABLE IF NOT EXISTS messages (
97
- id INTEGER PRIMARY KEY AUTOINCREMENT,
98
- chat_id TEXT,
99
- role TEXT,
100
- content TEXT,
101
- timestamp TEXT,
102
- FOREIGN KEY (chat_id) REFERENCES chats (id)
103
- )
104
- ''')
105
-
106
- conn.execute('''
107
- CREATE TABLE IF NOT EXISTS important_info (
108
- id INTEGER PRIMARY KEY AUTOINCREMENT,
109
- chat_id TEXT,
110
- content TEXT,
111
- FOREIGN KEY (chat_id) REFERENCES chats (id)
112
- )
113
- ''')
114
- conn.commit()
115
- except Exception as e:
116
- print(f"Error initializing database: {e}")
117
- raise
118
-
119
- # Initialize the application
120
- try:
121
- print("Initializing database...")
122
- init_db()
123
- print("Database initialized successfully")
124
-
125
- print("Starting model initialization...")
126
- llm = initialize_model()
127
- print("Model initialized successfully")
128
- except Exception as e:
129
- print(f"Initialization error: {e}")
130
- raise
131
-
132
 
133
  class ChatSession:
134
  def __init__(self, session_id):
@@ -235,6 +195,44 @@ class ChatSession:
235
  self.important_info = []
236
 
237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  # Rest of the prompt template and other configurations remain the same
239
  prompt_template = """
240
  Role: You are Figr Code Assistant, specializing in providing clear, error-free Python code solutions.
@@ -660,6 +658,17 @@ def home():
660
  return render_template("index.html")
661
 
662
 
663
- if __name__ == "__main__":
 
664
  init_db()
 
 
 
 
 
 
 
 
 
 
665
  app.run(host="0.0.0.0", port=PORT)
 
38
  # Database configuration
39
  DATABASE_PATH = '/tmp/chat_database.db'
40
 
41
+ # Database connection manager
42
+ @contextmanager
43
+ def get_db_connection():
44
+ """Context manager for database connections"""
45
+ conn = sqlite3.connect(DATABASE_PATH)
46
+ conn.row_factory = sqlite3.Row
47
+ try:
48
+ yield conn
49
+ finally:
50
+ conn.close()
51
+
52
  def initialize_model():
53
  """Initialize the model with appropriate settings"""
54
  try:
 
89
  print(f"Error initializing model: {e}")
90
  raise
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  class ChatSession:
94
  def __init__(self, session_id):
 
195
  self.important_info = []
196
 
197
 
198
+ def init_db():
199
+ """Initialize the database"""
200
+ try:
201
+ with get_db_connection() as conn:
202
+ conn.execute('''
203
+ CREATE TABLE IF NOT EXISTS chats (
204
+ id TEXT PRIMARY KEY,
205
+ title TEXT,
206
+ date TEXT,
207
+ last_message TEXT
208
+ )
209
+ ''')
210
+
211
+ conn.execute('''
212
+ CREATE TABLE IF NOT EXISTS messages (
213
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
214
+ chat_id TEXT,
215
+ role TEXT,
216
+ content TEXT,
217
+ timestamp TEXT,
218
+ FOREIGN KEY (chat_id) REFERENCES chats (id)
219
+ )
220
+ ''')
221
+
222
+ conn.execute('''
223
+ CREATE TABLE IF NOT EXISTS important_info (
224
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
225
+ chat_id TEXT,
226
+ content TEXT,
227
+ FOREIGN KEY (chat_id) REFERENCES chats (id)
228
+ )
229
+ ''')
230
+ conn.commit()
231
+ except Exception as e:
232
+ print(f"Error initializing database: {e}")
233
+ raise
234
+
235
+
236
  # Rest of the prompt template and other configurations remain the same
237
  prompt_template = """
238
  Role: You are Figr Code Assistant, specializing in providing clear, error-free Python code solutions.
 
658
  return render_template("index.html")
659
 
660
 
661
+ try:
662
+ print("Initializing database...")
663
  init_db()
664
+ print("Database initialized successfully")
665
+
666
+ print("Starting model initialization...")
667
+ llm = initialize_model()
668
+ print("Model initialized successfully")
669
+ except Exception as e:
670
+ print(f"Initialization error: {e}")
671
+ raise
672
+
673
+ if __name__ == "__main__":
674
  app.run(host="0.0.0.0", port=PORT)