cryogenic22 commited on
Commit
079e0f2
·
verified ·
1 Parent(s): b7836f7

Update utils/database.py

Browse files
Files changed (1) hide show
  1. utils/database.py +3 -104
utils/database.py CHANGED
@@ -16,99 +16,8 @@ from langchain_core.runnables import RunnablePassthrough
16
  import os
17
  import streamlit as st
18
 
19
- def create_connection(db_file):
20
- try:
21
- conn = sqlite3.connect(db_file)
22
- return conn
23
- except Error as e:
24
- st.error(f"Error: {e}")
25
- return None
26
-
27
-
28
- def create_tables(conn):
29
- try:
30
- sql_create_documents_table = """
31
- CREATE TABLE IF NOT EXISTS documents (
32
- id INTEGER PRIMARY KEY AUTOINCREMENT,
33
- name TEXT NOT NULL,
34
- content TEXT NOT NULL,
35
- upload_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
36
- );
37
- """
38
-
39
- sql_create_queries_table = """
40
- CREATE TABLE IF NOT EXISTS queries (
41
- id INTEGER PRIMARY KEY AUTOINCREMENT,
42
- query TEXT NOT NULL,
43
- response TEXT NOT NULL,
44
- document_id INTEGER,
45
- query_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
46
- FOREIGN KEY (document_id) REFERENCES documents (id)
47
- );
48
- """
49
-
50
- sql_create_annotations_table = """
51
- CREATE TABLE IF NOT EXISTS annotations (
52
- id INTEGER PRIMARY KEY AUTOINCREMENT,
53
- document_id INTEGER NOT NULL,
54
- annotation TEXT NOT NULL,
55
- page_number INTEGER,
56
- annotation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
57
- FOREIGN KEY (document_id) REFERENCES documents (id)
58
- );
59
- """
60
-
61
- c = conn.cursor()
62
- c.execute(sql_create_documents_table)
63
- c.execute(sql_create_queries_table)
64
- c.execute(sql_create_annotations_table)
65
- except Error as e:
66
- st.error(f"Error: {e}")
67
-
68
-
69
- def get_documents(conn):
70
- """Retrieve documents from database"""
71
- try:
72
- cursor = conn.cursor()
73
- cursor.execute(
74
- "SELECT id, name, upload_date FROM documents ORDER BY upload_date DESC"
75
- )
76
- return cursor.fetchall()
77
- except Exception as e:
78
- st.error(f"Error retrieving documents: {e}")
79
- return []
80
-
81
-
82
- def insert_document(conn, doc_name, doc_content):
83
- """Insert a document into database"""
84
- try:
85
- cursor = conn.cursor()
86
- cursor.execute("SELECT id FROM documents WHERE name = ?", (doc_name,))
87
- if not cursor.fetchone():
88
- conn.execute(
89
- "INSERT INTO documents (name, content) VALUES (?, ?)",
90
- (doc_name, doc_content),
91
- )
92
- return True
93
- except Exception as e:
94
- st.error(f"Error inserting document: {e}")
95
- return False
96
-
97
-
98
- def format_chat_history(messages: list[BaseMessage]) -> list[dict]:
99
- """Convert chat history to the format expected by langchain"""
100
- formatted = []
101
- for msg in messages:
102
- if isinstance(msg, HumanMessage):
103
- formatted.append({"role": "user", "content": msg.content})
104
- elif isinstance(msg, AIMessage):
105
- formatted.append({"role": "assistant", "content": msg.content})
106
- elif isinstance(msg, SystemMessage):
107
- formatted.append({"role": "system", "content": msg.content})
108
- return formatted
109
-
110
  def initialize_qa_system(vector_store):
111
-
112
  try:
113
  llm = ChatOpenAI(
114
  temperature=0.5,
@@ -170,7 +79,7 @@ def initialize_qa_system(vector_store):
170
  return None
171
 
172
  def initialize_faiss(embeddings, documents, document_names):
173
-
174
  try:
175
  from langchain.vectorstores import FAISS
176
 
@@ -187,7 +96,7 @@ def initialize_faiss(embeddings, documents, document_names):
187
 
188
  @st.cache_resource
189
  def get_embeddings_model():
190
-
191
  try:
192
  from langchain.embeddings import HuggingFaceEmbeddings
193
 
@@ -197,13 +106,3 @@ def get_embeddings_model():
197
  except Exception as e:
198
  st.error(f"Error loading embeddings model: {e}")
199
  return None
200
- # Defining the file paths explicitly again before writing the updated content
201
- components_chat_path = 'components/chat.py'
202
- utils_database_path = 'utils/database.py'
203
-
204
- # Writing the updated contents back to the original files
205
- with open(components_chat_path, 'w') as chat_file:
206
- chat_file.write(updated_components_chat_content)
207
-
208
- with open(utils_database_path, 'w') as database_file:
209
- database_file.write(updated_utils_database_content)
 
16
  import os
17
  import streamlit as st
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def initialize_qa_system(vector_store):
20
+ """Initialize QA system with proper chat handling"""
21
  try:
22
  llm = ChatOpenAI(
23
  temperature=0.5,
 
79
  return None
80
 
81
  def initialize_faiss(embeddings, documents, document_names):
82
+ """Initialize FAISS vector store"""
83
  try:
84
  from langchain.vectorstores import FAISS
85
 
 
96
 
97
  @st.cache_resource
98
  def get_embeddings_model():
99
+ """Get the embeddings model"""
100
  try:
101
  from langchain.embeddings import HuggingFaceEmbeddings
102
 
 
106
  except Exception as e:
107
  st.error(f"Error loading embeddings model: {e}")
108
  return None