Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
st.
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|