Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import io
|
|
| 10 |
import base64
|
| 11 |
import requests
|
| 12 |
import google.generativeai as genai
|
|
|
|
| 13 |
|
| 14 |
# API Endpoint and payload
|
| 15 |
API_URL = "https://irisplus.elixir.co.zw/public/api/profile/reporting/stock-card/genericReports"
|
|
@@ -38,7 +39,11 @@ def fetch_data():
|
|
| 38 |
st.error(f"Error fetching data: {response.status_code} - {response.text}")
|
| 39 |
return None
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
genai.configure(api_key=gemini_api_key)
|
| 44 |
|
|
@@ -94,29 +99,9 @@ class StreamLitResponse(ResponseParser):
|
|
| 94 |
'value': str(result['value'])
|
| 95 |
}
|
| 96 |
|
| 97 |
-
GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
|
| 98 |
-
if not GOOGLE_API_KEY:
|
| 99 |
-
st.error("GOOGLE_API_KEY environment variable not set.")
|
| 100 |
-
st.stop()
|
| 101 |
-
|
| 102 |
-
gemini_api_key = os.environ['GOOGLE_API_KEY']
|
| 103 |
-
|
| 104 |
-
genai.configure(api_key=gemini_api_key)
|
| 105 |
-
|
| 106 |
-
generation_config = {
|
| 107 |
-
"temperature": 0.2,
|
| 108 |
-
"top_p": 0.95,
|
| 109 |
-
"max_output_tokens": 5000,
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
model = genai.GenerativeModel(
|
| 113 |
-
model_name="gemini-2.0-flash-thinking-exp",
|
| 114 |
-
generation_config=generation_config,
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
def generateResponse(prompt, df):
|
| 118 |
"""Generate response using PandasAI with SmartDataframe"""
|
| 119 |
-
llm = GoogleGemini(api_key=
|
| 120 |
pandas_agent = SmartDataframe(df, config={
|
| 121 |
"llm": llm,
|
| 122 |
"response_parser": StreamLitResponse
|
|
@@ -188,6 +173,21 @@ def handle_userinput(question, df):
|
|
| 188 |
except Exception as e:
|
| 189 |
st.error(f"Error processing input: {e}")
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
def main():
|
| 192 |
st.set_page_config(page_title="AI Chat with Your Data", page_icon="📊")
|
| 193 |
|
|
@@ -238,12 +238,25 @@ def main():
|
|
| 238 |
st.dataframe(filtered_df.head())
|
| 239 |
with st.spinner("Generating Report, Please Wait...."):
|
| 240 |
prompt = f"""
|
| 241 |
-
"You are an expert business analyst. Analyze the following data and generate a comprehensive and insightful business report
|
| 242 |
-
|
| 243 |
"""
|
| 244 |
response = model.generate_content(prompt)
|
| 245 |
# Display the report text; adjust according to your response format.
|
| 246 |
report = response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
st.markdown(report)
|
| 248 |
else:
|
| 249 |
st.error("No data available for reports.")
|
|
|
|
| 10 |
import base64
|
| 11 |
import requests
|
| 12 |
import google.generativeai as genai
|
| 13 |
+
from fpdf import FPDF # New import for PDF generation
|
| 14 |
|
| 15 |
# API Endpoint and payload
|
| 16 |
API_URL = "https://irisplus.elixir.co.zw/public/api/profile/reporting/stock-card/genericReports"
|
|
|
|
| 39 |
st.error(f"Error fetching data: {response.status_code} - {response.text}")
|
| 40 |
return None
|
| 41 |
|
| 42 |
+
# Configure Gemini API
|
| 43 |
+
gemini_api_key = os.environ.get('GOOGLE_API_KEY')
|
| 44 |
+
if not gemini_api_key:
|
| 45 |
+
st.error("GOOGLE_API_KEY environment variable not set.")
|
| 46 |
+
st.stop()
|
| 47 |
|
| 48 |
genai.configure(api_key=gemini_api_key)
|
| 49 |
|
|
|
|
| 99 |
'value': str(result['value'])
|
| 100 |
}
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
def generateResponse(prompt, df):
|
| 103 |
"""Generate response using PandasAI with SmartDataframe"""
|
| 104 |
+
llm = GoogleGemini(api_key=gemini_api_key)
|
| 105 |
pandas_agent = SmartDataframe(df, config={
|
| 106 |
"llm": llm,
|
| 107 |
"response_parser": StreamLitResponse
|
|
|
|
| 173 |
except Exception as e:
|
| 174 |
st.error(f"Error processing input: {e}")
|
| 175 |
|
| 176 |
+
def generate_pdf(report_text):
|
| 177 |
+
"""
|
| 178 |
+
Generate a PDF in memory from the report text.
|
| 179 |
+
This function uses FPDF to create a PDF and returns its bytes.
|
| 180 |
+
"""
|
| 181 |
+
pdf = FPDF()
|
| 182 |
+
pdf.add_page()
|
| 183 |
+
pdf.set_font("Arial", size=12)
|
| 184 |
+
# Split report text into lines for writing
|
| 185 |
+
for line in report_text.split('\n'):
|
| 186 |
+
pdf.multi_cell(0, 10, line)
|
| 187 |
+
# Output PDF to a byte string
|
| 188 |
+
pdf_bytes = pdf.output(dest="S").encode("latin1")
|
| 189 |
+
return pdf_bytes
|
| 190 |
+
|
| 191 |
def main():
|
| 192 |
st.set_page_config(page_title="AI Chat with Your Data", page_icon="📊")
|
| 193 |
|
|
|
|
| 238 |
st.dataframe(filtered_df.head())
|
| 239 |
with st.spinner("Generating Report, Please Wait...."):
|
| 240 |
prompt = f"""
|
| 241 |
+
"You are an expert business analyst. Analyze the following data and generate a comprehensive and insightful business report
|
| 242 |
+
including key performance indicators and recommendations.\n\nData:\n" + {str(filtered_df.to_json(orient='records'))}
|
| 243 |
"""
|
| 244 |
response = model.generate_content(prompt)
|
| 245 |
# Display the report text; adjust according to your response format.
|
| 246 |
report = response.text
|
| 247 |
+
|
| 248 |
+
# Generate the PDF bytes from the report text
|
| 249 |
+
pdf_bytes = generate_pdf(report)
|
| 250 |
+
|
| 251 |
+
# Display the download button above the report
|
| 252 |
+
st.download_button(
|
| 253 |
+
label="Download Report as PDF",
|
| 254 |
+
data=pdf_bytes,
|
| 255 |
+
file_name="report.pdf",
|
| 256 |
+
mime="application/pdf"
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
# Display the report content
|
| 260 |
st.markdown(report)
|
| 261 |
else:
|
| 262 |
st.error("No data available for reports.")
|