"""Simple RobotPai App without OAuth for Hugging Face Spaces""" import os import gradio as gr from langchain_core.messages import HumanMessage from agent import build_graph class SimpleAgent: """A simple langgraph agent for Hugging Face Spaces.""" def __init__(self, provider="google"): print(f"SimpleAgent initialized with {provider} provider.") try: self.graph = build_graph(provider=provider) print("Graph built successfully!") except Exception as e: print(f"Error building graph: {e}") self.graph = None def __call__(self, question: str) -> str: if not self.graph: return "❌ Error: Agent not properly initialized. Please check API keys in Space settings." try: print(f"Agent received question: {question[:100]}...") # Wrap the question in a HumanMessage from langchain_core messages = [HumanMessage(content=question)] result = self.graph.invoke({"messages": messages}) answer = result['messages'][-1].content return answer except Exception as e: return f"❌ Error processing question: {str(e)}\n\nPlease check that your API keys are properly configured in the Space settings." # Force Groq provider to avoid Google rate limits def get_available_provider(): """Force Groq provider to avoid Google API rate limits""" return "groq" # Always use Groq to avoid Google rate limits # Initialize agent with Groq provider provider = get_available_provider() print(f"🤖 Initializing RobotPai with {provider} provider...") print("📝 Note: Using Groq to avoid Google API rate limits") agent = SimpleAgent(provider=provider) def chat_with_agent(message, history): """Chat function for Gradio interface""" if not message.strip(): return "Please enter a question or message." response = agent(message) return response # Create Gradio interface with gr.Blocks(title="incligen", theme=gr.themes.Soft()) as demo: gr.Markdown(""" # 🤖 incligen Welcome to incligen! I'm an AI agent powered by LangGraph that can help you with: - 🧮 **Math calculations** (add, subtract, multiply, divide, modulus, square root, power, absolute) - 🤔 **General questions** using advanced AI - 🔍 **Web search** (when Tavily API is configured) - 📚 **Wikipedia search** (when available) - 📄 **Arxiv research** (when available) Just type your question below and I'll do my best to help! """) # Status indicator if agent.graph: gr.Markdown("✅ **Status**: Agent is ready and operational!") else: gr.Markdown("❌ **Status**: Agent initialization failed. Please check API key configuration.") # Chat interface chatbot = gr.ChatInterface( fn=chat_with_agent, title="Chat with incligen", description="Ask me anything! I can do math, answer questions, and more.", examples=[ "What is 15 + 27?", "Calculate 144 divided by 12", "What is 25 * 8?", "What is the square root of 144?", "Calculate 2 to the power of 8", "What is the absolute value of -15?", "What is the capital of France?", "Explain quantum computing in simple terms" ], retry_btn="🔄 Retry", undo_btn="â†Šī¸ Undo", clear_btn="đŸ—‘ī¸ Clear", ) gr.Markdown(""" --- ### 🔧 Configuration Notes: - **LLM Provider**: Using Groq (optimized for performance and rate limits) - **Tools Available**: Math operations, AI reasoning - **Search Tools**: Available when API keys are configured ### 🔑 API Keys Required: Add these to Space settings for full functionality: - `GROQ_API_KEY` (for AI responses - recommended) - `TAVILY_API_KEY` (for web search) ### ⚡ Why Groq? - **Higher rate limits** than Google free tier - **Faster responses** with optimized inference - **More reliable** for frequent testing Built with [LangGraph](https://langchain-ai.github.io/langgraph/) and [Gradio](https://gradio.app) """) if __name__ == "__main__": print("\n" + "="*60) print("🚀 Starting incligen on Hugging Face Spaces") print("="*60) # Check configuration if agent.graph: print("✅ Agent initialized successfully!") print(f"🔧 Using {provider} provider") else: print("❌ Agent initialization failed!") print("💡 Please check API keys in Space settings") print("🌐 Launching web interface...") demo.launch()