incligen / simple_app.py
mause123
Change title from RobotPai to incligen
2d3e706
"""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()