Lokesh1024 commited on
Commit
7303dec
·
verified ·
1 Parent(s): aa0aab1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -82
app.py CHANGED
@@ -1,82 +1,76 @@
1
- import os
2
- import streamlit as st
3
- from dotenv import load_dotenv
4
- from phi.agent import Agent
5
- from phi.model.groq import Groq
6
- from phi.tools.duckduckgo import DuckDuckGo
7
- from phi.tools.yfinance import YFinanceTools
8
-
9
- # Load environment variables
10
- load_dotenv()
11
-
12
- # Retrieve API keys from the environment
13
- deepseek_api_key = os.getenv("GROQ_DEEPSEEK_API_KEY")
14
- qwen_api_key = os.getenv("GROQ_QWEN_API_KEY")
15
-
16
- # Debugging API key loading
17
- if not deepseek_api_key or not qwen_api_key:
18
- raise ValueError("Missing API keys. Check your .env file.")
19
- print("DeepSeek API Key Loaded")
20
- print("Qwen API Key Loaded")
21
-
22
- # Define the Web Agent using Groq's QWEN model
23
- web_agent = Agent(
24
- name="Web Agent",
25
- model=Groq(id="qwen-2.5-coder-32b", api_key=qwen_api_key),
26
- tools=[DuckDuckGo()],
27
- instructions=["Always include sources"],
28
- show_tool_calls=True,
29
- markdown=True,
30
- )
31
-
32
- # Define the Finance Agent using Groq's DeepSeek model
33
- finance_agent = Agent(
34
- name="Finance Agent",
35
- role="Get financial data",
36
- model=Groq(id="qwen-2.5-coder-32b", api_key=qwen_api_key),
37
- tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
38
- instructions=["Use tables to display data"],
39
- show_tool_calls=True,
40
- markdown=True,
41
- )
42
-
43
- # Combine agents into a team
44
- agent_team = Agent(
45
- model=Groq(id="deepseek-r1-distill-llama-70b", api_key=deepseek_api_key),
46
- team=[web_agent, finance_agent],
47
- instructions=["Always include sources", "Use tables to display data"],
48
- show_tool_calls=True,
49
- markdown=True,
50
- )
51
-
52
- # Debugging agent_team initialization
53
- if not agent_team:
54
- raise ValueError("Agent team failed to initialize.")
55
- print("Agent team initialized successfully")
56
-
57
- # Streamlit UI
58
- st.set_page_config(page_title="AI Chat Assistant", layout="wide")
59
- st.title("🤖 AI Chat Assistant")
60
-
61
- # User input
62
- if prompt := st.chat_input("Ask me anything..."):
63
- with st.chat_message("user"):
64
- st.markdown(prompt)
65
-
66
- with st.chat_message("assistant"):
67
- response_container = st.empty()
68
- response_text = ""
69
-
70
- # Ensure agent_team has a valid method to generate responses
71
- if hasattr(agent_team, "respond") and callable(agent_team.respond):
72
- try:
73
- response_text = web_agent.respond(prompt) # Try this instead
74
- response_container.markdown(response_text if response_text else "No response received.")
75
- except Exception as e:
76
- error_message = f"Error during response: {str(e)}"
77
- print(error_message)
78
- response_container.markdown(error_message)
79
- else:
80
- error_message = "Error: Agent does not support valid response methods."
81
- print(error_message)
82
- response_container.markdown(error_message)
 
1
+ import os
2
+ import streamlit as st
3
+ from dotenv import load_dotenv
4
+ from phi.agent import Agent
5
+ from phi.model.groq import Groq
6
+ from phi.tools.duckduckgo import DuckDuckGo
7
+ from phi.tools.yfinance import YFinanceTools
8
+
9
+ # Load environment variables
10
+ load_dotenv()
11
+
12
+ # Retrieve API keys from the environment
13
+ deepseek_api_key = os.getenv("GROQ_DEEPSEEK_API_KEY")
14
+ qwen_api_key = os.getenv("GROQ_QWEN_API_KEY")
15
+
16
+ # Debugging API key loading
17
+ if not deepseek_api_key or not qwen_api_key:
18
+ st.error("Missing API keys. Please set them in Hugging Face Secrets.")
19
+ st.stop()
20
+
21
+ # Define Web Agent
22
+ web_agent = Agent(
23
+ name="Web Agent",
24
+ model=Groq(id="qwen-2.5-coder-32b", api_key=qwen_api_key),
25
+ tools=[DuckDuckGo()],
26
+ instructions=["Always include sources"],
27
+ show_tool_calls=True,
28
+ markdown=True,
29
+ )
30
+
31
+ # Define Finance Agent
32
+ finance_agent = Agent(
33
+ name="Finance Agent",
34
+ role="Get financial data",
35
+ model=Groq(id="qwen-2.5-coder-32b", api_key=qwen_api_key),
36
+ tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
37
+ instructions=["Use tables to display data"],
38
+ show_tool_calls=True,
39
+ markdown=True,
40
+ )
41
+
42
+ # Create agent team
43
+ agent_team = Agent(
44
+ model=Groq(id="deepseek-r1-distill-llama-70b", api_key=deepseek_api_key),
45
+ team=[web_agent, finance_agent],
46
+ instructions=["Always include sources", "Use tables to display data"],
47
+ show_tool_calls=True,
48
+ markdown=True,
49
+ )
50
+
51
+ # Streamlit UI
52
+ st.set_page_config(page_title="AI Chat Assistant", layout="wide")
53
+ st.title("🤖 AI Chat Assistant")
54
+
55
+ # User input
56
+ if prompt := st.chat_input("Ask me anything..."):
57
+ with st.chat_message("user"):
58
+ st.markdown(prompt)
59
+
60
+ with st.chat_message("assistant"):
61
+ response_container = st.empty()
62
+ response_text = ""
63
+
64
+ # Check available response method
65
+ if hasattr(agent_team, "respond") and callable(agent_team.respond):
66
+ try:
67
+ response_text = agent_team.respond(prompt) or "No response received."
68
+ response_container.markdown(response_text)
69
+ except Exception as e:
70
+ error_message = f"Error: {str(e)}"
71
+ st.error(error_message)
72
+ response_text = error_message
73
+ else:
74
+ error_message = "Error: Agent does not support responses."
75
+ st.error(error_message)
76
+ response_text = error_message