Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,117 +1,3 @@
|
|
| 1 |
-
# import os
|
| 2 |
-
# import re
|
| 3 |
-
# import sqlite3
|
| 4 |
-
# from typing import TypedDict, Annotated
|
| 5 |
-
|
| 6 |
-
# from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 7 |
-
# from dotenv import load_dotenv
|
| 8 |
-
|
| 9 |
-
# from langchain_google_genai import ChatGoogleGenerativeAI
|
| 10 |
-
# from langchain_core.messages import (
|
| 11 |
-
# HumanMessage,
|
| 12 |
-
# BaseMessage,
|
| 13 |
-
# SystemMessage,
|
| 14 |
-
# )
|
| 15 |
-
# from langgraph.graph.message import add_messages
|
| 16 |
-
# from langgraph.graph import StateGraph, START, END
|
| 17 |
-
# from langgraph.checkpoint.sqlite import SqliteSaver
|
| 18 |
-
|
| 19 |
-
# # RAG imports
|
| 20 |
-
# from rag import load_student_documents, create_vectorstore, get_retriever
|
| 21 |
-
|
| 22 |
-
# load_dotenv(override=True)
|
| 23 |
-
|
| 24 |
-
# # ------------------ RAG SETUP ------------------
|
| 25 |
-
# documents = load_student_documents("studentDataset.csv")
|
| 26 |
-
# vectorstore = create_vectorstore(documents)
|
| 27 |
-
# retriever = get_retriever(vectorstore)
|
| 28 |
-
|
| 29 |
-
# # ------------------ LLM ------------------
|
| 30 |
-
# llm = ChatGoogleGenerativeAI(
|
| 31 |
-
# model="models/gemini-2.5-flash",
|
| 32 |
-
# temperature=0.3,
|
| 33 |
-
# )
|
| 34 |
-
|
| 35 |
-
# # ------------------ STATE ------------------
|
| 36 |
-
# class ChatState(TypedDict):
|
| 37 |
-
# messages: Annotated[list[BaseMessage], add_messages]
|
| 38 |
-
|
| 39 |
-
# # ------------------ GRAPH NODE ------------------
|
| 40 |
-
# def chat_node(state: ChatState):
|
| 41 |
-
# user_msg = state["messages"][-1].content
|
| 42 |
-
|
| 43 |
-
# # 1️⃣ Exact register number search
|
| 44 |
-
# match = re.search(r"\b\d{4,}\b", user_msg)
|
| 45 |
-
# docs = []
|
| 46 |
-
# if match:
|
| 47 |
-
# reg_no = match.group()
|
| 48 |
-
# docs = [d for d in documents if reg_no in d.page_content]
|
| 49 |
-
|
| 50 |
-
# # 2️⃣ Vector fallback
|
| 51 |
-
# if not docs:
|
| 52 |
-
# docs = retriever.invoke(user_msg)
|
| 53 |
-
|
| 54 |
-
# context = "\n\n".join(d.page_content for d in docs)
|
| 55 |
-
|
| 56 |
-
# system_prompt = f"""
|
| 57 |
-
# You are a student database assistant.
|
| 58 |
-
# Answer ONLY using the information below.
|
| 59 |
-
# If the answer is not present, say "I don't have that information".
|
| 60 |
-
|
| 61 |
-
# DATA:
|
| 62 |
-
# {context}
|
| 63 |
-
# """
|
| 64 |
-
|
| 65 |
-
# response = llm.invoke([
|
| 66 |
-
# SystemMessage(content=system_prompt),
|
| 67 |
-
# HumanMessage(content=user_msg),
|
| 68 |
-
# ])
|
| 69 |
-
|
| 70 |
-
# return {"messages": [response]}
|
| 71 |
-
|
| 72 |
-
# # ------------------ GRAPH ------------------
|
| 73 |
-
# graph = StateGraph(ChatState)
|
| 74 |
-
# graph.add_node("chat_node", chat_node)
|
| 75 |
-
# graph.add_edge(START, "chat_node")
|
| 76 |
-
# graph.add_edge("chat_node", END)
|
| 77 |
-
|
| 78 |
-
# conn = sqlite3.connect("a.db", check_same_thread=False)
|
| 79 |
-
# checkpointer = SqliteSaver(conn=conn)
|
| 80 |
-
|
| 81 |
-
# workflow = graph.compile(checkpointer=checkpointer)
|
| 82 |
-
|
| 83 |
-
# # ------------------ FASTAPI ------------------
|
| 84 |
-
# app = FastAPI()
|
| 85 |
-
|
| 86 |
-
# @app.websocket("/chat")
|
| 87 |
-
# async def chat_ws(websocket: WebSocket):
|
| 88 |
-
# await websocket.accept()
|
| 89 |
-
|
| 90 |
-
# try:
|
| 91 |
-
# session_id = websocket.query_params.get("session_id", "default")
|
| 92 |
-
|
| 93 |
-
# while True:
|
| 94 |
-
# user_text = await websocket.receive_text()
|
| 95 |
-
|
| 96 |
-
# config = {"configurable": {"thread_id": session_id}}
|
| 97 |
-
# response_text = ""
|
| 98 |
-
|
| 99 |
-
# for msg, _ in workflow.stream(
|
| 100 |
-
# {"messages": [HumanMessage(user_text)]},
|
| 101 |
-
# config=config,
|
| 102 |
-
# stream_mode="messages",
|
| 103 |
-
# ):
|
| 104 |
-
# if msg.content:
|
| 105 |
-
# await websocket.send_text(msg.content)
|
| 106 |
-
|
| 107 |
-
# # signal end of response
|
| 108 |
-
# await websocket.send_text("__END__")
|
| 109 |
-
|
| 110 |
-
# except WebSocketDisconnect:
|
| 111 |
-
# print("Middleware disconnected")
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
import json
|
| 116 |
import os
|
| 117 |
import re
|
|
@@ -155,14 +41,14 @@ class ChatState(TypedDict):
|
|
| 155 |
def chat_node(state: ChatState):
|
| 156 |
user_msg = state["messages"][-1].content
|
| 157 |
|
| 158 |
-
#
|
| 159 |
match = re.search(r"\b\d{4,}\b", user_msg)
|
| 160 |
docs = []
|
| 161 |
if match:
|
| 162 |
reg_no = match.group()
|
| 163 |
docs = [d for d in documents if reg_no in d.page_content]
|
| 164 |
|
| 165 |
-
#
|
| 166 |
if not docs:
|
| 167 |
docs = retriever.invoke(user_msg)
|
| 168 |
|
|
@@ -209,7 +95,7 @@ async def chat_ws(websocket: WebSocket):
|
|
| 209 |
|
| 210 |
config = {"configurable": {"thread_id": session_id}}
|
| 211 |
|
| 212 |
-
#
|
| 213 |
for msg, _ in workflow.stream(
|
| 214 |
{"messages": [HumanMessage(user_text)]},
|
| 215 |
config=config,
|
|
@@ -221,7 +107,7 @@ async def chat_ws(websocket: WebSocket):
|
|
| 221 |
"content": msg.content
|
| 222 |
})
|
| 223 |
|
| 224 |
-
#
|
| 225 |
await websocket.send_json({
|
| 226 |
"type": "complete"
|
| 227 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
import os
|
| 3 |
import re
|
|
|
|
| 41 |
def chat_node(state: ChatState):
|
| 42 |
user_msg = state["messages"][-1].content
|
| 43 |
|
| 44 |
+
# Exact register number search
|
| 45 |
match = re.search(r"\b\d{4,}\b", user_msg)
|
| 46 |
docs = []
|
| 47 |
if match:
|
| 48 |
reg_no = match.group()
|
| 49 |
docs = [d for d in documents if reg_no in d.page_content]
|
| 50 |
|
| 51 |
+
# Vector fallback
|
| 52 |
if not docs:
|
| 53 |
docs = retriever.invoke(user_msg)
|
| 54 |
|
|
|
|
| 95 |
|
| 96 |
config = {"configurable": {"thread_id": session_id}}
|
| 97 |
|
| 98 |
+
# STREAM RESPONSE
|
| 99 |
for msg, _ in workflow.stream(
|
| 100 |
{"messages": [HumanMessage(user_text)]},
|
| 101 |
config=config,
|
|
|
|
| 107 |
"content": msg.content
|
| 108 |
})
|
| 109 |
|
| 110 |
+
# SIGNAL COMPLETION
|
| 111 |
await websocket.send_json({
|
| 112 |
"type": "complete"
|
| 113 |
})
|