Spaces:
Build error
Build error
SHAMIL SHAHBAZ AWAN commited on
Update app.py
Browse files3 unmodified lines456789101112131426 unmodified lines4142434445464748495051525354555657585960616263646566676869707172735 unmodified lines79808182838485868788899091929394954 unmodified lines1001011021031041051061071081091101111121133 unmodified linesimport seaborn as snsimport osfrom io import StringIOimport openaifrom groq import Groq# Groq API key from secretsGROQ_API_KEY = os.getenv("HUGGINGFACE_KEY")# Function to load the uploaded file (CSV or Excel)def load_file(uploaded_file):26 unmodified linesst.pyplot(fig)else:st.error("The dataset must contain 'country' and 'gross_sales' columns.")else:st.error("Unsupported graph type. Try asking for a bar chart.")except Exception as e:st.error(f"Error generating graph: {e}")# Function to query the Groq model (Groq API)def query_groq_model(prompt):"""Send a query to the Groq model and get a response."""try:# Initialize Groq clientclient = Groq(api_key=GROQ_API_KEY)# Query Groq model for responsechat_completion = client.chat.completions.create(messages=[{"role": "user", "content": prompt}],model="llama-3.1-8b-instant", # Replace with your model)return chat_completion.choices[0].message.contentexcept Exception as e:st.error(f"Error querying Groq model: {e}")return None# Streamlit App Interfacedef main():st.set_page_config(page_title="Data Augmentation and Visualization", page_icon="📊", layout="wide")# Set background imagest.markdown("""5 unmodified lines</style>""", unsafe_allow_html=True)st.title("Data Augmentation and Visualization with Groq API")st.markdown("Created by: Shamil Shahbaz", unsafe_allow_html=True)# File upload sectionuploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])if uploaded_file is not None:# Load and display datadata = load_file(uploaded_file)if data is not None:st.write("Dataset preview:", data.head())4 unmodified lines# Generate the graph based on the querygenerate_graph(data, query)# User input for Groq model querymodel_query = st.text_input("Ask Groq model a question:")if model_query:# Query the Groq model and display responseresponse = query_groq_model(model_query)if response:st.write("Groq Model Response:", response)if __name__ == "__main__":main()3 unmodified lines45678926 unmodified lines36373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081825 unmodified lines8889909192939495969798991001011021031044 unmodified lines1091101111121133 unmodified linesimport seaborn as snsimport osfrom io import StringIO# Function to load the uploaded file (CSV or Excel)def load_file(uploaded_file):26 unmodified linesst.pyplot(fig)else:st.error("The dataset must contain 'country' and 'gross_sales' columns.")elif "line" in query.lower() and "sales trend" in query.lower():# Line chart for sales trend over timeif 'date' in data.columns and 'sales' in data.columns:data['date'] = pd.to_datetime(data['date'])sales_trend = data.groupby('date')['sales'].sum().reset_index()sns.lineplot(x='date', y='sales', data=sales_trend, ax=ax)ax.set_title("Sales Trend Over Time")st.pyplot(fig)else:st.error("The dataset must contain 'date' and 'sales' columns.")elif "scatter" in query.lower() and "relationship" in query.lower():# Scatter plot for relationshipscolumns = query.lower().split("between")[-1].strip().split("and")x_col = columns[0].strip()y_col = columns[1].strip()if x_col in data.columns and y_col in data.columns:sns.scatterplot(x=x_col, y=y_col, data=data, ax=ax)ax.set_title(f"Scatter Plot: {x_col} vs {y_col}")st.pyplot(fig)else:st.error(f"The dataset must contain '{x_col}' and '{y_col}' columns.")elif "histogram" in query.lower():# Histogram for a specified columncolumn = query.lower().split("for")[-1].strip()if column in data.columns:sns.histplot(data[column], bins=20, kde=True, ax=ax, color='green')ax.set_title(f"Histogram of {column}")st.pyplot(fig)else:st.error(f"The dataset must contain the column '{column}'.")else:st.error("Unsupported graph type. Try asking for a bar chart, line chart, scatter plot, or histogram.")except Exception as e:st.error(f"Error generating graph: {e}")# Streamlit App Interfacedef main():st.set_page_config(page_title="Data Visualization App", page_icon="📊", layout="wide")# Set background imagest.markdown("""5 unmodified lines</style>""", unsafe_allow_html=True)st.title("Data Visualization App")st.markdown("Created by: Shamil Shahbaz", unsafe_allow_html=True)# File upload sectionuploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])if uploaded_file is not None:# Load and display datadata = load_file(uploaded_file)if data is not None:st.write("Dataset preview:", data.head())4 unmodified lines# Generate the graph based on the querygenerate_graph(data, query)if __name__ == "__main__":main()
app.py+40-40