mause123 commited on
Commit
8ef6562
ยท
1 Parent(s): bc4e88d

Add OAuth-free simple app version for HF Spaces compatibility

Browse files
Files changed (2) hide show
  1. README.md +2 -3
  2. simple_app.py +124 -0
README.md CHANGED
@@ -4,12 +4,11 @@ emoji: ๐Ÿค–
4
  colorFrom: blue
5
  colorTo: green
6
  sdk: gradio
7
- sdk_version: 5.38.1
8
- app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
  short_description: RobotPai - LangGraph AI Agent with tools
12
- hf_oauth: true
13
  ---
14
 
15
  # RobotPai - AI Agent Evaluation System
 
4
  colorFrom: blue
5
  colorTo: green
6
  sdk: gradio
7
+ sdk_version: 4.44.0
8
+ app_file: simple_app.py
9
  pinned: false
10
  license: apache-2.0
11
  short_description: RobotPai - LangGraph AI Agent with tools
 
12
  ---
13
 
14
  # RobotPai - AI Agent Evaluation System
simple_app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Simple RobotPai App without OAuth for Hugging Face Spaces"""
2
+ import os
3
+ import gradio as gr
4
+ from langchain_core.messages import HumanMessage
5
+ from agent import build_graph
6
+
7
+ class SimpleAgent:
8
+ """A simple langgraph agent for Hugging Face Spaces."""
9
+ def __init__(self, provider="google"):
10
+ print(f"SimpleAgent initialized with {provider} provider.")
11
+ try:
12
+ self.graph = build_graph(provider=provider)
13
+ print("Graph built successfully!")
14
+ except Exception as e:
15
+ print(f"Error building graph: {e}")
16
+ self.graph = None
17
+
18
+ def __call__(self, question: str) -> str:
19
+ if not self.graph:
20
+ return "โŒ Error: Agent not properly initialized. Please check API keys in Space settings."
21
+
22
+ try:
23
+ print(f"Agent received question: {question[:100]}...")
24
+ # Wrap the question in a HumanMessage from langchain_core
25
+ messages = [HumanMessage(content=question)]
26
+ result = self.graph.invoke({"messages": messages})
27
+ answer = result['messages'][-1].content
28
+ return answer
29
+ except Exception as e:
30
+ return f"โŒ Error processing question: {str(e)}\n\nPlease check that your API keys are properly configured in the Space settings."
31
+
32
+ # Check which provider to use based on available API keys
33
+ def get_available_provider():
34
+ """Check which LLM provider is available based on environment variables"""
35
+ if os.getenv("GOOGLE_API_KEY"):
36
+ return "google"
37
+ elif os.getenv("GROQ_API_KEY"):
38
+ return "groq"
39
+ else:
40
+ return "google" # Default, will show error if no key
41
+
42
+ # Initialize agent with available provider
43
+ provider = get_available_provider()
44
+ print(f"๐Ÿค– Initializing RobotPai with {provider} provider...")
45
+ agent = SimpleAgent(provider=provider)
46
+
47
+ def chat_with_agent(message, history):
48
+ """Chat function for Gradio interface"""
49
+ if not message.strip():
50
+ return "Please enter a question or message."
51
+
52
+ response = agent(message)
53
+ return response
54
+
55
+ # Create Gradio interface
56
+ with gr.Blocks(title="RobotPai - AI Agent", theme=gr.themes.Soft()) as demo:
57
+ gr.Markdown("""
58
+ # ๐Ÿค– RobotPai - AI Agent
59
+
60
+ Welcome to RobotPai! I'm an AI agent powered by LangGraph that can help you with:
61
+
62
+ - ๐Ÿงฎ **Math calculations** (add, subtract, multiply, divide, modulus)
63
+ - ๐Ÿค” **General questions** using advanced AI
64
+ - ๐Ÿ” **Web search** (when Tavily API is configured)
65
+ - ๐Ÿ“š **Wikipedia search** (when available)
66
+ - ๐Ÿ“„ **Arxiv research** (when available)
67
+
68
+ Just type your question below and I'll do my best to help!
69
+ """)
70
+
71
+ # Status indicator
72
+ if agent.graph:
73
+ gr.Markdown("โœ… **Status**: Agent is ready and operational!")
74
+ else:
75
+ gr.Markdown("โŒ **Status**: Agent initialization failed. Please check API key configuration.")
76
+
77
+ # Chat interface
78
+ chatbot = gr.ChatInterface(
79
+ fn=chat_with_agent,
80
+ title="Chat with RobotPai",
81
+ description="Ask me anything! I can do math, answer questions, and more.",
82
+ examples=[
83
+ "What is 15 + 27?",
84
+ "Calculate 144 divided by 12",
85
+ "What is 25 * 8?",
86
+ "What is the capital of France?",
87
+ "Explain quantum computing in simple terms",
88
+ "What is the square root of 144?"
89
+ ],
90
+ retry_btn="๐Ÿ”„ Retry",
91
+ undo_btn="โ†ฉ๏ธ Undo",
92
+ clear_btn="๐Ÿ—‘๏ธ Clear",
93
+ )
94
+
95
+ gr.Markdown("""
96
+ ---
97
+ ### ๐Ÿ”ง Configuration Notes:
98
+ - **LLM Provider**: Using {provider} (based on available API keys)
99
+ - **Tools Available**: Math operations, AI reasoning
100
+ - **Search Tools**: Available when API keys are configured
101
+
102
+ ### ๐Ÿ”‘ API Keys Required:
103
+ Add these to Space settings for full functionality:
104
+ - `GOOGLE_API_KEY` or `GROQ_API_KEY` (for AI responses)
105
+ - `TAVILY_API_KEY` (for web search)
106
+
107
+ Built with [LangGraph](https://langchain-ai.github.io/langgraph/) and [Gradio](https://gradio.app)
108
+ """.format(provider=provider.title()))
109
+
110
+ if __name__ == "__main__":
111
+ print("\n" + "="*60)
112
+ print("๐Ÿš€ Starting RobotPai on Hugging Face Spaces")
113
+ print("="*60)
114
+
115
+ # Check configuration
116
+ if agent.graph:
117
+ print("โœ… Agent initialized successfully!")
118
+ print(f"๐Ÿ”ง Using {provider} provider")
119
+ else:
120
+ print("โŒ Agent initialization failed!")
121
+ print("๐Ÿ’ก Please check API keys in Space settings")
122
+
123
+ print("๐ŸŒ Launching web interface...")
124
+ demo.launch()