AumCoreAI commited on
Commit
f969923
·
verified ·
1 Parent(s): 3f47213

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -31
app.py CHANGED
@@ -1,7 +1,6 @@
1
- # app.py - FINAL VERSION WITH LANGUAGE DETECTION
2
  import os
3
  import uvicorn
4
- import json
5
  from fastapi import FastAPI, Form
6
  from fastapi.responses import HTMLResponse, JSONResponse
7
  from groq import Groq
@@ -10,26 +9,8 @@ from groq import Groq
10
  app = FastAPI()
11
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
12
 
13
- # Configuration & Persistent Memory Setup
14
  USERNAME = "Sanjay"
15
- MEMORY_FILE = "memory.json"
16
-
17
- def load_memory():
18
- if os.path.exists(MEMORY_FILE):
19
- try:
20
- with open(MEMORY_FILE, 'r') as f:
21
- data = json.load(f)
22
- return data if "history" in data else {"history": []}
23
- except Exception as e:
24
- return {"history": []}
25
- return {"history": []}
26
-
27
- def save_memory(chat_history):
28
- try:
29
- with open(MEMORY_FILE, 'w') as f:
30
- json.dump({"history": chat_history}, f, indent=4)
31
- except Exception as e:
32
- print(f"Memory Save Error: {e}")
33
 
34
  # UI REMAINS EXACTLY SAME
35
  HTML_UI = '''
@@ -71,8 +52,8 @@ HTML_UI = '''
71
  <div class="nav-item reset-btn" onclick="confirmReset()"><i class="fas fa-trash-alt"></i> Reset Memory</div>
72
  <div class="nav-item"><i class="fas fa-cog"></i> Settings</div>
73
  </div>
74
- </div>
75
- <div class="main-chat">
76
  <div id="chat-log" class="chat-box"></div>
77
  <div class="input-area">
78
  <div class="input-container">
@@ -121,23 +102,33 @@ async def get_ui():
121
 
122
  @app.post("/reset")
123
  async def reset():
124
- save_memory([])
125
  return {"message": "Memory clear ho gayi!"}
126
 
127
  @app.post("/chat")
128
  async def chat(message: str = Form(...)):
129
  # ✅ LANGUAGE DETECTION
130
  from language_detector import detect_input_language, get_system_prompt
 
 
131
  lang_mode = detect_input_language(message)
132
  system_prompt = get_system_prompt(lang_mode, USERNAME)
133
 
134
- memory_data = load_memory()
135
- history = memory_data.get("history", [])[-10:]
 
 
 
 
136
 
 
137
  api_messages = [{"role": "system", "content": system_prompt}]
138
- for chat_pair in history:
139
- api_messages.append({"role": "user", "content": chat_pair["u"]})
140
- api_messages.append({"role": "assistant", "content": chat_pair["a"]})
 
 
 
141
 
142
  api_messages.append({"role": "user", "content": message})
143
 
@@ -149,8 +140,13 @@ async def chat(message: str = Form(...)):
149
  max_tokens=800
150
  )
151
  ai_response = completion.choices[0].message.content.strip()
152
- history.append({"u": message, "a": ai_response})
153
- save_memory(history)
 
 
 
 
 
154
  return {"response": ai_response}
155
  except Exception as e:
156
  return {"response": f"Error: {str(e)}"}
 
1
+ # app.py - FINAL VERSION WITH TiDB INTEGRATION
2
  import os
3
  import uvicorn
 
4
  from fastapi import FastAPI, Form
5
  from fastapi.responses import HTMLResponse, JSONResponse
6
  from groq import Groq
 
9
  app = FastAPI()
10
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
11
 
12
+ # Configuration
13
  USERNAME = "Sanjay"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  # UI REMAINS EXACTLY SAME
16
  HTML_UI = '''
 
52
  <div class="nav-item reset-btn" onclick="confirmReset()"><i class="fas fa-trash-alt"></i> Reset Memory</div>
53
  <div class="nav-item"><i class="fas fa-cog"></i> Settings</div>
54
  </div>
55
+ </div>
56
+ <div class="main-chat">
57
  <div id="chat-log" class="chat-box"></div>
58
  <div class="input-area">
59
  <div class="input-container">
 
102
 
103
  @app.post("/reset")
104
  async def reset():
105
+ # TiDB reset logic (baad mein add karna)
106
  return {"message": "Memory clear ho gayi!"}
107
 
108
  @app.post("/chat")
109
  async def chat(message: str = Form(...)):
110
  # ✅ LANGUAGE DETECTION
111
  from language_detector import detect_input_language, get_system_prompt
112
+ from memory_db import tidb_memory # ✅ TiDB IMPORT
113
+
114
  lang_mode = detect_input_language(message)
115
  system_prompt = get_system_prompt(lang_mode, USERNAME)
116
 
117
+ # GET HISTORY FROM TiDB (JSON ki jagah)
118
+ recent_chats = []
119
+ try:
120
+ recent_chats = tidb_memory.get_recent_chats(limit=10)
121
+ except Exception as e:
122
+ print(f"TiDB history fetch error: {e}")
123
 
124
+ # Build messages
125
  api_messages = [{"role": "system", "content": system_prompt}]
126
+
127
+ # Add history from TiDB
128
+ for chat_row in recent_chats:
129
+ user_input, ai_response, _ = chat_row
130
+ api_messages.append({"role": "user", "content": user_input})
131
+ api_messages.append({"role": "assistant", "content": ai_response})
132
 
133
  api_messages.append({"role": "user", "content": message})
134
 
 
140
  max_tokens=800
141
  )
142
  ai_response = completion.choices[0].message.content.strip()
143
+
144
+ # ✅ SAVE TO TiDB
145
+ try:
146
+ tidb_memory.save_chat(message, ai_response, lang_mode)
147
+ except Exception as e:
148
+ print(f"TiDB save error: {e}")
149
+
150
  return {"response": ai_response}
151
  except Exception as e:
152
  return {"response": f"Error: {str(e)}"}