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("