Spaces:
Sleeping
Sleeping
SHAMIL SHAHBAZ AWAN
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,63 +2,27 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import seaborn as sns
|
|
|
|
|
|
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
import openpyxl
|
| 10 |
-
except ImportError:
|
| 11 |
-
st.error("openpyxl module is required to read Excel files. Attempting to install...")
|
| 12 |
-
os.system('pip install openpyxl')
|
| 13 |
-
st.success("openpyxl installed successfully. Please restart the app.")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
try:
|
| 17 |
-
from groq import Groq
|
| 18 |
-
except ImportError:
|
| 19 |
-
st.error("The 'groq' library is required but not installed. Please ensure it's included in requirements.txt.")
|
| 20 |
-
raise
|
| 21 |
-
|
| 22 |
-
# Configure page
|
| 23 |
-
st.set_page_config(page_title="Data Augmentation App", layout="wide")
|
| 24 |
-
st.markdown(
|
| 25 |
-
f"""
|
| 26 |
-
<style>
|
| 27 |
-
.reportview-container {{
|
| 28 |
-
background: url("https://cdn.pixabay.com/photo/2016/06/02/02/33/triangles-1430105_1280.png");
|
| 29 |
-
background-size: cover;
|
| 30 |
-
}}
|
| 31 |
-
footer {{
|
| 32 |
-
text-align: left;
|
| 33 |
-
}}
|
| 34 |
-
</style>
|
| 35 |
-
""",
|
| 36 |
-
unsafe_allow_html=True,
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
st.title("Data Augmentation and Analysis App")
|
| 40 |
-
st.sidebar.title("Upload Your File")
|
| 41 |
-
st.sidebar.markdown("Supported formats: CSV, Excel")
|
| 42 |
-
|
| 43 |
-
# Fetch Groq API key from Streamlit secrets
|
| 44 |
-
groq_api_key = st.secrets.get("HUGGINGFACE_KEY") # Fetch Groq API key stored in secrets.json
|
| 45 |
-
if not groq_api_key:
|
| 46 |
-
st.error("Groq API key not found in secrets.")
|
| 47 |
-
else:
|
| 48 |
-
st.success("Groq API Key Loaded Successfully!")
|
| 49 |
-
|
| 50 |
-
# Initialize Groq client
|
| 51 |
-
client = Groq(api_key=groq_api_key)
|
| 52 |
-
|
| 53 |
-
# Function to load data from file (CSV/Excel)
|
| 54 |
def load_file(uploaded_file):
|
| 55 |
-
"""Load
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return None
|
| 63 |
|
| 64 |
# Function to generate graph based on user query
|
|
@@ -80,7 +44,7 @@ def generate_graph(data, query):
|
|
| 80 |
# Bar chart for countries and units sold
|
| 81 |
if 'country' in data.columns and 'units sold' in data.columns:
|
| 82 |
country_data = data[['country', 'units sold']].groupby('country').sum().reset_index()
|
| 83 |
-
sns.barplot(x='country', y='units sold', data=country_data, ax=ax)
|
| 84 |
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')
|
| 85 |
st.pyplot(fig)
|
| 86 |
else:
|
|
@@ -90,44 +54,68 @@ def generate_graph(data, query):
|
|
| 90 |
except Exception as e:
|
| 91 |
st.error(f"Error generating graph: {e}")
|
| 92 |
|
| 93 |
-
# Function to
|
| 94 |
-
def
|
| 95 |
-
"""
|
| 96 |
try:
|
| 97 |
-
#
|
|
|
|
|
|
|
|
|
|
| 98 |
chat_completion = client.chat.completions.create(
|
| 99 |
-
messages=[{"role": "user", "content":
|
| 100 |
-
model="
|
| 101 |
)
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
st.write("Response from Groq Model:", chat_completion.choices[0].message.content)
|
| 105 |
-
|
| 106 |
except Exception as e:
|
| 107 |
-
st.error(f"Error
|
|
|
|
| 108 |
|
| 109 |
-
#
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
if "table" in query.lower():
|
| 120 |
-
st.write("Table Preview")
|
| 121 |
-
st.write(data)
|
| 122 |
-
elif "graph" in query.lower():
|
| 123 |
generate_graph(data, query)
|
| 124 |
-
else:
|
| 125 |
-
handle_query(query) # Use Groq model for query processing
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import seaborn as sns
|
| 5 |
+
from io import StringIO
|
| 6 |
+
import openai
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# Groq API key from secrets
|
| 10 |
+
GROQ_API_KEY = os.getenv("HUGGINGFACE_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Function to load the uploaded file (CSV or Excel)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def load_file(uploaded_file):
|
| 14 |
+
"""Load data from an uploaded file."""
|
| 15 |
+
try:
|
| 16 |
+
if uploaded_file.type == "text/csv":
|
| 17 |
+
data = pd.read_csv(uploaded_file)
|
| 18 |
+
elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
| 19 |
+
data = pd.read_excel(uploaded_file)
|
| 20 |
+
else:
|
| 21 |
+
st.error("Unsupported file type.")
|
| 22 |
+
return None
|
| 23 |
+
return data
|
| 24 |
+
except Exception as e:
|
| 25 |
+
st.error(f"Error loading file: {e}")
|
| 26 |
return None
|
| 27 |
|
| 28 |
# Function to generate graph based on user query
|
|
|
|
| 44 |
# Bar chart for countries and units sold
|
| 45 |
if 'country' in data.columns and 'units sold' in data.columns:
|
| 46 |
country_data = data[['country', 'units sold']].groupby('country').sum().reset_index()
|
| 47 |
+
sns.barplot(x='country', y='units sold', data=country_data, ax=ax, color='skyblue')
|
| 48 |
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')
|
| 49 |
st.pyplot(fig)
|
| 50 |
else:
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
st.error(f"Error generating graph: {e}")
|
| 56 |
|
| 57 |
+
# Function to query the Groq model (Groq API)
|
| 58 |
+
def query_groq_model(prompt):
|
| 59 |
+
"""Send a query to the Groq model and get a response."""
|
| 60 |
try:
|
| 61 |
+
# Initialize Groq client
|
| 62 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 63 |
+
|
| 64 |
+
# Query Groq model for response
|
| 65 |
chat_completion = client.chat.completions.create(
|
| 66 |
+
messages=[{"role": "user", "content": prompt}],
|
| 67 |
+
model="llama-3.1-8b-instant", # Replace with your model
|
| 68 |
)
|
| 69 |
+
|
| 70 |
+
return chat_completion.choices[0].message.content
|
|
|
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
+
st.error(f"Error querying Groq model: {e}")
|
| 73 |
+
return None
|
| 74 |
|
| 75 |
+
# Streamlit App Interface
|
| 76 |
+
def main():
|
| 77 |
+
st.set_page_config(page_title="Data Augmentation and Visualization", page_icon="📊", layout="wide")
|
| 78 |
+
|
| 79 |
+
# Set background image
|
| 80 |
+
st.markdown(
|
| 81 |
+
"""
|
| 82 |
+
<style>
|
| 83 |
+
.stApp {
|
| 84 |
+
background-image: url('https://cdn.pixabay.com/photo/2016/06/02/02/33/triangles-1430105_1280.png');
|
| 85 |
+
background-size: cover;
|
| 86 |
+
}
|
| 87 |
+
</style>
|
| 88 |
+
""", unsafe_allow_html=True
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
st.title("Data Augmentation and Visualization with Groq API")
|
| 92 |
+
st.markdown("Created by: Shamil Shahbaz", unsafe_allow_html=True)
|
| 93 |
+
|
| 94 |
+
# File upload section
|
| 95 |
+
uploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])
|
| 96 |
+
|
| 97 |
+
if uploaded_file is not None:
|
| 98 |
+
# Load and display data
|
| 99 |
+
data = load_file(uploaded_file)
|
| 100 |
+
|
| 101 |
+
if data is not None:
|
| 102 |
+
st.write("Dataset preview:", data.head())
|
| 103 |
+
|
| 104 |
+
# User input for graph generation
|
| 105 |
+
query = st.text_input("Enter your query (e.g., 'Generate a bar chart for countries and units sold')")
|
| 106 |
|
| 107 |
+
if query:
|
| 108 |
+
# Generate the graph based on the query
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
generate_graph(data, query)
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
# User input for Groq model query
|
| 112 |
+
model_query = st.text_input("Ask Groq model a question:")
|
| 113 |
+
|
| 114 |
+
if model_query:
|
| 115 |
+
# Query the Groq model and display response
|
| 116 |
+
response = query_groq_model(model_query)
|
| 117 |
+
if response:
|
| 118 |
+
st.write("Groq Model Response:", response)
|
| 119 |
+
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
main()
|