File size: 1,218 Bytes
593f82b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
from workflow import build_agent
from langchain.memory import ConversationBufferMemory




session={}

def manage_memory(session_id:str):
    if session_id not in session:
        session[session_id]=ConversationBufferMemory(memory_key="chat_history", return_messages=True)
    return session[session_id]



state={
    'query':'',
    'cat':'',
    'bank':'',
    'product':'',
    'search_result':'',
    'final_answer':'',
    'chat_history':[]
}

agent=build_agent(state)

def chat_history(message,history,request:gr.Request):
    state['query']=message
    state["chat_history"] = [f"User: {u}\nAssistant: {a}" for u, a in history if u and a]
    session_id=request.session_hash
    memory=manage_memory(session_id)
    memory.chat_memory.add_user_message(message)
    ans = agent.invoke(state)
    memory.chat_memory.add_ai_message(ans["final_answer"])
    return ans["final_answer"]

with gr.Blocks() as demo:
    gr.ChatInterface(
        chat_history,
        title="💬 Banking Research Agent",
        description="Ask me about banking products, and I'll fetch & compare results for you."
    )

if __name__=="__main__":
    demo.launch()