Skip to content

Commit 8c79165

Browse files
authored
Merge pull request #47 from HyperbolicLabs/amr/fix-gradio
Amr/fix gradio
2 parents dceb179 + bdc99f1 commit 8c79165

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,24 @@ poetry run python gradio_ui.py
216216
```
217217

218218
4. **Browser Automation Issues**
219+
219220
```bash
220221
# Reinstall Playwright browsers
221222
poetry run playwright install --force
222223
```
223224

225+
5. **Chrome Browser Setup Issues**
226+
- Ensure Google Chrome is installed on your system
227+
- Configure a default Chrome profile:
228+
1. Open Chrome
229+
2. Make sure a profile is already selected/active
230+
3. Remove all pinned tabs from the active profile (they can cause browser automation issues)
231+
4. Ensure Chrome doesn't show a profile selector on startup
232+
- If using browser automation tools, the agent assumes:
233+
- Chrome is your default browser
234+
- A default profile exists and is automatically selected
235+
- No pinned tabs are present in the active profile
236+
224237
## Adding New Tools
225238

226239
The agent framework supports two main interfaces, each with its own tool registration point:

gradio_ui.py

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,64 @@
77
from utils import format_ai_message_content
88
from datetime import datetime
99

10-
async def chat_with_agent(message, history):
11-
# Initialize agent if not already done
12-
if not hasattr(chat_with_agent, "agent"):
13-
agent_executor, config, twitter_api_wrapper, knowledge_base = await initialize_agent()
14-
chat_with_agent.agent = agent_executor
15-
chat_with_agent.config = config
10+
# Global variables to store initialized agent and config
11+
agent = None
12+
agent_config = None
1613

14+
async def chat_with_agent(message, history):
15+
global agent, agent_config
16+
17+
# Convert history into messages format that the agent expects
18+
messages = []
19+
if history:
20+
print("History:", history) # Debug print
21+
for msg in history:
22+
if isinstance(msg, dict):
23+
if msg.get("role") == "user":
24+
messages.append(HumanMessage(content=msg["content"]))
25+
elif msg.get("role") == "assistant":
26+
messages.append({"role": "assistant", "content": msg["content"]})
27+
28+
# Add the current message
29+
messages.append(HumanMessage(content=message))
30+
31+
print("Final messages:", messages) # Debug print
32+
1733
runnable_config = RunnableConfig(
18-
recursion_limit=config["configurable"]["recursion_limit"],
34+
recursion_limit=agent_config["configurable"]["recursion_limit"],
1935
configurable={
20-
"thread_id": config["configurable"]["thread_id"],
36+
"thread_id": agent_config["configurable"]["thread_id"],
2137
"checkpoint_ns": "chat_mode",
2238
"checkpoint_id": str(datetime.now().timestamp())
2339
}
2440
)
2541

26-
messages = []
27-
yield messages
42+
response_messages = []
43+
yield response_messages
2844
# Process message with agent
29-
async for chunk in chat_with_agent.agent.astream(
30-
{"messages": [HumanMessage(content=message)]},
45+
async for chunk in agent.astream(
46+
{"messages": messages}, # Pass the full message history
3147
runnable_config
3248
):
3349
if "agent" in chunk:
3450
print("agent in chunk")
3551
response = chunk["agent"]["messages"][0].content
36-
messages.append(dict(
52+
response_messages.append(dict(
3753
role="assistant",
3854
content=format_ai_message_content(response, format_mode="markdown")
3955
))
40-
print(messages)
41-
yield messages
56+
print(response_messages)
57+
yield response_messages
4258
elif "tools" in chunk:
4359
print("tools in chunk")
4460
tool_message = str(chunk["tools"]["messages"][0].content)
45-
messages.append(dict(
61+
response_messages.append(dict(
4662
role="assistant",
4763
content=tool_message,
4864
metadata={"title": "🛠️ Tool Call"}
4965
))
50-
print(messages)
51-
yield messages
66+
print(response_messages)
67+
yield response_messages
5268

5369
def create_ui():
5470
# Create the Gradio interface
@@ -95,8 +111,20 @@ def create_ui():
95111

96112
return demo
97113

98-
if __name__ == "__main__":
114+
async def main():
115+
global agent, agent_config
116+
# Initialize agent before creating UI
117+
print("Initializing agent...")
118+
agent_executor, config, runnable_config = await initialize_agent()
119+
agent = agent_executor
120+
agent_config = config
121+
99122
# Create and launch the UI
123+
print("Starting Gradio UI...")
100124
demo = create_ui()
101125
demo.queue()
102-
demo.launch(share=True)
126+
demo.launch(share=True)
127+
128+
if __name__ == "__main__":
129+
# Run the async main function
130+
asyncio.run(main())

0 commit comments

Comments
 (0)