hertogateis commited on
Commit
b1c60f6
·
verified ·
1 Parent(s): e72b6dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -79
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 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
- # 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
- # Show the original data with text columns intact
 
 
52
  st.write("Original Data:")
53
  st.write(df)
54
 
55
- # Display a sample of data for graph generation
56
- st.write("Sample data for graph generation:")
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 graph-related question in {language}')
64
-
65
- with st.spinner():
66
- if st.button('Generate Graph'):
67
- try:
68
- # Ensure the question is a valid string
69
- if not question or not isinstance(question, str):
70
- st.error("Please enter a valid question in the form of text.")
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
- st.warning("TAPAS did not return a valid answer.")
89
-
90
- # Determine if the question relates to graph generation
91
- if 'between' in question.lower() and 'and' in question.lower():
92
- # This is a request for a scatter plot (two columns)
93
- columns = question.split('between')[-1].split('and')
94
- columns = [col.strip() for col in columns]
95
- if len(columns) == 2 and all(col in df.columns for col in columns):
96
- # Prepare the data for Plotly (scatter plot)
97
- x_data = df[columns[0]].dropna() # Extract x column, drop NaN values
98
- y_data = df[columns[1]].dropna() # Extract y column, drop NaN values
99
-
100
- # Ensure x_data and y_data have the same length
101
- min_length = min(len(x_data), len(y_data))
102
- x_data = x_data[:min_length]
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(f"Column '{column}' not found in the data.")
124
- else:
125
- st.warning("Please ask a valid graph-related question (e.g., 'make a graph between column1 and column2').")
126
 
127
- except Exception as e:
128
- st.warning(f"Error processing question or generating graph: {str(e)}")
 
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)}")