Spaces:
Sleeping
Sleeping
| """ | |
| EU Agricultural Legislation Agent Tab | |
| ====================================== | |
| UI component for consulting with an AI agent specialized in EU agricultural legislation. | |
| This module provides a conversational interface powered by ElevenLabs AI agent | |
| to answer questions about European Union agricultural regulations, policies, | |
| subsidies, and compliance requirements. | |
| MCP Endpoints Exposed: | |
| - /add_user_question_to_legislation_chat: Add user message to chat history | |
| - /get_ai_legislation_expert_response: Get AI agent response | |
| - /clear_legislation_chat_history: Clear conversation history | |
| """ | |
| import gradio as gr | |
| from src.utils.elevenlabs_agent import chat_with_agent | |
| def create_legislation_tab(): | |
| """ | |
| Create the EU legislation consultation tab with interactive chat interface. | |
| This function builds a complete Gradio tab with: | |
| - Chatbot interface for conversational AI | |
| - Input textbox for user questions | |
| - Submit and clear buttons | |
| - Auto-exposed MCP API endpoints | |
| Returns: | |
| gr.Tab: Configured Gradio tab component | |
| MCP APIs: | |
| When mcp_server=True in app launch, this tab exposes: | |
| - /add_user_question_to_legislation_chat(message, history) -> (str, list) | |
| - /get_ai_legislation_expert_response(history) -> list | |
| - /clear_legislation_chat_history() -> list | |
| """ | |
| with gr.Tab("โ๏ธ EU Agricultural Legislation"): | |
| gr.Markdown( | |
| """ | |
| ### ๐ช๐บ Expert Agent in European Agricultural Legislation | |
| Ask questions about regulations, policies, subsidies, and compliance requirements | |
| for agriculture in the European Union. | |
| **Examples:** | |
| - What are the main requirements for organic certification in the EU? | |
| - How does the Common Agricultural Policy (CAP) affect small farmers? | |
| - What are the regulations for pesticide use in European agriculture? | |
| """ | |
| ) | |
| # Chat interface | |
| chatbot = gr.Chatbot( | |
| label="๐ฌ Conversation with EU Legislation Expert", | |
| height=500, | |
| show_label=True, | |
| avatar_images=(None, "๐ค") | |
| # Gradio 6 uses message format by default: {"role": "user/assistant", "content": "..."} | |
| ) | |
| with gr.Row(): | |
| msg = gr.Textbox( | |
| label="Your question", | |
| placeholder="Ask about EU agricultural legislation...", | |
| scale=4, | |
| show_label=False | |
| ) | |
| submit_btn = gr.Button("Send", variant="primary", scale=1) | |
| with gr.Row(): | |
| clear_btn = gr.Button("๐๏ธ Clear conversation", size="sm") | |
| # Function to handle user message | |
| def add_user_question_to_legislation_chat(message, history): | |
| """ | |
| Add user message to chat history. | |
| MCP API: /add_user_question_to_legislation_chat | |
| Description: Adds a user's question to the conversation history and prepares | |
| it for the AI agent response. Validates input and returns updated | |
| history with user message and placeholder for bot response. | |
| Args: | |
| message (str): User's question about EU agricultural legislation. | |
| Example: "What are the CAP requirements for organic farming?" | |
| history (list): Previous conversation history in messages format: | |
| [{"role": "user", "content": msg1}, | |
| {"role": "assistant", "content": resp1}, ...] | |
| Returns: | |
| tuple: (empty_string, updated_history) | |
| - empty_string (str): Clears the input textbox | |
| - updated_history (list): History with new user message appended | |
| Format: [..., {"role": "user", "content": message}] | |
| Example: | |
| >>> add_user_question_to_legislation_chat("What is CAP?", []) | |
| ("", [{"role": "user", "content": "What is CAP?"}]) | |
| """ | |
| if not message.strip(): | |
| return "", history | |
| return "", history + [{"role": "user", "content": message}] | |
| # Function to get bot response | |
| def get_ai_legislation_expert_response(history): | |
| """ | |
| Get response from ElevenLabs AI agent. | |
| MCP API: /get_ai_legislation_expert_response | |
| Description: Processes the last user message in history through the | |
| ElevenLabs conversational AI agent specialized in EU | |
| agricultural legislation and returns the complete response. | |
| Args: | |
| history (list): Conversation history in messages format where last entry | |
| should be a user message. | |
| Format: [{"role": "user", "content": msg1}, | |
| {"role": "assistant", "content": resp1}, ...] | |
| Returns: | |
| list: Updated conversation history with assistant response appended. | |
| Format: [..., {"role": "assistant", "content": response}] | |
| Returns unchanged history if empty. | |
| Behavior: | |
| - Returns unchanged history if empty | |
| - Calls chat_with_agent() to get AI response | |
| - Appends assistant message to history | |
| Example: | |
| >>> history = [{"role": "user", "content": "What is CAP?"}] | |
| >>> get_ai_legislation_expert_response(history) | |
| [{"role": "user", "content": "What is CAP?"}, | |
| {"role": "assistant", "content": "The Common Agricultural Policy..."}] | |
| """ | |
| if not history: | |
| return history | |
| # Get the last user message | |
| user_msg = None | |
| for msg in reversed(history): | |
| if msg.get("role") == "user": | |
| user_msg = msg.get("content") | |
| break | |
| if not user_msg: | |
| return history | |
| response = chat_with_agent(user_msg) | |
| # Append assistant response | |
| return history + [{"role": "assistant", "content": response}] | |
| # Function to clear chat | |
| def clear_legislation_chat_history(): | |
| """ | |
| Clear the chat history. | |
| MCP API: /clear_legislation_chat_history | |
| Description: Resets the entire conversation history, allowing users to | |
| start a fresh conversation with the AI agent. | |
| Args: | |
| None | |
| Returns: | |
| list: Empty list representing cleared conversation history. | |
| Example: | |
| >>> clear_legislation_chat_history() | |
| [] | |
| """ | |
| return [] | |
| # Connect events | |
| msg.submit( | |
| fn=add_user_question_to_legislation_chat, | |
| inputs=[msg, chatbot], | |
| outputs=[msg, chatbot], | |
| queue=False | |
| ).then( | |
| fn=get_ai_legislation_expert_response, | |
| inputs=[chatbot], | |
| outputs=[chatbot] | |
| ) | |
| submit_btn.click( | |
| fn=add_user_question_to_legislation_chat, | |
| inputs=[msg, chatbot], | |
| outputs=[msg, chatbot], | |
| queue=False | |
| ).then( | |
| fn=get_ai_legislation_expert_response, | |
| inputs=[chatbot], | |
| outputs=[chatbot] | |
| ) | |
| clear_btn.click( | |
| fn=clear_legislation_chat_history, | |
| outputs=[chatbot] | |
| ) | |