Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,27 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
|
|
4 |
|
5 |
# Set the page layout for Streamlit
|
6 |
st.set_page_config(layout="wide")
|
7 |
|
|
|
|
|
|
|
|
|
|
|
8 |
# Title and Introduction
|
9 |
-
st.title("Data Visualization App")
|
10 |
st.markdown("""
|
11 |
-
This app allows you to upload a table (CSV or Excel) and ask
|
12 |
-
|
13 |
|
14 |
### Available Features:
|
15 |
-
- **
|
16 |
-
- **
|
17 |
|
18 |
-
Upload your data and ask questions to generate visualizations.
|
19 |
""")
|
20 |
|
21 |
# File uploader in the sidebar
|
@@ -36,31 +42,35 @@ else:
|
|
36 |
df = None
|
37 |
|
38 |
if df is not None:
|
39 |
-
|
40 |
-
|
41 |
-
df[col] = pd.to_numeric(df[col], errors='ignore')
|
42 |
|
43 |
st.write("Original Data:")
|
44 |
st.write(df)
|
45 |
|
46 |
-
|
47 |
-
df = df.astype(str)
|
48 |
-
|
49 |
-
# Display the first 5 rows of the dataframe in an editable grid
|
50 |
st.write("Sample data for graph generation:")
|
51 |
st.write(df.head())
|
52 |
|
53 |
except Exception as e:
|
54 |
st.error(f"Error reading file: {str(e)}")
|
55 |
|
56 |
-
# User input for the
|
57 |
question = st.text_input('Ask your graph-related question')
|
58 |
|
59 |
with st.spinner():
|
60 |
if st.button('Generate Graph'):
|
61 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
if 'between' in question.lower() and 'and' in question.lower():
|
63 |
-
#
|
64 |
columns = question.split('between')[-1].split('and')
|
65 |
columns = [col.strip() for col in columns]
|
66 |
if len(columns) == 2 and all(col in df.columns for col in columns):
|
@@ -70,7 +80,7 @@ else:
|
|
70 |
else:
|
71 |
st.warning("Columns not found in the dataset.")
|
72 |
elif 'column' in question.lower():
|
73 |
-
#
|
74 |
column = question.split('of')[-1].strip()
|
75 |
if column in df.columns:
|
76 |
fig = px.line(df, x=df.index, y=column, title=f"Graph of column '{column}'")
|
@@ -82,4 +92,4 @@ else:
|
|
82 |
st.warning("Please ask a valid graph-related question (e.g., 'make a graph between column1 and column2').")
|
83 |
|
84 |
except Exception as e:
|
85 |
-
st.warning(f"Error processing question or generating graph: {str(e)}")
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
+
from transformers import pipeline
|
5 |
|
6 |
# Set the page layout for Streamlit
|
7 |
st.set_page_config(layout="wide")
|
8 |
|
9 |
+
# Initialize TAPAS pipeline for table-based question answering
|
10 |
+
tqa = pipeline(task="table-question-answering",
|
11 |
+
model="google/tapas-large-finetuned-wtq",
|
12 |
+
device=0) # Assuming GPU is available, otherwise set device="cpu"
|
13 |
+
|
14 |
# Title and Introduction
|
15 |
+
st.title("Data Visualization App with TAPAS NLP Integration")
|
16 |
st.markdown("""
|
17 |
+
This app allows you to upload a table (CSV or Excel) and ask questions to generate graphs visualizing the data.
|
18 |
+
Using **TAPAS**, the app can interpret your questions and generate the corresponding graphs.
|
19 |
|
20 |
### Available Features:
|
21 |
+
- **Scatter Plot**: Visualize relationships between two columns.
|
22 |
+
- **Line Graph**: Visualize a single column over time.
|
23 |
|
24 |
+
Upload your data and ask questions about the data to generate visualizations.
|
25 |
""")
|
26 |
|
27 |
# File uploader in the sidebar
|
|
|
42 |
df = None
|
43 |
|
44 |
if df is not None:
|
45 |
+
# Convert object columns to numeric where possible
|
46 |
+
df = df.apply(pd.to_numeric, errors='ignore')
|
|
|
47 |
|
48 |
st.write("Original Data:")
|
49 |
st.write(df)
|
50 |
|
51 |
+
# Display a sample of data for graph generation
|
|
|
|
|
|
|
52 |
st.write("Sample data for graph generation:")
|
53 |
st.write(df.head())
|
54 |
|
55 |
except Exception as e:
|
56 |
st.error(f"Error reading file: {str(e)}")
|
57 |
|
58 |
+
# User input for the question
|
59 |
question = st.text_input('Ask your graph-related question')
|
60 |
|
61 |
with st.spinner():
|
62 |
if st.button('Generate Graph'):
|
63 |
try:
|
64 |
+
# Use TAPAS model to process the question
|
65 |
+
result = tqa(table=df, query=question)
|
66 |
+
|
67 |
+
# Check if TAPAS is returning column names that could be used for graphing
|
68 |
+
answer = result['answer']
|
69 |
+
st.write(f"TAPAS Answer: {answer}")
|
70 |
+
|
71 |
+
# Determine if the question relates to graph generation
|
72 |
if 'between' in question.lower() and 'and' in question.lower():
|
73 |
+
# This is a request for a scatter plot (two columns)
|
74 |
columns = question.split('between')[-1].split('and')
|
75 |
columns = [col.strip() for col in columns]
|
76 |
if len(columns) == 2 and all(col in df.columns for col in columns):
|
|
|
80 |
else:
|
81 |
st.warning("Columns not found in the dataset.")
|
82 |
elif 'column' in question.lower():
|
83 |
+
# This is a request for a line graph (single column)
|
84 |
column = question.split('of')[-1].strip()
|
85 |
if column in df.columns:
|
86 |
fig = px.line(df, x=df.index, y=column, title=f"Graph of column '{column}'")
|
|
|
92 |
st.warning("Please ask a valid graph-related question (e.g., 'make a graph between column1 and column2').")
|
93 |
|
94 |
except Exception as e:
|
95 |
+
st.warning(f"Error processing question or generating graph: {str(e)}")
|