-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
172 lines (143 loc) · 5.77 KB
/
app.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
from flask import Flask, request, jsonify, render_template, flash, redirect, url_for
from flask_cors import CORS
from werkzeug.utils import secure_filename
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import TextLoader, PyPDFLoader, CSVLoader
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain_openai import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from dotenv import load_dotenv
import sqlite3
from typing import List, Tuple
import logging
logging.basicConfig(level=logging.DEBUG)
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
raise ValueError("OPENAI_API_KEY not found in environment variables")
app = Flask(__name__)
CORS(app)
app.config['SECRET_KEY'] = os.urandom(24)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max-limit
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'csv'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def load_documents(file_path: str) -> List[str]:
try:
if file_path.endswith('.pdf'):
loader = PyPDFLoader(file_path)
elif file_path.endswith('.txt'):
loader = TextLoader(file_path, encoding='utf-8')
elif file_path.endswith('.csv'):
loader = CSVLoader(file_path)
else:
raise ValueError(f"Unsupported file type: {file_path}")
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
length_function=len,
)
texts = text_splitter.split_documents(documents)
return texts
except Exception as e:
logging.error(f"Error loading file {file_path}: {str(e)}")
raise
with open("bad.txt", "r") as f:
bad_words = set(word.strip().lower() for word in f)
def filter_bad_words(text: str) -> str:
words = text.split()
filtered_words = [word if word.lower() not in bad_words else "[FILTERED]" for word in words]
return " ".join(filtered_words)
def setup_database():
conn = sqlite3.connect('company_data.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS employees (
id INTEGER PRIMARY KEY,
name TEXT,
department TEXT,
position TEXT
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS hr_policies (
id INTEGER PRIMARY KEY,
policy_name TEXT,
description TEXT
)
''')
conn.commit()
return conn
def query_database(query: str) -> List[Tuple]:
conn = setup_database()
cursor = conn.cursor()
cursor.execute(query)
results = cursor.fetchall()
conn.close()
return results
embeddings = OpenAIEmbeddings()
docsearch = FAISS.from_texts(["Initial document"], embeddings)
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
llm = ChatOpenAI(temperature=0.7, model="gpt-3.5-turbo-16k")
qa = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=docsearch.as_retriever(search_kwargs={"k": 3}),
memory=memory
)
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
try:
texts = load_documents(file_path)
docsearch.add_documents(texts)
flash(f'File {filename} has been processed and added to the knowledge base.')
except Exception as e:
logging.error(f"Error processing file {filename}: {str(e)}")
flash(f'Error processing file: {str(e)}')
return redirect(url_for('upload_file'))
return render_template('upload.html')
@app.route('/chat', methods=['POST'])
def chat():
user_query = request.json.get('query', '')
logging.debug(f"Received chat query: {user_query}")
filtered_query = filter_bad_words(user_query)
if filtered_query != user_query:
return jsonify({"response": "Your message contained inappropriate language and has been filtered."}), 200
try:
if "employee" in filtered_query.lower() or "hr policy" in filtered_query.lower():
if "employee" in filtered_query.lower():
db_query = "SELECT * FROM employees"
else:
db_query = "SELECT * FROM hr_policies"
db_results = query_database(db_query)
formatted_results = "\n".join([str(row) for row in db_results])
combined_query = f"{filtered_query}\n\nRelevant database information:\n{formatted_results}"
response = qa({"query": combined_query})
else:
response = qa({"query": filtered_query})
filtered_response = filter_bad_words(response['result'])
logging.debug(f"Chat response: {filtered_response}")
return jsonify({"response": filtered_response}), 200
except Exception as e:
logging.error(f"Error in chat route: {str(e)}")
return jsonify({"response": "I'm sorry, but I encountered an error while processing your request."}), 500
if __name__ == '__main__':
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
app.run(debug=True)