Spaces:
Running
Running
Hasan-Atris3
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,269 +7,6 @@ from db import SessionLocal, User, Chat, Message
|
|
| 7 |
|
| 8 |
claude = ClaudeAnswerer()
|
| 9 |
|
| 10 |
-
# app.py
|
| 11 |
-
import time
|
| 12 |
-
import chainlit as cl
|
| 13 |
-
|
| 14 |
-
from srd_engine_v2 import SmartKnowledgeBase, ClaudeAnswerer
|
| 15 |
-
from db import SessionLocal, User, Chat, Message
|
| 16 |
-
|
| 17 |
-
claude = ClaudeAnswerer()
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
@cl.on_chat_start
|
| 21 |
-
async def start():
|
| 22 |
-
session = cl.user_session
|
| 23 |
-
db = SessionLocal()
|
| 24 |
-
try:
|
| 25 |
-
# -----------------------
|
| 26 |
-
# USER IDENTIFICATION
|
| 27 |
-
# -----------------------
|
| 28 |
-
if not session.get("user_id"):
|
| 29 |
-
user = User()
|
| 30 |
-
db.add(user)
|
| 31 |
-
db.commit()
|
| 32 |
-
session.set("user_id", user.id)
|
| 33 |
-
|
| 34 |
-
user_id = session.get("user_id")
|
| 35 |
-
|
| 36 |
-
# -----------------------
|
| 37 |
-
# CHAT SELECTION
|
| 38 |
-
# -----------------------
|
| 39 |
-
chats = db.query(Chat).filter(Chat.user_id == user_id).all()
|
| 40 |
-
|
| 41 |
-
actions = [cl.Action(name="new_chat", payload={}, label="➕ New Project Chat")]
|
| 42 |
-
for c in chats[-5:]:
|
| 43 |
-
actions.append(
|
| 44 |
-
cl.Action(
|
| 45 |
-
name="resume_chat",
|
| 46 |
-
payload={"chat_id": c.id},
|
| 47 |
-
label=f"📂 {c.project_name}"
|
| 48 |
-
)
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
res = await cl.AskActionMessage(
|
| 52 |
-
content="Choose a chat or start a new one:",
|
| 53 |
-
actions=actions
|
| 54 |
-
).send()
|
| 55 |
-
|
| 56 |
-
if not res:
|
| 57 |
-
return
|
| 58 |
-
|
| 59 |
-
# -----------------------
|
| 60 |
-
# NEW CHAT
|
| 61 |
-
# -----------------------
|
| 62 |
-
if res["name"] == "new_chat":
|
| 63 |
-
project_res = await cl.AskUserMessage(
|
| 64 |
-
content="Enter **Project Name**:",
|
| 65 |
-
timeout=300
|
| 66 |
-
).send()
|
| 67 |
-
if not project_res:
|
| 68 |
-
return
|
| 69 |
-
project_name = project_res["output"]
|
| 70 |
-
|
| 71 |
-
learn_res = await cl.AskActionMessage(
|
| 72 |
-
content="Allow this chat to be saved/learned for improving the bot?",
|
| 73 |
-
actions=[
|
| 74 |
-
cl.Action(name="learn_yes", payload={"v": True}, label="✅ Yes (Enable Learning)"),
|
| 75 |
-
cl.Action(name="learn_no", payload={"v": False}, label="❌ No (Do Not Learn)"),
|
| 76 |
-
],
|
| 77 |
-
).send()
|
| 78 |
-
learning_enabled = bool(learn_res["payload"]["v"]) if learn_res else True
|
| 79 |
-
|
| 80 |
-
chat = Chat(user_id=user_id, project_name=project_name, learning_enabled=learning_enabled)
|
| 81 |
-
db.add(chat)
|
| 82 |
-
db.commit()
|
| 83 |
-
|
| 84 |
-
session.set("chat_id", chat.id)
|
| 85 |
-
session.set("learning_enabled", chat.learning_enabled)
|
| 86 |
-
|
| 87 |
-
engine = SmartKnowledgeBase(chroma_dir="chroma_global_db")
|
| 88 |
-
engine.set_current_project(project_name)
|
| 89 |
-
engine.set_current_chat(chat.id) # ✅ NEW
|
| 90 |
-
engine.set_current_user(user_id) # ✅ NEW
|
| 91 |
-
session.set("engine", engine)
|
| 92 |
-
|
| 93 |
-
await run_ingestion(engine)
|
| 94 |
-
|
| 95 |
-
# -----------------------
|
| 96 |
-
# RESUME CHAT
|
| 97 |
-
# -----------------------
|
| 98 |
-
else:
|
| 99 |
-
chat_id = res["payload"]["chat_id"]
|
| 100 |
-
chat = db.query(Chat).get(chat_id)
|
| 101 |
-
if not chat:
|
| 102 |
-
await cl.Message(content="⚠️ Chat not found.").send()
|
| 103 |
-
return
|
| 104 |
-
|
| 105 |
-
session.set("chat_id", chat.id)
|
| 106 |
-
session.set("learning_enabled", chat.learning_enabled)
|
| 107 |
-
|
| 108 |
-
engine = SmartKnowledgeBase(chroma_dir="chroma_global_db")
|
| 109 |
-
engine.set_current_project(chat.project_name)
|
| 110 |
-
engine.set_current_chat(chat.id) # ✅ NEW
|
| 111 |
-
engine.set_current_user(user_id) # ✅ NEW
|
| 112 |
-
session.set("engine", engine)
|
| 113 |
-
|
| 114 |
-
# Restore history
|
| 115 |
-
messages = (
|
| 116 |
-
db.query(Message)
|
| 117 |
-
.filter(Message.chat_id == chat_id)
|
| 118 |
-
.order_by(Message.created_at)
|
| 119 |
-
.all()
|
| 120 |
-
)
|
| 121 |
-
for m in messages:
|
| 122 |
-
await cl.Message(content=m.content, author=m.role).send()
|
| 123 |
-
|
| 124 |
-
finally:
|
| 125 |
-
db.close()
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
async def run_ingestion(engine: SmartKnowledgeBase):
|
| 129 |
-
files = await cl.AskFileMessage(
|
| 130 |
-
content="Upload the **SRD PDF**:",
|
| 131 |
-
accept=["application/pdf"],
|
| 132 |
-
max_size_mb=50,
|
| 133 |
-
timeout=600
|
| 134 |
-
).send()
|
| 135 |
-
if not files:
|
| 136 |
-
return
|
| 137 |
-
|
| 138 |
-
srd_file_path = files[0].path
|
| 139 |
-
|
| 140 |
-
res = await cl.AskActionMessage(
|
| 141 |
-
content="Select Diagram Vision Mode:",
|
| 142 |
-
actions=[
|
| 143 |
-
cl.Action(name="qwen", payload={"v": "qwen"}, label="Qwen"),
|
| 144 |
-
cl.Action(name="claude", payload={"v": "claude"}, label="Claude"),
|
| 145 |
-
cl.Action(name="both", payload={"v": "both"}, label="Both"),
|
| 146 |
-
cl.Action(name="none", payload={"v": "none"}, label="None"),
|
| 147 |
-
]
|
| 148 |
-
).send()
|
| 149 |
-
|
| 150 |
-
mode = res["payload"]["v"] if res else "none"
|
| 151 |
-
use_qwen = mode in ("qwen", "both")
|
| 152 |
-
use_claude = mode in ("claude", "both")
|
| 153 |
-
|
| 154 |
-
status = cl.Message(content="🚀 Starting ingestion...")
|
| 155 |
-
await status.send()
|
| 156 |
-
|
| 157 |
-
await cl.make_async(engine.process_document_step)(
|
| 158 |
-
srd_file_path, "pdf_text", "SRD Main", False, False
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
status.content += "\n✅ SRD indexed"
|
| 162 |
-
await status.update()
|
| 163 |
-
|
| 164 |
-
while True:
|
| 165 |
-
add = await cl.AskActionMessage(
|
| 166 |
-
content="Add a diagram?",
|
| 167 |
-
actions=[
|
| 168 |
-
cl.Action(name="yes", payload={}, label="➕ Add"),
|
| 169 |
-
cl.Action(name="done", payload={}, label="Done"),
|
| 170 |
-
]
|
| 171 |
-
).send()
|
| 172 |
-
|
| 173 |
-
if not add or add["name"] == "done":
|
| 174 |
-
break
|
| 175 |
-
|
| 176 |
-
title = await cl.AskUserMessage(content="Diagram title:", timeout=300).send()
|
| 177 |
-
if not title:
|
| 178 |
-
break
|
| 179 |
-
|
| 180 |
-
file = await cl.AskFileMessage(
|
| 181 |
-
content="Upload diagram:",
|
| 182 |
-
accept=["image/png", "image/jpeg", "application/pdf"],
|
| 183 |
-
max_size_mb=20,
|
| 184 |
-
timeout=600
|
| 185 |
-
).send()
|
| 186 |
-
|
| 187 |
-
if not file:
|
| 188 |
-
break
|
| 189 |
-
|
| 190 |
-
await cl.make_async(engine.process_document_step)(
|
| 191 |
-
file[0].path, "diagram", title["output"], use_qwen, use_claude
|
| 192 |
-
)
|
| 193 |
-
|
| 194 |
-
status.content += f"\n🎨 Diagram '{title['output']}' indexed"
|
| 195 |
-
await status.update()
|
| 196 |
-
|
| 197 |
-
status.content += "\n🎉 Ingestion complete. Ask questions!"
|
| 198 |
-
await status.update()
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
@cl.on_message
|
| 202 |
-
async def main(message: cl.Message):
|
| 203 |
-
session = cl.user_session
|
| 204 |
-
db = SessionLocal()
|
| 205 |
-
try:
|
| 206 |
-
engine: SmartKnowledgeBase = session.get("engine")
|
| 207 |
-
chat_id = session.get("chat_id")
|
| 208 |
-
|
| 209 |
-
if not engine or not chat_id:
|
| 210 |
-
await cl.Message(content="⚠️ Session error. Please refresh.").send()
|
| 211 |
-
return
|
| 212 |
-
|
| 213 |
-
# Save user msg
|
| 214 |
-
db.add(Message(chat_id=chat_id, role="user", content=message.content))
|
| 215 |
-
db.commit()
|
| 216 |
-
|
| 217 |
-
# Answer
|
| 218 |
-
response = await cl.make_async(engine.generate_smart_response)(message.content, claude)
|
| 219 |
-
|
| 220 |
-
# Save assistant msg
|
| 221 |
-
db.add(Message(chat_id=chat_id, role="assistant", content=response))
|
| 222 |
-
db.commit()
|
| 223 |
-
|
| 224 |
-
await cl.Message(content=response).send()
|
| 225 |
-
|
| 226 |
-
# Feedback
|
| 227 |
-
await cl.Message(
|
| 228 |
-
content="",
|
| 229 |
-
actions=[
|
| 230 |
-
cl.Action(
|
| 231 |
-
name="correct",
|
| 232 |
-
payload={"original": message.content},
|
| 233 |
-
label="🔧 Correct This"
|
| 234 |
-
)
|
| 235 |
-
]
|
| 236 |
-
).send()
|
| 237 |
-
|
| 238 |
-
finally:
|
| 239 |
-
db.close()
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
@cl.action_callback("correct")
|
| 243 |
-
async def on_correct(action):
|
| 244 |
-
session = cl.user_session
|
| 245 |
-
db = SessionLocal()
|
| 246 |
-
try:
|
| 247 |
-
engine: SmartKnowledgeBase = session.get("engine")
|
| 248 |
-
chat_id = session.get("chat_id")
|
| 249 |
-
learning_enabled = bool(session.get("learning_enabled", True))
|
| 250 |
-
|
| 251 |
-
await action.remove()
|
| 252 |
-
|
| 253 |
-
res = await cl.AskUserMessage(
|
| 254 |
-
content="Paste the correct information:",
|
| 255 |
-
timeout=600
|
| 256 |
-
).send()
|
| 257 |
-
if not res:
|
| 258 |
-
return
|
| 259 |
-
|
| 260 |
-
# Always store correction text in DB (audit trail)
|
| 261 |
-
db.add(Message(chat_id=chat_id, role="user_feedback", content=res["output"]))
|
| 262 |
-
db.commit()
|
| 263 |
-
|
| 264 |
-
# Learn only if allowed
|
| 265 |
-
if learning_enabled:
|
| 266 |
-
engine.learn_from_interaction(action.payload["original"], res["output"])
|
| 267 |
-
await cl.Message(content="✅ Correction saved and learned.").send()
|
| 268 |
-
else:
|
| 269 |
-
await cl.Message(content="✅ Correction saved (learning disabled for this chat).").send()
|
| 270 |
-
|
| 271 |
-
finally:
|
| 272 |
-
db.close()
|
| 273 |
|
| 274 |
@cl.on_chat_start
|
| 275 |
async def start():
|
|
|
|
| 7 |
|
| 8 |
claude = ClaudeAnswerer()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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