Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
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
|
@@ -12,24 +11,17 @@ tqa = pipeline(task="table-question-answering",
|
|
12 |
device=0) # Assuming GPU is available, otherwise set device="cpu"
|
13 |
|
14 |
# Title and Introduction
|
15 |
-
st.title("Data
|
16 |
st.markdown("""
|
17 |
-
This app allows you to upload a table (CSV or Excel) and ask questions to
|
18 |
-
Using **TAPAS**, the app can interpret your questions and
|
19 |
|
20 |
### Available Features:
|
21 |
-
- **
|
22 |
-
- **Line Graph**: Visualize a single column over time.
|
23 |
|
24 |
-
Upload your data and ask questions
|
25 |
""")
|
26 |
|
27 |
-
# Language Selection
|
28 |
-
language = st.selectbox(
|
29 |
-
"Select the language of your question",
|
30 |
-
("English", "German", "French", "Spanish", "Italian", "Others")
|
31 |
-
)
|
32 |
-
|
33 |
# File uploader in the sidebar
|
34 |
file_name = st.sidebar.file_uploader("Upload file:", type=['csv', 'xlsx'])
|
35 |
|
@@ -48,81 +40,46 @@ else:
|
|
48 |
df = None
|
49 |
|
50 |
if df is not None:
|
51 |
-
#
|
|
|
|
|
52 |
st.write("Original Data:")
|
53 |
st.write(df)
|
54 |
|
55 |
-
# Display a sample of data for
|
56 |
-
st.write("Sample data
|
57 |
st.write(df.head())
|
58 |
|
59 |
except Exception as e:
|
60 |
st.error(f"Error reading file: {str(e)}")
|
61 |
|
62 |
# User input for the question
|
63 |
-
question = st.text_input(f'Ask your
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
else:
|
72 |
-
# Use TAPAS model to process the question
|
73 |
-
result = tqa(table=df, query=question)
|
74 |
-
|
75 |
-
# Display the raw output from TAPAS
|
76 |
-
st.write("TAPAS Raw Output (Response):")
|
77 |
-
st.write(result) # This will display the raw output from TAPAS
|
78 |
-
|
79 |
-
# Optionally, you can output the raw output as plain text:
|
80 |
-
st.text("Raw TAPAS Output (Plain Text):")
|
81 |
-
st.text(str(result)) # This will display raw output as plain text
|
82 |
-
|
83 |
-
# Check if TAPAS is returning the expected answer
|
84 |
-
answer = result.get('answer', None)
|
85 |
-
if answer:
|
86 |
-
st.write(f"TAPAS Answer: {answer}")
|
87 |
else:
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
y_data = y_data[:min_length]
|
104 |
-
|
105 |
-
# Create the scatter plot
|
106 |
-
fig = px.scatter(x=x_data, y=y_data, title=f"Scatter Plot between {columns[0]} and {columns[1]}")
|
107 |
-
st.plotly_chart(fig, use_container_width=True)
|
108 |
-
st.success(f"Here is the scatter plot between '{columns[0]}' and '{columns[1]}'.")
|
109 |
-
else:
|
110 |
-
st.warning("Columns not found in the dataset or the question format is incorrect.")
|
111 |
-
elif 'column' in question.lower():
|
112 |
-
# This is a request for a line graph (single column)
|
113 |
-
column = question.split('of')[-1].strip() # Handle 'of' keyword
|
114 |
-
if column in df.columns:
|
115 |
-
# Prepare the data for Plotly (line graph)
|
116 |
-
column_data = df[column].dropna() # Drop NaN values
|
117 |
-
|
118 |
-
# Create the line plot
|
119 |
-
fig = px.line(x=column_data.index, y=column_data, title=f"Graph of column '{column}'")
|
120 |
-
st.plotly_chart(fig, use_container_width=True)
|
121 |
-
st.success(f"Here is the graph of column '{column}'.")
|
122 |
else:
|
123 |
-
st.warning(
|
124 |
-
else:
|
125 |
-
st.warning("Please ask a valid graph-related question (e.g., 'make a graph between column1 and column2').")
|
126 |
|
127 |
-
|
128 |
-
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
3 |
from transformers import pipeline
|
4 |
|
5 |
# Set the page layout for Streamlit
|
|
|
11 |
device=0) # Assuming GPU is available, otherwise set device="cpu"
|
12 |
|
13 |
# Title and Introduction
|
14 |
+
st.title("Data Table with TAPAS NLP Integration")
|
15 |
st.markdown("""
|
16 |
+
This app allows you to upload a table (CSV or Excel) and ask questions to extract information from the data.
|
17 |
+
Using **TAPAS**, the app can interpret your questions and provide the corresponding answers.
|
18 |
|
19 |
### Available Features:
|
20 |
+
- **Table Question Answering**: Ask questions related to the uploaded table.
|
|
|
21 |
|
22 |
+
Upload your data and ask questions to extract answers.
|
23 |
""")
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# File uploader in the sidebar
|
26 |
file_name = st.sidebar.file_uploader("Upload file:", type=['csv', 'xlsx'])
|
27 |
|
|
|
40 |
df = None
|
41 |
|
42 |
if df is not None:
|
43 |
+
# Convert object columns to numeric where possible
|
44 |
+
df = df.apply(pd.to_numeric, errors='ignore')
|
45 |
+
|
46 |
st.write("Original Data:")
|
47 |
st.write(df)
|
48 |
|
49 |
+
# Display a sample of data for user reference
|
50 |
+
st.write("Sample data:")
|
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 question
|
57 |
+
question = st.text_input(f'Ask your question related to the table')
|
58 |
+
|
59 |
+
with st.spinner():
|
60 |
+
if st.button('Get Answer'):
|
61 |
+
try:
|
62 |
+
# Ensure the question is a valid string
|
63 |
+
if not question or not isinstance(question, str):
|
64 |
+
st.error("Please enter a valid question.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
else:
|
66 |
+
# Use TAPAS model to process the question
|
67 |
+
result = tqa(table=df, query=question)
|
68 |
+
|
69 |
+
# Display the raw output from TAPAS
|
70 |
+
st.write("TAPAS Raw Output (Response):")
|
71 |
+
st.write(result) # This will display the raw output from TAPAS
|
72 |
+
|
73 |
+
# Optionally, you can output the raw output as plain text:
|
74 |
+
st.text("Raw TAPAS Output (Plain Text):")
|
75 |
+
st.text(str(result)) # This will display raw output as plain text
|
76 |
+
|
77 |
+
# Check if TAPAS is returning the expected answer
|
78 |
+
answer = result.get('answer', None)
|
79 |
+
if answer:
|
80 |
+
st.write(f"TAPAS Answer: {answer}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
else:
|
82 |
+
st.warning("TAPAS did not return a valid answer.")
|
|
|
|
|
83 |
|
84 |
+
except Exception as e:
|
85 |
+
st.warning(f"Error processing question or generating answer: {str(e)}")
|