Spaces:
Sleeping
Sleeping
SHAMIL SHAHBAZ AWAN
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import numpy as np
|
|
| 7 |
|
| 8 |
# Load a lightweight NLP model for query understanding
|
| 9 |
nlp = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
|
|
|
| 10 |
|
| 11 |
# Function to load the uploaded file (CSV or Excel)
|
| 12 |
def load_file(uploaded_file):
|
|
@@ -32,6 +33,22 @@ def classify_query(query, candidate_labels):
|
|
| 32 |
return results['labels'][0]
|
| 33 |
return None
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Function to generate a graph based on user query
|
| 36 |
def generate_graph(data, query):
|
| 37 |
"""Generate a graph based on user query."""
|
|
@@ -99,6 +116,12 @@ def generate_graph(data, query):
|
|
| 99 |
else:
|
| 100 |
response += " Could not find relevant 'department' or 'sales' columns in the dataset."
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
elif query_type == "general question":
|
| 103 |
# Handle general questions
|
| 104 |
response = "Analyzing the data for your general question."
|
|
@@ -159,7 +182,7 @@ def main():
|
|
| 159 |
st.write("Dataset preview:", data.head())
|
| 160 |
|
| 161 |
# User input for query
|
| 162 |
-
user_query = st.text_input("Enter your query (e.g., 'Generate a bar chart for countries and sales', or '
|
| 163 |
|
| 164 |
if user_query:
|
| 165 |
# Generate the graph based on the query or handle general questions
|
|
|
|
| 7 |
|
| 8 |
# Load a lightweight NLP model for query understanding
|
| 9 |
nlp = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 10 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") # Model for summarization
|
| 11 |
|
| 12 |
# Function to load the uploaded file (CSV or Excel)
|
| 13 |
def load_file(uploaded_file):
|
|
|
|
| 33 |
return results['labels'][0]
|
| 34 |
return None
|
| 35 |
|
| 36 |
+
# Function to summarize document content
|
| 37 |
+
def summarize_document(data):
|
| 38 |
+
"""Generate a summary for the document."""
|
| 39 |
+
if data is None:
|
| 40 |
+
return "No data loaded."
|
| 41 |
+
|
| 42 |
+
# Convert the dataframe to a string representation for summarization
|
| 43 |
+
document_text = data.to_string(index=False)
|
| 44 |
+
|
| 45 |
+
# Summarize the document text using the summarizer
|
| 46 |
+
try:
|
| 47 |
+
summary = summarizer(document_text, max_length=150, min_length=50, do_sample=False)
|
| 48 |
+
return summary[0]['summary_text']
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Error summarizing document: {e}"
|
| 51 |
+
|
| 52 |
# Function to generate a graph based on user query
|
| 53 |
def generate_graph(data, query):
|
| 54 |
"""Generate a graph based on user query."""
|
|
|
|
| 116 |
else:
|
| 117 |
response += " Could not find relevant 'department' or 'sales' columns in the dataset."
|
| 118 |
|
| 119 |
+
elif query.lower() == "what is this document":
|
| 120 |
+
# Provide a document summary
|
| 121 |
+
response = "Analyzing the document content."
|
| 122 |
+
document_summary = summarize_document(data)
|
| 123 |
+
response += f" Here is a summary of the document: {document_summary}"
|
| 124 |
+
|
| 125 |
elif query_type == "general question":
|
| 126 |
# Handle general questions
|
| 127 |
response = "Analyzing the data for your general question."
|
|
|
|
| 182 |
st.write("Dataset preview:", data.head())
|
| 183 |
|
| 184 |
# User input for query
|
| 185 |
+
user_query = st.text_input("Enter your query (e.g., 'Generate a bar chart for countries and sales', or 'What is this document?')")
|
| 186 |
|
| 187 |
if user_query:
|
| 188 |
# Generate the graph based on the query or handle general questions
|