Spaces:
No application file
No application file
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
| 34 |
+
tools=tools,
|
| 35 |
+
llm=llm,
|
| 36 |
+
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 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.")
|