Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,10 @@ import gradio as gr
|
|
| 2 |
import PyPDF2
|
| 3 |
import google.generativeai as genai
|
| 4 |
import re
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 8 |
|
| 9 |
def extract_text_from_pdf(file):
|
|
@@ -27,26 +29,27 @@ def analyze_financial_data(file):
|
|
| 27 |
text = extract_text_from_pdf(file)
|
| 28 |
|
| 29 |
if not text:
|
| 30 |
-
return (
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
)
|
| 34 |
|
| 35 |
prompt = f"""
|
| 36 |
-
Analyze
|
|
|
|
| 37 |
**Financial Insights**
|
| 38 |
-
**- Monthly Income & Expenses:**
|
| 39 |
-
**- Unnecessary Expense Categories:**
|
| 40 |
-
**- Estimated Savings %:**
|
| 41 |
-
**- Spending Trends:**
|
| 42 |
-
**- Category-wise Expense Breakdown (Partial):**
|
| 43 |
-
**- Cost Control Suggestions:**
|
|
|
|
| 44 |
Transaction History:
|
| 45 |
{text}
|
| 46 |
"""
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 50 |
response = model.generate_content(prompt)
|
| 51 |
full_text = response.text.strip()
|
| 52 |
|
|
@@ -65,16 +68,14 @@ def analyze_financial_data(file):
|
|
| 65 |
|
| 66 |
gr.Interface(
|
| 67 |
fn=analyze_financial_data,
|
| 68 |
-
inputs=gr.File(label="
|
| 69 |
outputs=[
|
| 70 |
-
gr.Textbox(label="
|
| 71 |
-
gr.Textbox(label="
|
| 72 |
-
gr.Textbox(label="
|
| 73 |
-
gr.Textbox(label="
|
| 74 |
-
gr.Textbox(label="
|
| 75 |
-
gr.Textbox(label="
|
| 76 |
-
gr.Textbox(label="
|
| 77 |
],
|
| 78 |
-
|
| 79 |
-
description="Upload your Paytm transaction PDF (text-based) and get structured financial insights using Gemini AI.",
|
| 80 |
-
).launch()
|
|
|
|
| 2 |
import PyPDF2
|
| 3 |
import google.generativeai as genai
|
| 4 |
import re
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Load API key safely
|
| 8 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") # set in environment
|
| 9 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 10 |
|
| 11 |
def extract_text_from_pdf(file):
|
|
|
|
| 29 |
text = extract_text_from_pdf(file)
|
| 30 |
|
| 31 |
if not text:
|
| 32 |
+
return ("β οΈ Failed to extract text", "", "", "", "", "", "")
|
| 33 |
+
|
| 34 |
+
text = text[:6000] # LIMIT TOKENS (IMPORTANT)
|
|
|
|
| 35 |
|
| 36 |
prompt = f"""
|
| 37 |
+
Analyze Paytm transactions and return structured insights.
|
| 38 |
+
|
| 39 |
**Financial Insights**
|
| 40 |
+
**- Monthly Income & Expenses:** ...
|
| 41 |
+
**- Unnecessary Expense Categories:** ...
|
| 42 |
+
**- Estimated Savings %:** ...
|
| 43 |
+
**- Spending Trends:** ...
|
| 44 |
+
**- Category-wise Expense Breakdown (Partial):** ...
|
| 45 |
+
**- Cost Control Suggestions:** ...
|
| 46 |
+
|
| 47 |
Transaction History:
|
| 48 |
{text}
|
| 49 |
"""
|
| 50 |
|
| 51 |
try:
|
| 52 |
+
model = genai.GenerativeModel("gemini-1.5-flash-latest")
|
| 53 |
response = model.generate_content(prompt)
|
| 54 |
full_text = response.text.strip()
|
| 55 |
|
|
|
|
| 68 |
|
| 69 |
gr.Interface(
|
| 70 |
fn=analyze_financial_data,
|
| 71 |
+
inputs=gr.File(label="Upload Paytm PDF", file_types=[".pdf"]),
|
| 72 |
outputs=[
|
| 73 |
+
gr.Textbox(label="Status"),
|
| 74 |
+
gr.Textbox(label="Monthly Income & Expenses"),
|
| 75 |
+
gr.Textbox(label="Unnecessary Expense Categories"),
|
| 76 |
+
gr.Textbox(label="Estimated Savings %"),
|
| 77 |
+
gr.Textbox(label="Spending Trends"),
|
| 78 |
+
gr.Textbox(label="Category-wise Breakdown"),
|
| 79 |
+
gr.Textbox(label="Cost Control Suggestions"),
|
| 80 |
],
|
| 81 |
+
).launch()
|
|
|
|
|
|