Data-Excel / app.py
SHAMIL SHAHBAZ AWAN
Update app.py
9bcc73a verified
raw
history blame
4.75 kB
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
# Try importing openpyxl, and install if not present
try:
import openpyxl
except ImportError:
st.error("openpyxl module is required to read Excel files. Attempting to install...")
os.system('pip install openpyxl')
st.success("openpyxl installed successfully. Please restart the app.")
# Import the Groq SDK (after installing it)
try:
from groq import Groq
except ImportError:
st.error("The 'groq' library is required but not installed. Please ensure it's included in requirements.txt.")
raise
# Configure page
st.set_page_config(page_title="Data Augmentation App", layout="wide")
st.markdown(
f"""
<style>
.reportview-container {{
background: url("https://cdn.pixabay.com/photo/2016/06/02/02/33/triangles-1430105_1280.png");
background-size: cover;
}}
footer {{
text-align: left;
}}
</style>
""",
unsafe_allow_html=True,
)
st.title("Data Augmentation and Analysis App")
st.sidebar.title("Upload Your File")
st.sidebar.markdown("Supported formats: CSV, Excel")
# Fetch Groq API key from Streamlit secrets
groq_api_key = st.secrets.get("HUGGINGFACE_KEY") # Fetch Groq API key stored in secrets.json
if not groq_api_key:
st.error("Groq API key not found in secrets.")
else:
st.success("Groq API Key Loaded Successfully!")
# Initialize Groq client
client = Groq(api_key=groq_api_key)
# Function to load data from file (CSV/Excel)
def load_file(uploaded_file):
"""Load the uploaded file."""
if uploaded_file.name.endswith('.csv'):
return pd.read_csv(uploaded_file)
elif uploaded_file.name.endswith('.xlsx'):
return pd.read_excel(uploaded_file)
else:
st.error("Unsupported file format. Please upload a CSV or Excel file.")
return None
# Function to generate graph based on user query
def generate_graph(data, query):
"""Generate a graph based on user query."""
try:
fig, ax = plt.subplots(figsize=(10, 6))
if "correlation" in query.lower():
# Correlation matrix
sns.heatmap(data.corr(), annot=True, cmap="coolwarm", ax=ax)
st.pyplot(fig)
elif "histogram" in query.lower():
# Histogram
column = st.selectbox("Select a column for the histogram", data.columns)
sns.histplot(data[column], kde=True, ax=ax)
st.pyplot(fig)
elif "bar" in query.lower() and "country" in query.lower():
# Bar chart for countries and units sold
if 'country' in data.columns and 'units sold' in data.columns:
country_data = data[['country', 'units sold']].groupby('country').sum().reset_index()
sns.barplot(x='country', y='units sold', data=country_data, ax=ax)
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')
st.pyplot(fig)
else:
st.error("The dataset must contain 'country' and 'units sold' columns.")
else:
st.error("Unsupported graph type. Try asking for a correlation matrix, histogram, or bar chart.")
except Exception as e:
st.error(f"Error generating graph: {e}")
# Function to handle user queries using the Groq model
def handle_query(query):
"""Handle user query using the Groq model."""
try:
# Send user query to Groq model using Groq SDK
chat_completion = client.chat.completions.create(
messages=[{"role": "user", "content": query}],
model="llama3-8b-8192", # Model name as per your Groq model
)
# Display the response
st.write("Response from Groq Model:", chat_completion.choices[0].message.content)
except Exception as e:
st.error(f"Error in Groq processing: {e}")
# Main App
uploaded_file = st.sidebar.file_uploader("Upload your file here", type=["csv", "xlsx"])
if uploaded_file:
data = load_file(uploaded_file)
if data is not None:
st.write("Dataset Preview")
st.dataframe(data)
query = st.text_area("Ask your question about the dataset")
if query:
if "table" in query.lower():
st.write("Table Preview")
st.write(data)
elif "graph" in query.lower():
generate_graph(data, query)
else:
handle_query(query) # Use Groq model for query processing
# Footer
footer = """
<div style='text-align: left; padding: 10px;'>
<footer>Created by: Shamil Shahbaz</footer>
</div>
"""
st.markdown(footer, unsafe_allow_html=True)