Spaces:
Runtime error
Runtime error
| # -*- coding: utf-8 -*- | |
| """Context-Aware Smart.ipynb | |
| Automatically generated by Colab. | |
| Original file is located at | |
| https://colab.research.google.com/drive/1zrqp56QXy-rRJtCXU_zSc6EPd1rgBg9l | |
| """ | |
| #!pip install langchain langchain-community gradio python-dotenv | |
| #!pip install groq | |
| from langchain.llms.base import LLM | |
| import gradio as gr | |
| from langchain.agents import initialize_agent, AgentType | |
| from llm import llm | |
| from web_search_tool import WebSearchTool | |
| from Context_Relevance_Splitter import Context_Relevance_Splitter_tool | |
| from context_presence_judge import context_tool | |
| # --- Agent initialization with all tools --- | |
| tools = [context_tool, | |
| Context_Relevance_Splitter_tool, | |
| WebSearchTool | |
| ] | |
| agent = initialize_agent( | |
| tools=tools, | |
| llm=llm, | |
| agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, | |
| handle_parsing_errors=True, | |
| verbose=True, | |
| max_iterations=5 | |
| ) | |
| # --- Enhanced Agent Runner Function --- | |
| def run_agent(user_input): | |
| try: | |
| # Initial context processing | |
| context_result = Context_Relevance_Splitter_tool(user_input) | |
| # If result is a string (error message) | |
| if isinstance(context_result, str): | |
| if "🚫" in context_result or "⚠" in context_result: | |
| return context_result | |
| else: | |
| final_question = user_input | |
| background = "" | |
| else: | |
| # If result is a dictionary (successful processing) | |
| final_question = context_result.get('core_question', user_input) | |
| background = context_result.get('background', '') | |
| # Build final agent input | |
| if background: | |
| enhanced_input = f""" | |
| Context Background: {background} | |
| Question: {final_question} | |
| """ | |
| else: | |
| enhanced_input = final_question | |
| # Run agent with final input | |
| return agent.run(enhanced_input) | |
| except Exception as e: | |
| return f"⛔ Unexpected error occurred: {str(e)}\nPlease rephrase your question or try again later." | |
| # --- Enhanced Gradio Interface --- | |
| interface = gr.Interface( | |
| fn=run_agent, | |
| inputs=gr.Textbox( | |
| lines=3, | |
| placeholder="Enter your question here...\nTo add context use format: Question||Context\nExample: What's France's capital||Speaking about a European country" | |
| ), | |
| outputs="text", | |
| title="🤖 Context-Aware Smart Agent", | |
| description=""" | |
| Advanced system for understanding complex questions: | |
| - Supports external context using || | |
| - Automatically get the answer from context | |
| - Answers directly or searches when needed | |
| """, | |
| examples=[ | |
| ["العاصمة السعودية هي الرياض||ما هي عاصمة مصر؟"], | |
| ["العاصمة السعودية هي الرياض||ما هي عاصمة السعودية؟"] | |
| ] | |
| ) | |
| # --- Launch Interface --- | |
| if __name__ == "__main__": | |
| interface.launch() | |