import gradio as gr from Client.mcp_client import MCPClientExcelRAG from test import run_agent # ← import agent entry point import subprocess import sys import time subprocess.Popen([sys.executable, "-m", "Server.mcp_server"]) time.sleep(5) # Initialize MCP client client = MCPClientExcelRAG() client.start() # --- Supported scope for intent/planner --- SUPPORTED_IITS = ["IIT Bombay", "IIT Delhi", "IIT Madras"] SUPPORTED_BRANCHES = ["CSE", "ECE", "Mechanical", "Electrical", "Engineering Design"] YEARS = ["2024", "2025"] # --- Core ask: writes into selected session and returns messages twice --- ''' def ask(history, question, top_k, temperature, sessions, selected_name): history = history or [] sessions = sessions or [] selected_name = selected_name or (sessions[-1]["name"] if sessions else None) if not question or not question.strip(): history.append({"role": "assistant", "content": "⚠️ Please enter a question."}) # also update the current session if exists if selected_name: for s in sessions: if s["name"] == selected_name: s["messages"] = history break return history, history, sessions # Append user message history.append({"role": "user", "content": question}) # Agent call: intent → plan → call MCP tools → synthesize try: final_answer = run_agent( user_q=question, mcp_client=client, available_iits=SUPPORTED_IITS, available_branches=SUPPORTED_BRANCHES, years=YEARS, top_k=int(top_k), temperature=float(temperature), ) except Exception as e: final_answer = f"❌ Error while processing your request: {str(e)}" # Append assistant message history.append({"role": "assistant", "content": final_answer}) # Persist into the selected session if selected_name: for s in sessions: if s["name"] == selected_name: s["messages"] = history break return history, history, sessions ''' def ask(history, question, top_k, temperature, sessions, selected_name): history = history or [] sessions = sessions or [] selected_name = selected_name or (sessions[-1]["name"] if sessions else None) if not question or not question.strip(): history.append({"role": "assistant", "content": "⚠️ Please enter a question."}) if selected_name: for s in sessions: if s["name"] == selected_name: s["messages"] = history break return history, history, sessions # Append user message to visible chat history.append({"role": "user", "content": question}) # --- NEW: gather full session conversation for context --- if selected_name: for s in sessions: if s["name"] == selected_name: session_messages = s.get("messages", []) # include current question conversation = session_messages + [{"role": "user", "content": question}] break else: conversation = history.copy() # Call the agent (NOW conversation-aware) try: final_answer = run_agent( user_q=question, mcp_client=client, available_iits=SUPPORTED_IITS, available_branches=SUPPORTED_BRANCHES, years=YEARS, conversation=conversation, # <-- PASS HISTORY top_k=int(top_k), temperature=float(temperature), ) except Exception as e: final_answer = f"❌ Error while processing your request: {str(e)}" # Append assistant reply and persist to session history.append({"role": "assistant", "content": final_answer}) if selected_name: for s in sessions: if s["name"] == selected_name: s["messages"] = history break return history, history, sessions def clear_chat(): return [], [] # --- New chat: create session, clear history, select it in dropdown --- def new_chat_fn(history, sessions, selected_name): sessions = sessions or [] new_idx = len(sessions) + 1 new_name = f"Chat {new_idx}" sessions.append({"name": new_name, "messages": []}) # Clear current chat view and select the new one dropdown_update = gr.update(choices=[s["name"] for s in sessions], value=new_name) return [], [], sessions, dropdown_update # --- Load selected session into Chatbot --- def load_session_fn(sessions, selected_name): sessions = sessions or [] selected_name = selected_name or None messages = [] if selected_name: for s in sessions: if s["name"] == selected_name: messages = s.get("messages", []) break # Return messages -> Chatbot and State (keep them in sync) return messages, messages with gr.Blocks() as demo: # (your CSS and layout remain unchanged) gr.HTML(""" """) # --- States --- history_messages = gr.State([]) # current chat messages sessions_state = gr.State([]) # [{"name": "Chat 1", "messages": [...]}, ...] selected_session = gr.State(None) # current dropdown selection with gr.Row(): # LEFT with gr.Column(scale=1, min_width=260, elem_classes="left-panel"): gr.Markdown("") new_chat_btn = gr.Button("➕ New Chat", elem_id="new-chat-btn") gr.Markdown("
Chat History
") session_select = gr.Dropdown(choices=[], label=None, interactive=True, elem_id="session-select") with gr.Group(elem_id="top-k-wrapper"): top_k = gr.Slider(1, 10, value=5, step=1, label="Top-K") with gr.Group(elem_id="temperature-wrapper"): temperature = gr.Slider(0.0, 1.0, value=0.1, step=0.1, label="Temperature") # RIGHT with gr.Column(scale=3): chatbot = gr.Chatbot(height=520, elem_classes=["chatbot"]) question = gr.Textbox(placeholder="Ask...", lines=2, show_label=False, elem_id="question-box") with gr.Row(): ask_btn = gr.Button("Ask") clear_btn = gr.Button("Clear") gr.Markdown( """
⚠️ This is an agent just providing some recommendations. For final decisions, please visit the official website:
https://josaa.admissions.nic.in/applicant/seatmatrix/openingclosingrankarchieve.aspx JoSAA Opening & Closing Ranks
""", elem_classes="note-text" ) # Events ask_btn.click( ask, inputs=[history_messages, question, top_k, temperature, sessions_state, session_select], outputs=[chatbot, history_messages, sessions_state] ) question.submit( ask, inputs=[history_messages, question, top_k, temperature, sessions_state, session_select], outputs=[chatbot, history_messages, sessions_state] ) clear_btn.click(clear_chat, None, [chatbot, history_messages]) new_chat_btn.click( new_chat_fn, inputs=[history_messages, sessions_state, session_select], outputs=[chatbot, history_messages, sessions_state, session_select] ) session_select.change( load_session_fn, inputs=[sessions_state, session_select], outputs=[chatbot, history_messages] ) demo.launch()