Expenseanslyzer / app.py
tayy786's picture
Create app.py
dbda235 verified
import gradio as gr
import pandas as pd
from groq import Groq
import os
# Load Groq API Key from HuggingFace Secrets
GROQ_API_KEY = os.getenv("Clip")
client = Groq(api_key=GROQ_API_KEY)
def analyze_expenses(file):
if file is None:
return "Please upload a CSV file."
df = pd.read_csv(file.name)
expense_text = ""
for index, row in df.iterrows():
expense_text += f"Date: {row['Date']}, Description: {row['Description']}, Amount: {row['Amount']}\n"
prompt = f"""
You are a financial AI assistant.
Analyze the following expenses.
1. Categorize each expense.
2. Calculate total spending.
3. Give category-wise summary.
4. Give smart budgeting advice.
Expenses:
{expense_text}
"""
try:
response = client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[
{"role": "user", "content": prompt}
],
temperature=0.5
)
result = response.choices[0].message.content
return result
except Exception as e:
return f"Error: {str(e)}"
with gr.Blocks() as app:
gr.Markdown("# 🤖 AI Expense Analyzer (Powered by Groq)")
gr.Markdown("Upload your CSV file and get AI-powered financial insights.")
file_input = gr.File(label="Upload CSV File")
output = gr.Textbox(label="AI Analysis Result", lines=20)
analyze_button = gr.Button("Analyze Expenses")
analyze_button.click(analyze_expenses, inputs=file_input, outputs=output)
app.launch()