DrishtiSharma commited on
Commit
211910d
·
verified ·
1 Parent(s): 3c6e954

Update app1.py

Browse files
Files changed (1) hide show
  1. app1.py +92 -0
app1.py CHANGED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from pandasai import SmartDataframe
4
+ from pandasai.llm import OpenAI
5
+ from dotenv import load_dotenv
6
+ from datasets import load_dataset
7
+ import os
8
+
9
+ # Load environment variables
10
+ load_dotenv()
11
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
12
+
13
+ if not OPENAI_API_KEY:
14
+ st.error("OpenAI API Key is missing. Make sure you set it in a .env file.")
15
+ st.stop()
16
+
17
+ # Initialize OpenAI LLM
18
+ llm = OpenAI(api_token=OPENAI_API_KEY)
19
+
20
+ # App title and description
21
+ st.title("Patent Analytics: Chat With Your Dataset")
22
+ st.markdown(
23
+ """
24
+ Upload a CSV file or load a dataset from Hugging Face to:
25
+ - Analyze data with natural language queries.
26
+ - Visualize trends and insights (e.g., "Plot the number of patents filed per year").
27
+ """
28
+ )
29
+
30
+ # Initialize session state for the dataframe
31
+ if "df" not in st.session_state:
32
+ st.session_state.df = None
33
+
34
+ # Dataset input options
35
+ input_option = st.sidebar.radio(
36
+ "Choose Dataset Input Method",
37
+ options=["Use Hugging Face Dataset", "Upload CSV File"],
38
+ index=0
39
+ )
40
+
41
+ # Dataset loading logic
42
+ if input_option == "Use Hugging Face Dataset":
43
+ dataset_name = st.sidebar.text_input("Enter Hugging Face Dataset Name:", value="HUPD/hupd")
44
+ if st.sidebar.button("Load Dataset"):
45
+ try:
46
+ dataset = load_dataset(dataset_name, name="sample", split="train", trust_remote_code=True)
47
+ st.session_state.df = pd.DataFrame(dataset)
48
+ st.sidebar.success(f"Dataset '{dataset_name}' loaded successfully!")
49
+ except Exception as e:
50
+ st.sidebar.error(f"Error loading dataset: {e}")
51
+ elif input_option == "Upload CSV File":
52
+ uploaded_file = st.sidebar.file_uploader("Upload CSV File:", type=["csv"])
53
+ if uploaded_file:
54
+ try:
55
+ st.session_state.df = pd.read_csv(uploaded_file)
56
+ st.sidebar.success("File uploaded successfully!")
57
+ except Exception as e:
58
+ st.sidebar.error(f"Error loading file: {e}")
59
+
60
+ # Show the loaded dataframe preview
61
+ if st.session_state.df is not None:
62
+ st.subheader("Dataset Preview")
63
+ st.dataframe(st.session_state.df.head(10))
64
+
65
+ # Create a SmartDataFrame for PandasAI
66
+ chat_df = SmartDataframe(st.session_state.df, config={"llm": llm})
67
+
68
+ # Input box for user questions
69
+ question = st.text_input(
70
+ "Ask a question about your data or request a visualization",
71
+ placeholder="E.g., 'Which assignee has the most patents?' or 'Plot patent filings per year'",
72
+ )
73
+
74
+ if question:
75
+ with st.spinner("Processing your request..."):
76
+ try:
77
+ # Chat with the dataframe
78
+ response = chat_df.chat(question)
79
+
80
+ # Detect visualizations in the query
81
+ if "plot" in question.lower() or "graph" in question.lower():
82
+ st.write("### Visualization")
83
+ else:
84
+ st.write("### Response")
85
+
86
+ # Display response or plot
87
+ st.write(response)
88
+ st.success("Request processed successfully!")
89
+ except Exception as e:
90
+ st.error(f"An error occurred: {e}")
91
+ else:
92
+ st.write("Upload a CSV file or load a dataset to get started.")