Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,131 +1,64 @@
|
|
1 |
import os
|
2 |
-
import streamlit as st
|
3 |
-
from st_aggrid import AgGrid
|
4 |
import pandas as pd
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
st.markdown(style, unsafe_allow_html=True)
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
st.write("Original Data:")
|
65 |
-
st.write(df)
|
66 |
-
|
67 |
-
# Create a copy for numerical operations
|
68 |
-
df_numeric = df.copy()
|
69 |
-
df = df.astype(str)
|
70 |
-
|
71 |
-
# Display the first 5 rows of the dataframe in an editable grid
|
72 |
-
grid_response = AgGrid(
|
73 |
-
df.head(5),
|
74 |
-
columns_auto_size_mode='FIT_CONTENTS',
|
75 |
-
editable=True,
|
76 |
-
height=300,
|
77 |
-
width='100%',
|
78 |
-
)
|
79 |
-
|
80 |
-
except Exception as e:
|
81 |
-
st.error(f"Error reading file: {str(e)}")
|
82 |
-
|
83 |
-
# User input for the question
|
84 |
-
question = st.text_input('Type your question')
|
85 |
-
|
86 |
-
# Process the answer using TAPAS and T5
|
87 |
-
with st.spinner():
|
88 |
-
if st.button('Answer'):
|
89 |
-
try:
|
90 |
-
# Get the raw answer from TAPAS
|
91 |
-
raw_answer = tqa(table=df, query=question, truncation=True)
|
92 |
-
|
93 |
-
st.markdown("<p style='font-family:sans-serif;font-size: 0.9rem;'> Raw Result From TAPAS: </p>",
|
94 |
-
unsafe_allow_html=True)
|
95 |
-
st.success(raw_answer)
|
96 |
-
|
97 |
-
# Extract relevant information from the TAPAS result
|
98 |
-
answer = raw_answer['answer']
|
99 |
-
aggregator = raw_answer.get('aggregator', '')
|
100 |
-
coordinates = raw_answer.get('coordinates', [])
|
101 |
-
cells = raw_answer.get('cells', [])
|
102 |
-
|
103 |
-
# Construct a base sentence replacing 'SUM' with the query term
|
104 |
-
base_sentence = f"The {question.lower()} of the selected data is {answer}."
|
105 |
-
if coordinates and cells:
|
106 |
-
rows_info = [f"Row {coordinate[0] + 1}, Column '{df.columns[coordinate[1]]}' with value {cell}"
|
107 |
-
for coordinate, cell in zip(coordinates, cells)]
|
108 |
-
rows_description = " and ".join(rows_info)
|
109 |
-
base_sentence += f" This includes the following data: {rows_description}."
|
110 |
-
|
111 |
-
# Generate a fluent response using the T5 model, rephrasing the base sentence
|
112 |
-
input_text = f"Given the question: '{question}', generate a more human-readable response: {base_sentence}"
|
113 |
-
|
114 |
-
# Tokenize the input and generate a fluent response using T5
|
115 |
-
inputs = t5_tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
116 |
-
summary_ids = t5_model.generate(inputs, max_length=150, num_beams=4, early_stopping=True)
|
117 |
-
|
118 |
-
# Decode the generated text
|
119 |
-
generated_text = t5_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
120 |
-
|
121 |
-
# Display the final generated response
|
122 |
-
st.markdown("<p style='font-family:sans-serif;font-size: 0.9rem;'> Final Generated Response with LLM: </p>", unsafe_allow_html=True)
|
123 |
-
st.success(generated_text)
|
124 |
-
|
125 |
-
except Exception as e:
|
126 |
-
st.warning("Please retype your question and make sure to use the column name and cell value correctly.")
|
127 |
-
|
128 |
-
|
129 |
# Manually fix the aggregator if it returns an incorrect one
|
130 |
if 'MEDIAN' in question.upper() and 'AVERAGE' in aggregator.upper():
|
131 |
aggregator = 'MEDIAN'
|
@@ -139,23 +72,27 @@ elif 'TOTAL' in question.upper() and 'SUM' in aggregator.upper():
|
|
139 |
# Use the corrected aggregator for further processing
|
140 |
summary_type = aggregator.lower()
|
141 |
|
142 |
-
#
|
143 |
-
if
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
elif summary_type == '
|
148 |
-
|
149 |
-
elif summary_type == '
|
150 |
-
|
151 |
-
elif summary_type == '
|
152 |
-
|
153 |
-
elif summary_type == '
|
154 |
-
|
155 |
-
elif summary_type == '
|
156 |
-
|
|
|
|
|
|
|
|
|
157 |
else:
|
158 |
-
numeric_value =
|
159 |
|
160 |
# Construct a natural language response
|
161 |
if summary_type == 'sum':
|
@@ -186,8 +123,3 @@ st.success(f"""
|
|
186 |
Additional Context:
|
187 |
• Query Asked: "{question}"
|
188 |
""")
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
1 |
import os
|
|
|
|
|
2 |
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
+
from tapas import tqa, t5_tokenizer, t5_model
|
5 |
+
|
6 |
+
# Assuming 'df' is the DataFrame you are using and has numeric columns
|
7 |
+
df_numeric = df.select_dtypes(include='number')
|
8 |
+
|
9 |
+
# Ensure that `column_name` is defined and valid
|
10 |
+
column_name = None # Make sure this is defined later from TAPAS response
|
11 |
+
|
12 |
+
# User input for the question
|
13 |
+
question = st.text_input('Type your question')
|
14 |
+
|
15 |
+
# Process the answer using TAPAS and T5
|
16 |
+
with st.spinner():
|
17 |
+
if st.button('Answer'):
|
18 |
+
try:
|
19 |
+
# Get the raw answer from TAPAS
|
20 |
+
raw_answer = tqa(table=df, query=question, truncation=True)
|
21 |
+
|
22 |
+
st.markdown("<p style='font-family:sans-serif;font-size: 0.9rem;'> Raw Result From TAPAS: </p>", unsafe_allow_html=True)
|
23 |
+
st.success(raw_answer)
|
24 |
+
|
25 |
+
# Extract relevant information from the TAPAS result
|
26 |
+
answer = raw_answer['answer']
|
27 |
+
aggregator = raw_answer.get('aggregator', '')
|
28 |
+
coordinates = raw_answer.get('coordinates', [])
|
29 |
+
cells = raw_answer.get('cells', [])
|
30 |
+
|
31 |
+
# Extract the column name based on coordinates
|
32 |
+
if coordinates:
|
33 |
+
row, col = coordinates[0] # assuming single cell result
|
34 |
+
column_name = df.columns[col] # Get the column name
|
35 |
+
|
36 |
+
# Construct a base sentence replacing 'SUM' with the query term
|
37 |
+
base_sentence = f"The {question.lower()} of the selected data is {answer}."
|
38 |
+
if coordinates and cells:
|
39 |
+
rows_info = [f"Row {coordinate[0] + 1}, Column '{df.columns[coordinate[1]]}' with value {cell}"
|
40 |
+
for coordinate, cell in zip(coordinates, cells)]
|
41 |
+
rows_description = " and ".join(rows_info)
|
42 |
+
base_sentence += f" This includes the following data: {rows_description}."
|
43 |
+
|
44 |
+
# Generate a fluent response using the T5 model, rephrasing the base sentence
|
45 |
+
input_text = f"Given the question: '{question}', generate a more human-readable response: {base_sentence}"
|
46 |
+
|
47 |
+
# Tokenize the input and generate a fluent response using T5
|
48 |
+
inputs = t5_tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
49 |
+
summary_ids = t5_model.generate(inputs, max_length=150, num_beams=4, early_stopping=True)
|
50 |
+
|
51 |
+
# Decode the generated text
|
52 |
+
generated_text = t5_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
53 |
+
|
54 |
+
# Display the final generated response
|
55 |
+
st.markdown("<p style='font-family:sans-serif;font-size: 0.9rem;'> Final Generated Response with LLM: </p>", unsafe_allow_html=True)
|
56 |
+
st.success(generated_text)
|
57 |
+
|
58 |
+
except Exception as e:
|
59 |
+
st.warning("Please retype your question and make sure to use the column name and cell value correctly.")
|
60 |
+
|
61 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
# Manually fix the aggregator if it returns an incorrect one
|
63 |
if 'MEDIAN' in question.upper() and 'AVERAGE' in aggregator.upper():
|
64 |
aggregator = 'MEDIAN'
|
|
|
72 |
# Use the corrected aggregator for further processing
|
73 |
summary_type = aggregator.lower()
|
74 |
|
75 |
+
# Check if `column_name` is valid before proceeding
|
76 |
+
if column_name and column_name in df_numeric.columns:
|
77 |
+
# Now, calculate the correct value using pandas based on the corrected aggregator
|
78 |
+
if summary_type == 'sum':
|
79 |
+
numeric_value = df_numeric[column_name].sum()
|
80 |
+
elif summary_type == 'max':
|
81 |
+
numeric_value = df_numeric[column_name].max()
|
82 |
+
elif summary_type == 'min':
|
83 |
+
numeric_value = df_numeric[column_name].min()
|
84 |
+
elif summary_type == 'average':
|
85 |
+
numeric_value = df_numeric[column_name].mean()
|
86 |
+
elif summary_type == 'count':
|
87 |
+
numeric_value = df_numeric[column_name].count()
|
88 |
+
elif summary_type == 'median':
|
89 |
+
numeric_value = df_numeric[column_name].median()
|
90 |
+
elif summary_type == 'std_dev':
|
91 |
+
numeric_value = df_numeric[column_name].std()
|
92 |
+
else:
|
93 |
+
numeric_value = answer # Fallback if something went wrong
|
94 |
else:
|
95 |
+
numeric_value = "Invalid column"
|
96 |
|
97 |
# Construct a natural language response
|
98 |
if summary_type == 'sum':
|
|
|
123 |
Additional Context:
|
124 |
• Query Asked: "{question}"
|
125 |
""")
|
|
|
|
|
|
|
|
|
|