Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +69 -0
- htmlTemplate.py +74 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
import streamlit as st
|
3 |
+
from langchain_experimental.agents import create_csv_agent
|
4 |
+
from langchain.llms import HuggingFaceHub
|
5 |
+
from htmlTemplate import css, bot_template, user_template
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
def main():
|
10 |
+
st.set_page_config(
|
11 |
+
page_title= "QnA with CSV",
|
12 |
+
page_icon=":robot_face:",
|
13 |
+
layout="wide"
|
14 |
+
)
|
15 |
+
st.write(css, unsafe_allow_html=True)
|
16 |
+
|
17 |
+
st.title("QnA with CSV 🤖")
|
18 |
+
user_csv = st.file_uploader(label = "", type= "csv")
|
19 |
+
if not user_csv:
|
20 |
+
st.warning("Please Upload your CSV file 🚨")
|
21 |
+
|
22 |
+
llm = HuggingFaceHub(
|
23 |
+
repo_id = "mistralai/Mistral-7B-Instruct-v0.2",
|
24 |
+
model_kwargs = {
|
25 |
+
'max_new_tokens': 249,
|
26 |
+
'temperature': 0.3,
|
27 |
+
}
|
28 |
+
)
|
29 |
+
user_input = st.chat_input("Ask your Question about CSV files",
|
30 |
+
disabled= not user_csv)
|
31 |
+
|
32 |
+
with st.sidebar:
|
33 |
+
st.subheader("Example Questions:")
|
34 |
+
example_questions = [
|
35 |
+
"What is the total number of rows in the CSV?",
|
36 |
+
"Can you show me the first 5 rows of the CSV?",
|
37 |
+
"What are the column names in the CSV?",
|
38 |
+
"How many columns does the CSV have?",
|
39 |
+
"What is the data type of a specific column in the CSV?",
|
40 |
+
"Can you provide a summary statistics for the numerical columns?",
|
41 |
+
"Are there any missing values in the CSV?",
|
42 |
+
"Can you filter the data based on a specific condition?",
|
43 |
+
"What is the average value of a numerical column?",
|
44 |
+
]
|
45 |
+
|
46 |
+
selected_example = st.selectbox("Select an example question:", example_questions, disabled= not user_csv)
|
47 |
+
|
48 |
+
if not user_csv:
|
49 |
+
st.warning("Please Upload your CSV file 🚨")
|
50 |
+
|
51 |
+
if st.button("Use Selected Example"):
|
52 |
+
user_input = selected_example
|
53 |
+
|
54 |
+
if user_csv is not None:
|
55 |
+
agent = create_csv_agent(
|
56 |
+
llm = llm,
|
57 |
+
path = user_csv,
|
58 |
+
verbose = True,
|
59 |
+
handle_parsing_errors=True
|
60 |
+
)
|
61 |
+
|
62 |
+
if user_input is not None and user_input != "":
|
63 |
+
st.write(user_template.replace("{{MSG}}",user_input), unsafe_allow_html= True)
|
64 |
+
with st.spinner("Processing..."):
|
65 |
+
response = agent.run(user_input)
|
66 |
+
st.write(bot_template.replace("{{MSG}}",response), unsafe_allow_html= True)
|
67 |
+
|
68 |
+
if __name__ == "__main__":
|
69 |
+
main()
|
htmlTemplate.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Updated CSS
|
2 |
+
# CSS Styles
|
3 |
+
css = '''
|
4 |
+
<style>
|
5 |
+
/* Styling for the chat messages */
|
6 |
+
.chat-message {
|
7 |
+
padding: 1rem;
|
8 |
+
border-radius: 0.5rem;
|
9 |
+
margin-bottom: 1rem;
|
10 |
+
display: flex;
|
11 |
+
border: 1px solid #d3d3d3; /* Add a subtle border */
|
12 |
+
}
|
13 |
+
|
14 |
+
/* Styling for user messages */
|
15 |
+
.chat-message.user {
|
16 |
+
background-color: #2b313e;
|
17 |
+
}
|
18 |
+
|
19 |
+
/* Styling for bot messages */
|
20 |
+
.chat-message.bot {
|
21 |
+
background-color: #475063;
|
22 |
+
}
|
23 |
+
|
24 |
+
/* Styling for the avatar */
|
25 |
+
.chat-message .avatar {
|
26 |
+
width: 15%; /* Adjust avatar size */
|
27 |
+
}
|
28 |
+
|
29 |
+
/* Styling for the avatar image */
|
30 |
+
.chat-message .avatar img {
|
31 |
+
max-width: 60px;
|
32 |
+
max-height: 60px;
|
33 |
+
border-radius: 50%;
|
34 |
+
object-fit: cover;
|
35 |
+
}
|
36 |
+
|
37 |
+
/* Styling for the message content */
|
38 |
+
.chat-message .message {
|
39 |
+
width: auto; /* Adjust message width */
|
40 |
+
padding: 0.75rem;
|
41 |
+
color: #fff;
|
42 |
+
margin-right: 1rem; /* Add margin to the left of the message */
|
43 |
+
margin-left: 1rem;
|
44 |
+
}
|
45 |
+
|
46 |
+
/* Styling for strong (name) in the message */
|
47 |
+
.chat-message .message strong {
|
48 |
+
margin-right: 0.25rem; /* Adjust the margin as needed */
|
49 |
+
}
|
50 |
+
</style>
|
51 |
+
'''
|
52 |
+
|
53 |
+
# HTML Templates for Bot and User Messages
|
54 |
+
bot_template = '''
|
55 |
+
<div class="chat-message bot">
|
56 |
+
<div class="avatar">
|
57 |
+
<img src="https://i.ibb.co/3pvQJ2B/bot-icon.jpg">
|
58 |
+
</div>
|
59 |
+
<div class="message">
|
60 |
+
<strong>Baymax:</strong> {{MSG}}
|
61 |
+
</div>
|
62 |
+
</div>
|
63 |
+
'''
|
64 |
+
|
65 |
+
user_template = '''
|
66 |
+
<div class="chat-message user">
|
67 |
+
<div class="avatar">
|
68 |
+
<img src="https://i.ibb.co/HY8rRpL/human.jpg">
|
69 |
+
</div>
|
70 |
+
<div class="message">
|
71 |
+
<strong>Tadashi:</strong> {{MSG}}
|
72 |
+
</div>
|
73 |
+
</div>
|
74 |
+
'''
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub
|
2 |
+
python-dotenv
|
3 |
+
langchain
|
4 |
+
sentence-transformers
|
5 |
+
streamlit
|
6 |
+
langchain-experimental
|
7 |
+
tabulate
|