CoderHassan commited on
Commit
23764bd
Β·
verified Β·
1 Parent(s): 6a26864

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -24
app.py CHANGED
@@ -1,33 +1,24 @@
1
- import streamlit as st
2
- from dotenv import load_dotenv
3
  import os
4
  from langchain.chat_models import ChatOpenAI
5
- from langchain.agents import initialize_agent, Tool
6
- from langchain.agents import AgentType
7
 
8
- # Load API key from .env
9
- load_dotenv()
10
  api_key = os.getenv("OPENAI_API_KEY")
11
 
12
- # Streamlit UI
13
- st.set_page_config(page_title="πŸ€– Agentic AI Assistant", layout="wide")
14
  st.title("πŸ€– Agentic AI Application")
15
- st.write("Ask me anything or give me a task β€” I can reason and use tools!")
16
 
17
- # Create input box
18
- user_query = st.text_input("🧩 Your question or task:", placeholder="e.g., Calculate 45 * 23, or summarize a topic...")
19
 
20
- # Setup AI agent
21
- if api_key:
 
22
  llm = ChatOpenAI(openai_api_key=api_key, model="gpt-4o-mini", temperature=0)
23
 
24
- # Define tools
25
  tools = [
26
- Tool(
27
- name="Calculator",
28
- func=lambda x: str(eval(x)),
29
- description="Useful for solving math problems."
30
- ),
31
  ]
32
 
33
  agent = initialize_agent(
@@ -37,11 +28,7 @@ if api_key:
37
  verbose=True
38
  )
39
 
40
- # Run agent on user query
41
  if st.button("πŸš€ Run Agent") and user_query:
42
- with st.spinner("πŸ€” Thinking..."):
43
  result = agent.run(user_query)
44
  st.success(result)
45
-
46
- else:
47
- st.error("❌ Missing OpenAI API key. Please add it to your .env file.")
 
 
 
1
  import os
2
  from langchain.chat_models import ChatOpenAI
3
+ from langchain.agents import initialize_agent, Tool, AgentType
4
+ import streamlit as st
5
 
6
+ # Get API key from Hugging Face secret
 
7
  api_key = os.getenv("OPENAI_API_KEY")
8
 
9
+ st.set_page_config(page_title="πŸ€– Agentic AI App", layout="wide")
 
10
  st.title("πŸ€– Agentic AI Application")
11
+ st.write("Ask me anything β€” I can reason, calculate, or search the web!")
12
 
13
+ user_query = st.text_input("🧠 Your question or task:")
 
14
 
15
+ if not api_key:
16
+ st.error("Missing API key! Add OPENAI_API_KEY in Hugging Face β†’ Settings β†’ Secrets.")
17
+ else:
18
  llm = ChatOpenAI(openai_api_key=api_key, model="gpt-4o-mini", temperature=0)
19
 
 
20
  tools = [
21
+ Tool(name="Calculator", func=lambda x: str(eval(x)), description="For math tasks."),
 
 
 
 
22
  ]
23
 
24
  agent = initialize_agent(
 
28
  verbose=True
29
  )
30
 
 
31
  if st.button("πŸš€ Run Agent") and user_query:
32
+ with st.spinner("Thinking..."):
33
  result = agent.run(user_query)
34
  st.success(result)