AumCoreAI commited on
Commit
3bc54f0
·
verified ·
1 Parent(s): 1d47910

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -1
app.py CHANGED
@@ -532,7 +532,39 @@ async def chat(message: str = Form(...)):
532
 
533
  lang_mode = detect_input_language(message)
534
  system_prompt = get_system_prompt(lang_mode, USERNAME)
535
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
536
  # ✅ GET HISTORY FROM TiDB (instead of JSON)
537
  recent_chats = []
538
  try:
 
532
 
533
  lang_mode = detect_input_language(message)
534
  system_prompt = get_system_prompt(lang_mode, USERNAME)
535
+ @app.post("/chat")
536
+ async def chat(message: str = Form(...)):
537
+ # ✅ LANGUAGE DETECTION
538
+ from language_detector import detect_input_language, get_system_prompt, generate_basic_code
539
+ from memory_db import tidb_memory # ✅ TiDB IMPORT
540
+
541
+ lang_mode = detect_input_language(message)
542
+ system_prompt = get_system_prompt(lang_mode, USERNAME)
543
+
544
+ # --- CODE DETECTION BLOCK ---
545
+ msg_lower = message.lower()
546
+ CODE_KEYWORDS = [
547
+ "code", "script", "program", "function",
548
+ "mount", "google drive", "colab",
549
+ "python", "build", "create", "generate"
550
+ ]
551
+
552
+ if any(k in msg_lower for k in CODE_KEYWORDS):
553
+ code_response = generate_basic_code(message)
554
+ try:
555
+ tidb_memory.save_chat(message, code_response, lang_mode)
556
+ except Exception as e:
557
+ print(f"⚠️ TiDB save error (code): {e}")
558
+ return {"response": code_response}
559
+
560
+ # ✅ GET HISTORY FROM TiDB (instead of JSON)
561
+ recent_chats = []
562
+ try:
563
+ recent_chats = tidb_memory.get_recent_chats(limit=10)
564
+ except Exception as e:
565
+ print(f"⚠️ TiDB history fetch error: {e}")
566
+ # Fallback to empty history if TiDB fails
567
+
568
  # ✅ GET HISTORY FROM TiDB (instead of JSON)
569
  recent_chats = []
570
  try: