Spaces:
Configuration error
Configuration error
init
Browse files- app.py +90 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from langchain.agents import create_csv_agent
|
4 |
+
from langchain.llms import OpenAI
|
5 |
+
|
6 |
+
# Set Streamlit page configuration
|
7 |
+
st.set_page_config(page_title='CSV Processing',
|
8 |
+
page_icon=":memo:",
|
9 |
+
layout='wide',
|
10 |
+
initial_sidebar_state='collapsed')
|
11 |
+
|
12 |
+
# Set CSS properties for HTML components
|
13 |
+
st.markdown("""
|
14 |
+
<style>
|
15 |
+
body {
|
16 |
+
color: #fff;
|
17 |
+
background-color: #4f8bf9;
|
18 |
+
}
|
19 |
+
h1, h2 {
|
20 |
+
color: #ffdd00;
|
21 |
+
}
|
22 |
+
</style>
|
23 |
+
""", unsafe_allow_html=True)
|
24 |
+
|
25 |
+
hide_style='''
|
26 |
+
<style>
|
27 |
+
#MainMenu {visibility:hidden;}
|
28 |
+
footer {visibility:hidden;}
|
29 |
+
.css-hi6a2p {padding-top: 0rem;}
|
30 |
+
head {visibility:hidden;}
|
31 |
+
</style>
|
32 |
+
'''
|
33 |
+
st.markdown("""
|
34 |
+
<h1 style='text-align: center; color: #ffdd00;'>XLS Office Documents Analysis with ChatGPT4 NLP Model</h1>
|
35 |
+
""", unsafe_allow_html=True)
|
36 |
+
#st.title('XLS Office Documents Analysis with ChatGPT4 NLP Model')
|
37 |
+
|
38 |
+
def load_data(file):
|
39 |
+
df = pd.read_excel(file, engine='openpyxl')
|
40 |
+
df.to_csv('data.csv', index=False) # Convert XLS to CSV
|
41 |
+
return 'data.csv'
|
42 |
+
|
43 |
+
def initialize_agent(file, openai_api_key):
|
44 |
+
agent = create_csv_agent(OpenAI(temperature=0, openai_api_key=openai_api_key), file, verbose=True)
|
45 |
+
return agent
|
46 |
+
|
47 |
+
uploaded_file = st.file_uploader("Upload XLS", type=['xlsx'])
|
48 |
+
st.markdown(hide_style, unsafe_allow_html=True)
|
49 |
+
|
50 |
+
openai_api_key = st.sidebar.text_input('OpenAI API Key', type="password")
|
51 |
+
|
52 |
+
# Pre-defined question examples
|
53 |
+
question_examples = [
|
54 |
+
"how many rows are there?",
|
55 |
+
"how many people are female?",
|
56 |
+
"how many people have stayed more than 3 years in the city?",
|
57 |
+
"how many people have stayed more than 3 years in the city and are female?",
|
58 |
+
"Are there more males or females?",
|
59 |
+
"What are the column names?",
|
60 |
+
"What is the average age?",
|
61 |
+
"Which country appears the most and how many times does it appear?",
|
62 |
+
"What is the ratio of males to females?"
|
63 |
+
# Add more examples as needed
|
64 |
+
]
|
65 |
+
|
66 |
+
# Dropdown select box for question examples
|
67 |
+
selected_example = st.selectbox('Choose a question example:', question_examples)
|
68 |
+
|
69 |
+
# Pre-populate the question field with the selected example
|
70 |
+
question = st.text_input('Enter your question:', value=selected_example)
|
71 |
+
|
72 |
+
if not openai_api_key or not openai_api_key.startswith('sk-'):
|
73 |
+
st.warning('Please enter your OpenAI API key!', icon='⚠️')
|
74 |
+
else:
|
75 |
+
if uploaded_file is not None:
|
76 |
+
# Create a progress bar
|
77 |
+
progress_bar = st.progress(0)
|
78 |
+
progress_bar.progress(25) # Start the progress at 25%
|
79 |
+
|
80 |
+
csv_file = load_data(uploaded_file) # Now the uploaded file is an XLS file
|
81 |
+
progress_bar.progress(50) # Update the progress to 50%
|
82 |
+
|
83 |
+
agent = initialize_agent(csv_file, openai_api_key)
|
84 |
+
progress_bar.progress(100) # Complete the progress bar
|
85 |
+
|
86 |
+
if question:
|
87 |
+
response = agent.run(question)
|
88 |
+
#st.markdown(f'**Response:** {response}')
|
89 |
+
st.markdown(f'<div style="color: red; font-size: 24px; text-align: center;">**{response}**</div>',unsafe_allow_html=True)
|
90 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
openai
|
3 |
+
langchain
|
4 |
+
pandas
|
5 |
+
openpyxl
|