Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| try: | |
| from groqflow.groqmodel import GroqModel | |
| except ImportError as e: | |
| st.error(f"Failed to import GroqModel. Please ensure all dependencies are installed correctly. Error: {e}") | |
| # 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") | |
| # Load Groq API key from secrets | |
| try: | |
| groq_api_key = st.secrets["HUGGINGFACE_KEY"] | |
| groq_model = GroqModel("llama3-8b-8192", api_key=groq_api_key) | |
| except KeyError: | |
| st.error("API key not found in secrets. Please configure your `HUGGINGFACE_KEY` in Streamlit secrets.") | |
| except Exception as e: | |
| st.error(f"Error initializing GroqModel: {e}") | |
| 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 | |
| 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(): | |
| sns.heatmap(data.corr(), annot=True, cmap="coolwarm", ax=ax) | |
| st.pyplot(fig) | |
| elif "histogram" in query.lower(): | |
| column = st.selectbox("Select a column for the histogram", data.columns) | |
| sns.histplot(data[column], kde=True, ax=ax) | |
| st.pyplot(fig) | |
| else: | |
| st.error("Unsupported graph type. Try asking for a correlation matrix or histogram.") | |
| except Exception as e: | |
| st.error(f"Error generating graph: {e}") | |
| def handle_query(data, query): | |
| """Handle user query using Groq API.""" | |
| try: | |
| if not groq_model: | |
| st.error("GroqModel is not initialized. Check for errors in setup.") | |
| return | |
| prompt = f"Given the dataset: {data.to_dict(orient='records')}, answer the following: {query}" | |
| response = groq_model.generate(prompt) | |
| st.write("Response:", response) | |
| except Exception as e: | |
| st.error(f"Error in LLM 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) | |
| elif "predict" in query.lower(): | |
| st.write("Prediction functionality is in progress.") | |
| else: | |
| handle_query(data, query) | |
| footer = """ | |
| <div style='text-align: left; padding: 10px;'> | |
| <footer>Created by: Shamil Shahbaz</footer> | |
| </div> | |
| """ | |
| st.markdown(footer, unsafe_allow_html=True) | |