File size: 1,068 Bytes
37e3edf
82d11d3
 
37e3edf
82d11d3
 
 
 
 
 
 
 
 
 
 
37e3edf
82d11d3
37e3edf
82d11d3
 
 
37e3edf
82d11d3
37e3edf
 
82d11d3
37e3edf
82d11d3
 
 
37e3edf
 
 
 
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
import gradio as gr
from Rag_conversation import rag_chain  # Import the RAG pipeline from your script
from langchain_core.messages import HumanMessage, SystemMessage

# Function to process user messages
def chatbot(user_message, history):
    """
    :param user_message: The latest user message
    :param history: A list of (user, ai) tuples from Gradio chat
    :return: (bot_reply, updated_history)
    """
    chat_history = []
    for h in history:
        chat_history.append(HumanMessage(content=h[0]))  # User message
        chat_history.append(SystemMessage(content=h[1]))  # AI response

    chat_history.append(HumanMessage(content=user_message))  # Add new message

    # Get response from RAG-based chatbot
    result = rag_chain.invoke({"input": user_message, "chat_history": chat_history})
    bot_reply = result["answer"]

    return bot_reply


# Create Gradio UI
demo = gr.ChatInterface(
    chatbot,
    title="Binghamton RAG Chatbot",
    description="Ask questions about Binghamton University.",
)

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