data-analysis / app.py
Rajendra Prasad
Add requirements.txt file
8430d1f
Raw History Blame Contribute Delete
4.03 kB
import streamlit as st
import pandas as pd
import google.generativeai as genai
import os
from dotenv import load_dotenv
load_dotenv() # Load environment variables from .env file if it exists
# Function to get Gemini API key (you can set it in Streamlit secrets or environment variables)
def get_gemini_api_key():
api_key = os.getenv("GOOGLE_API_KEY") # Try environment variable first
if not api_key:
api_key = st.secrets.get("GOOGLE_API_KEY") # Then try Streamlit secrets
return api_key
# Function to analyze data with Gemini
def analyze_csv_with_gemini(csv_file, analysis_request):
"""
Analyzes CSV data using the Gemini Pro model.
Args:
csv_file: Uploaded CSV file object.
analysis_request: User's text request for analysis.
Returns:
str: Gemini's analysis response.
"""
try:
df = pd.read_csv(csv_file)
csv_string = df.to_csv(index=False) # Convert DataFrame to string for Gemini prompt
api_key = get_gemini_api_key()
if not api_key:
st.error("Gemini API key not found. Please set it as GOOGLE_API_KEY environment variable or in Streamlit secrets.")
return None
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-2.0-flash-thinking-exp-01-21')
prompt_content = f"""
You are a data analysis expert. Analyze the following CSV data and provide insights based on the user's request.
CSV Data:
```csv
{csv_string}
```
Analysis Request: {analysis_request}
Please provide your analysis in a clear, concise, and informative way.
Focus on identifying key trends, patterns, and insights from the data that are relevant to the request.
If possible, suggest potential next steps or questions for further investigation based on your analysis.
"""
response = model.generate_content(prompt_content)
return response.text
except pd.errors.ParserError:
return "Error: Could not parse the CSV file. Please ensure it is a valid CSV format."
except Exception as e:
return f"An error occurred during analysis: {e}"
# Streamlit App
st.title("CSV Data Analyzer with Gemini")
st.write("Upload your CSV file and enter your analysis request below.")
uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
analysis_request = st.text_area("Enter your analysis request (e.g., 'Summarize key trends', 'Identify correlations', 'Find outliers', 'Give me top 5 insights'):", height=150)
if st.button("Analyze"):
if uploaded_file is not None and analysis_request:
st.info("Analyzing data with Gemini... Please wait.")
gemini_response = analyze_csv_with_gemini(uploaded_file, analysis_request)
if gemini_response:
st.subheader("Gemini Analysis Results:")
st.markdown(gemini_response)
else:
st.error("Failed to get analysis from Gemini. Please check API key and try again.")
elif uploaded_file is None:
st.warning("Please upload a CSV file.")
elif not analysis_request:
st.warning("Please enter your analysis request.")
# Instructions and API Key Setup
st.sidebar.header("Instructions")
st.sidebar.markdown(
"""
1. **Upload CSV File:** Use the file uploader to upload your CSV file.
2. **Enter Analysis Request:** Type your specific data analysis request in the text area. Be clear about what insights you are looking for.
3. **Click 'Analyze':** Press the 'Analyze' button to start the analysis using Gemini.
4. **View Results:** The Gemini analysis will be displayed below.
**API Key Setup:**
**Important Notes:**
* This application uses the gemini-2.0-flash-thinking-exp-01-21 model.
* The performance depends on the complexity of your data and analysis request.
* For very large CSV files, consider sampling or preprocessing the data before analysis for faster results on CPU-only machines.
"""
)