File size: 3,452 Bytes
55ee048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os

from fastapi import FastAPI, UploadFile, File

from FraudDetectionAgent import CodeAgent

app = FastAPI()


@app.post("/detect-fraud/")
async def detect_fraud(file: UploadFile = File(...)):
    contents = await file.read()
    temp_path = "temp_transactions.csv"

    with open(temp_path, "wb") as f:
        f.write(contents)

    agent = CodeAgent(data_path=temp_path)
    frauds = agent.run()

    os.remove(temp_path)
    return {"flagged_transactions": frauds}


from fastapi import FastAPI, UploadFile, File
from pydantic import BaseModel
from FraudDetectionAgent import FraudDetectionAgent
import pandas as pd
import os

app = FastAPI()


# Basic chat message schema
class ChatMessage(BaseModel):
    message: str


@app.post("/chat/")
def chat_with_agent(msg: ChatMessage):
    user_input = msg.message.lower()

    if "fraud" in user_input:
        return {"response": "You can upload a CSV file at /detect-fraud/ to check for fraudulent transactions."}
    elif "hello" in user_input or "hi" in user_input:
        return {"response": "Hello! I can help you detect transaction frauds. Ask me how."}
    else:
        return {"response": "I'm still learning! Try asking about fraud detection or uploading a CSV."}


@app.post("/detect-fraud/")
async def detect_fraud(file: UploadFile = File(...)):
    contents = await file.read()
    temp_path = "temp_transactions.csv"
    with open(temp_path, "wb") as f:
        f.write(contents)

    agent = FraudDetectionAgent(data_path=temp_path)
    frauds = agent.run()
    os.remove(temp_path)
    return {"flagged_transactions": [f.to_dict() for f in frauds]}


from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from FraudDetectionAgent import CodeAgent
import pandas as pd
import os

app = FastAPI()

# Enable CORS for browser frontend
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


class ChatMessage(BaseModel):
    message: str


@app.post("/chat/")
def chat_with_agent(msg: ChatMessage):
    text = msg.message.lower()
    if "fraud" in text:
        return {"response": "You can upload a CSV of transactions below and I'll tell you which ones look suspicious."}
    elif "hello" in text:
        return {"response": "Hi there! I'm your fraud detection assistant. Upload a CSV to get started."}
    else:
        return {
            "response": "I'm here to help with transaction fraud detection. Try asking about fraud or upload your data."}


@app.post("/upload/")
async def upload_file(file: UploadFile = File(...)):
    try:
        temp_file = "temp_data.csv"
        with open(temp_file, "wb") as f:
            f.write(await file.read())

        agent = FraudDetectionAgent(data_path=temp_file)
        frauds = agent.run()
        os.remove(temp_file)

        if not frauds:
            return {"response": "✅ No fraud detected!"}

        summary = "\n".join([
            f"- ID: {f.get('transaction_id')}, Amount: ${f.get('amount')}"
            for f in frauds[:5]
        ])
        return {"response": f"⚠️ Detected {len(frauds)} suspicious transactions:\n{summary}"}

    except Exception as e:
        return {"response": f"❌ Error processing file: {str(e)}"}