Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.agents import create_csv_agent
|
2 |
+
from langchain.agents.agent_types import AgentType
|
3 |
+
from langchain.llms import OpenAI
|
4 |
+
import streamlit as st
|
5 |
+
|
6 |
+
def main():
|
7 |
+
st.set_page_config(page_title="Ask your CSV")
|
8 |
+
st.header("Ask your CSV 📈")
|
9 |
+
|
10 |
+
# API key input
|
11 |
+
api_key = st.text_input("Enter your OpenAI API key:", type="password")
|
12 |
+
|
13 |
+
# File uploader
|
14 |
+
csv_file = st.file_uploader("Upload a CSV file", type="csv")
|
15 |
+
|
16 |
+
# Process only if both API key and file are provided
|
17 |
+
if api_key and csv_file is not None:
|
18 |
+
# Create agent with user-provided API key
|
19 |
+
try:
|
20 |
+
agent = create_csv_agent(
|
21 |
+
OpenAI(temperature=0, api_key=api_key),
|
22 |
+
csv_file,
|
23 |
+
verbose=True,
|
24 |
+
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION
|
25 |
+
)
|
26 |
+
|
27 |
+
# User question input
|
28 |
+
user_question = st.text_input("Ask a question about your CSV: ")
|
29 |
+
|
30 |
+
# Process question if provided
|
31 |
+
if user_question and user_question != "":
|
32 |
+
with st.spinner(text="In progress..."):
|
33 |
+
st.write(agent.run(user_question))
|
34 |
+
except Exception as e:
|
35 |
+
st.error(f"Error: {str(e)}")
|
36 |
+
elif not api_key and csv_file is not None:
|
37 |
+
st.info("Please enter your OpenAI API key to proceed.")
|
38 |
+
elif api_key and csv_file is None:
|
39 |
+
st.info("Please upload a CSV file to proceed.")
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
main()
|