reddmann007 commited on
Commit
0d55deb
·
verified ·
1 Parent(s): 6a3775b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -55
app.py CHANGED
@@ -1,55 +1,24 @@
1
- import streamlit as st
2
- import os
3
- from dotenv import load_dotenv
4
- from langchain_groq import ChatGroq
5
- from langchain_community.agent_toolkits.load_tools import load_tools
6
- from langgraph.prebuilt import create_react_agent
7
- import langchainhub as hub
8
-
9
- # 1. Setup Page & Secrets
10
- st.set_page_config(page_title="My First AI Math Agent", layout="centered")
11
- st.title("My First AI Math Agent 🧮")
12
-
13
- # Look for API Key in environment (Local .env or HF Secrets)
14
- load_dotenv()
15
- api_key = os.getenv("GROQ_API_KEY")
16
-
17
- if not api_key:
18
- st.error("Missing GROQ_API_KEY! Please check your secrets.")
19
- st.stop()
20
-
21
- # 2. Initialize the Agent (Cached so it doesn't reload every click)
22
- @st.cache_resource
23
- def get_agent():
24
- llm = ChatGroq(model_name="llama-3.3-70b-versatile", temperature=0)
25
- tools = load_tools(["llm-math"], llm=llm)
26
- # TODO: Create the agent using the same function we used in your terminal script
27
- return create_react_agent(llm, tools)
28
-
29
- agent_executor = get_agent()
30
-
31
- # 3. Chat History Setup
32
- if "messages" not in st.session_state:
33
- st.session_state.messages = []
34
-
35
- for message in st.session_state.messages:
36
- with st.chat_message(message["role"]):
37
- st.markdown(message["content"])
38
-
39
- # 4. The Interaction Loop
40
- if prompt := st.chat_input("What math problem should I solve?"):
41
- st.session_state.messages.append({"role": "user", "content": prompt})
42
- with st.chat_message("user"):
43
- st.markdown(prompt)
44
-
45
- with st.chat_message("assistant"):
46
- with st.spinner("Thinking..."):
47
- # TODO: Call the agent_executor with the correct input format
48
- # Hint: We used .invoke({"messages": [("human", prompt)]}) before!
49
- result = agent_executor.invoke({"messages": [("human", prompt)]})
50
-
51
- # Extract the text from the last message in the response
52
- final_answer = result["messages"][-1].content
53
- st.markdown(final_answer)
54
-
55
- st.session_state.messages.append({"role": "assistant", "content": final_answer})
 
1
+ import streamlit as st
2
+ import os
3
+ from langchain_groq import ChatGroq
4
+ from langchain_community.agent_toolkits.load_tools import load_tools
5
+ from langgraph.prebuilt import create_react_agent
6
+
7
+ # 1. Page Config (Best practice to put this at the very top)
8
+ st.set_page_config(page_title="AI Math Agent", page_icon="🧮")
9
+
10
+ st.title("My First AI Math Agent 🧮")
11
+
12
+ # 2. Secret Retrieval
13
+ api_key = os.getenv("GROQ_API_KEY")
14
+
15
+ # 3. Validation Logic
16
+ if not api_key:
17
+ st.warning("⚠️ Waiting for GROQ_API_KEY... Please add it to your Space Secrets in Settings.")
18
+ st.info("After adding the secret, you may need to 'Restart' the Space.")
19
+ st.stop()
20
+ else:
21
+ st.success("🚀 API Key connected! The agent is ready.")
22
+
23
+ # Simple placeholder for your agent logic
24
+ st.write("Next step: Initialize your ChatGroq model here.")