|
|
"""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]}...") |
|
|
|
|
|
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." |
|
|
|
|
|
|
|
|
def get_available_provider(): |
|
|
"""Force Groq provider to avoid Google API rate limits""" |
|
|
return "groq" |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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! |
|
|
""") |
|
|
|
|
|
|
|
|
if agent.graph: |
|
|
gr.Markdown("โ
**Status**: Agent is ready and operational!") |
|
|
else: |
|
|
gr.Markdown("โ **Status**: Agent initialization failed. Please check API key configuration.") |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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() |
|
|
|