Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain.agents import create_react_agent, AgentExecutor
|
| 3 |
from langchain_community.tools import DuckDuckGoSearchRun
|
|
@@ -6,10 +7,11 @@ from langchain.prompts import PromptTemplate
|
|
| 6 |
from langchain.chains import LLMChain
|
| 7 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
|
|
|
|
| 9 |
key = os.environ.get("GOOGLE_API_KEY")
|
| 10 |
if not key:
|
| 11 |
st.error("❌ GOOGLE_API_KEY not found. Please set it in your Hugging Face Secrets.")
|
| 12 |
-
|
| 13 |
|
| 14 |
# Set page config
|
| 15 |
st.set_page_config(page_title="Company Info Agent", layout="wide")
|
|
@@ -19,8 +21,7 @@ st.markdown(
|
|
| 19 |
"Ask anything about companies: *stock, revenue, growth, acquisitions, news*, etc."
|
| 20 |
)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
# --- Your custom prompt ---
|
| 24 |
prompt = PromptTemplate(
|
| 25 |
input_variables=["input", "agent_scratchpad", "tool_names", "tools"],
|
| 26 |
template="""You are a smart and reliable business assistant.
|
|
@@ -65,31 +66,29 @@ Thought:{agent_scratchpad}
|
|
| 65 |
"""
|
| 66 |
)
|
| 67 |
|
|
|
|
| 68 |
search_tool = DuckDuckGoSearchRun()
|
| 69 |
-
|
| 70 |
tools = [search_tool]
|
| 71 |
|
| 72 |
-
# --- Initializing the LLM
|
| 73 |
llm = ChatGoogleGenerativeAI(
|
| 74 |
-
api_key
|
| 75 |
-
model
|
| 76 |
)
|
| 77 |
|
| 78 |
-
# User
|
| 79 |
user_input = st.text_input("Enter your question:")
|
| 80 |
|
|
|
|
| 81 |
agent = create_react_agent(llm=llm, tools=tools, prompt=prompt)
|
| 82 |
-
|
| 83 |
-
# Create executor
|
| 84 |
agent_executor = AgentExecutor(
|
| 85 |
agent=agent,
|
| 86 |
tools=tools,
|
| 87 |
handle_parsing_errors=True,
|
| 88 |
)
|
| 89 |
-
|
| 90 |
-
#
|
| 91 |
if user_input:
|
| 92 |
with st.spinner("🔎 Thinking..."):
|
| 93 |
response = agent_executor.invoke({"input": user_input})
|
| 94 |
st.markdown("### 🧠 Answer:")
|
| 95 |
-
st.write(response["output"])
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
from langchain.agents import create_react_agent, AgentExecutor
|
| 4 |
from langchain_community.tools import DuckDuckGoSearchRun
|
|
|
|
| 7 |
from langchain.chains import LLMChain
|
| 8 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 9 |
|
| 10 |
+
# Get API Key from Hugging Face secrets
|
| 11 |
key = os.environ.get("GOOGLE_API_KEY")
|
| 12 |
if not key:
|
| 13 |
st.error("❌ GOOGLE_API_KEY not found. Please set it in your Hugging Face Secrets.")
|
| 14 |
+
st.stop()
|
| 15 |
|
| 16 |
# Set page config
|
| 17 |
st.set_page_config(page_title="Company Info Agent", layout="wide")
|
|
|
|
| 21 |
"Ask anything about companies: *stock, revenue, growth, acquisitions, news*, etc."
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Custom Prompt
|
|
|
|
| 25 |
prompt = PromptTemplate(
|
| 26 |
input_variables=["input", "agent_scratchpad", "tool_names", "tools"],
|
| 27 |
template="""You are a smart and reliable business assistant.
|
|
|
|
| 66 |
"""
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# Tool and LLM
|
| 70 |
search_tool = DuckDuckGoSearchRun()
|
|
|
|
| 71 |
tools = [search_tool]
|
| 72 |
|
|
|
|
| 73 |
llm = ChatGoogleGenerativeAI(
|
| 74 |
+
api_key=key,
|
| 75 |
+
model="gemini-2.0-flash"
|
| 76 |
)
|
| 77 |
|
| 78 |
+
# User Input
|
| 79 |
user_input = st.text_input("Enter your question:")
|
| 80 |
|
| 81 |
+
# Create Agent and Executor
|
| 82 |
agent = create_react_agent(llm=llm, tools=tools, prompt=prompt)
|
|
|
|
|
|
|
| 83 |
agent_executor = AgentExecutor(
|
| 84 |
agent=agent,
|
| 85 |
tools=tools,
|
| 86 |
handle_parsing_errors=True,
|
| 87 |
)
|
| 88 |
+
|
| 89 |
+
# Handle User Input
|
| 90 |
if user_input:
|
| 91 |
with st.spinner("🔎 Thinking..."):
|
| 92 |
response = agent_executor.invoke({"input": user_input})
|
| 93 |
st.markdown("### 🧠 Answer:")
|
| 94 |
+
st.write(response["output"])
|