Update app.py
Browse files
app.py
CHANGED
|
@@ -38,6 +38,9 @@ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
| 38 |
# Database configuration
|
| 39 |
DATABASE_PATH = '/tmp/chat_database.db'
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
# Initialize prompt template
|
| 42 |
prompt_template = """
|
| 43 |
Role: You are Figr Code Assistant, specializing in providing clear, error-free Python code solutions.
|
|
@@ -265,6 +268,26 @@ def init_db():
|
|
| 265 |
print(f"Error initializing database: {e}")
|
| 266 |
raise
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
# Rest of the prompt template and other configurations remain the same
|
| 270 |
prompt_template = """
|
|
@@ -445,6 +468,12 @@ def get_chat_list():
|
|
| 445 |
|
| 446 |
@app.route("/api/chat", methods=["POST"])
|
| 447 |
def chat():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
data = request.json
|
| 449 |
user_input = data.get("message", "")
|
| 450 |
session_id = data.get("sessionId", "default")
|
|
@@ -691,21 +720,7 @@ def home():
|
|
| 691 |
return render_template("index.html")
|
| 692 |
|
| 693 |
|
| 694 |
-
|
| 695 |
-
print("Initializing database...")
|
| 696 |
-
init_db()
|
| 697 |
-
print("Database initialized successfully")
|
| 698 |
-
|
| 699 |
-
print("Starting model initialization...")
|
| 700 |
-
llm = initialize_model()
|
| 701 |
-
print("Model initialized successfully")
|
| 702 |
-
|
| 703 |
-
print("Creating LLM chain...")
|
| 704 |
-
llm_chain = LLMChain(llm=llm, prompt=prompt)
|
| 705 |
-
print("LLM chain created successfully")
|
| 706 |
-
except Exception as e:
|
| 707 |
-
print(f"Initialization error: {e}")
|
| 708 |
-
raise
|
| 709 |
|
| 710 |
if __name__ == "__main__":
|
| 711 |
app.run(host="0.0.0.0", port=PORT)
|
|
|
|
| 38 |
# Database configuration
|
| 39 |
DATABASE_PATH = '/tmp/chat_database.db'
|
| 40 |
|
| 41 |
+
llm = None
|
| 42 |
+
llm_chain = None
|
| 43 |
+
|
| 44 |
# Initialize prompt template
|
| 45 |
prompt_template = """
|
| 46 |
Role: You are Figr Code Assistant, specializing in providing clear, error-free Python code solutions.
|
|
|
|
| 268 |
print(f"Error initializing database: {e}")
|
| 269 |
raise
|
| 270 |
|
| 271 |
+
def initialize_app():
|
| 272 |
+
"""Initialize all components of the application"""
|
| 273 |
+
global llm, llm_chain
|
| 274 |
+
|
| 275 |
+
try:
|
| 276 |
+
print("Initializing database...")
|
| 277 |
+
init_db()
|
| 278 |
+
print("Database initialized successfully")
|
| 279 |
+
|
| 280 |
+
print("Starting model initialization...")
|
| 281 |
+
llm = initialize_model()
|
| 282 |
+
print("Model initialized successfully")
|
| 283 |
+
|
| 284 |
+
print("Creating LLM chain...")
|
| 285 |
+
llm_chain = LLMChain(llm=llm, prompt=prompt)
|
| 286 |
+
print("LLM chain created successfully")
|
| 287 |
+
except Exception as e:
|
| 288 |
+
print(f"Initialization error: {e}")
|
| 289 |
+
raise
|
| 290 |
+
|
| 291 |
|
| 292 |
# Rest of the prompt template and other configurations remain the same
|
| 293 |
prompt_template = """
|
|
|
|
| 468 |
|
| 469 |
@app.route("/api/chat", methods=["POST"])
|
| 470 |
def chat():
|
| 471 |
+
global llm_chain
|
| 472 |
+
if llm_chain is None:
|
| 473 |
+
return jsonify({
|
| 474 |
+
"response": "System is still initializing. Please try again in a moment.",
|
| 475 |
+
"success": False
|
| 476 |
+
})
|
| 477 |
data = request.json
|
| 478 |
user_input = data.get("message", "")
|
| 479 |
session_id = data.get("sessionId", "default")
|
|
|
|
| 720 |
return render_template("index.html")
|
| 721 |
|
| 722 |
|
| 723 |
+
initialize_app()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 724 |
|
| 725 |
if __name__ == "__main__":
|
| 726 |
app.run(host="0.0.0.0", port=PORT)
|