Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -518,6 +518,84 @@ def create_visualizations(df, column_mapping):
|
|
| 518 |
|
| 519 |
return visualizations
|
| 520 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
def process_transactions(file):
|
| 522 |
"""Main function to process transaction data and detect fraud"""
|
| 523 |
try:
|
|
|
|
| 518 |
|
| 519 |
return visualizations
|
| 520 |
|
| 521 |
+
def analyze_transaction_with_ai(transaction_data, suspicious_transactions, column_mapping):
|
| 522 |
+
"""Use OpenAI to analyze suspicious transactions and provide insights"""
|
| 523 |
+
if not openai.api_key:
|
| 524 |
+
return "OpenAI API key not found. Please add it to the Hugging Face Spaces secrets."
|
| 525 |
+
|
| 526 |
+
try:
|
| 527 |
+
# Prepare information for OpenAI, converting to a JSON-serializable format
|
| 528 |
+
suspicious_sample = suspicious_transactions.head(5).copy()
|
| 529 |
+
|
| 530 |
+
# Convert any datetime columns to string format to make it JSON serializable
|
| 531 |
+
for col in suspicious_sample.columns:
|
| 532 |
+
if pd.api.types.is_datetime64_any_dtype(suspicious_sample[col]):
|
| 533 |
+
suspicious_sample[col] = suspicious_sample[col].astype(str)
|
| 534 |
+
# Convert NumPy types to Python native types
|
| 535 |
+
elif suspicious_sample[col].dtype in (np.int64, np.float64):
|
| 536 |
+
suspicious_sample[col] = suspicious_sample[col].astype(float)
|
| 537 |
+
# Handle boolean columns
|
| 538 |
+
elif suspicious_sample[col].dtype == bool:
|
| 539 |
+
suspicious_sample[col] = suspicious_sample[col].astype(str)
|
| 540 |
+
|
| 541 |
+
# Convert to dictionary
|
| 542 |
+
suspicious_dict = suspicious_sample.to_dict(orient='records')
|
| 543 |
+
|
| 544 |
+
# Get summary statistics
|
| 545 |
+
summary_stats = {
|
| 546 |
+
"total_transactions": int(len(transaction_data)),
|
| 547 |
+
"flagged_transactions": int(len(suspicious_transactions)),
|
| 548 |
+
"flagged_percentage": float(round(len(suspicious_transactions) / len(transaction_data) * 100, 2)),
|
| 549 |
+
}
|
| 550 |
+
|
| 551 |
+
# Add amount-related statistics if available
|
| 552 |
+
amount_col = column_mapping.get("amount_column")
|
| 553 |
+
if amount_col and amount_col in transaction_data.columns:
|
| 554 |
+
summary_stats.update({
|
| 555 |
+
"avg_transaction_amount": float(round(transaction_data[amount_col].mean(), 2)),
|
| 556 |
+
"suspicious_avg_amount": float(round(suspicious_transactions[amount_col].mean(), 2))
|
| 557 |
+
})
|
| 558 |
+
|
| 559 |
+
# Create prompt for OpenAI
|
| 560 |
+
prompt = f"""
|
| 561 |
+
Analyze these potentially fraudulent transactions and identify patterns or anomalies:
|
| 562 |
+
|
| 563 |
+
Transaction Data Summary:
|
| 564 |
+
{json.dumps(summary_stats)}
|
| 565 |
+
|
| 566 |
+
Column Mapping:
|
| 567 |
+
{json.dumps(column_mapping)}
|
| 568 |
+
|
| 569 |
+
Sample of Suspicious Transactions:
|
| 570 |
+
{json.dumps(suspicious_dict)}
|
| 571 |
+
|
| 572 |
+
Provide a concise fraud analysis report with:
|
| 573 |
+
1. Key patterns and red flags in these transactions
|
| 574 |
+
2. Possible fraud scenarios explaining the anomalies
|
| 575 |
+
3. Recommended next steps for investigation
|
| 576 |
+
"""
|
| 577 |
+
|
| 578 |
+
# Create an OpenAI client with the API key
|
| 579 |
+
client = openai.OpenAI(api_key=openai.api_key)
|
| 580 |
+
|
| 581 |
+
# Call OpenAI API
|
| 582 |
+
response = client.chat.completions.create(
|
| 583 |
+
model="gpt-3.5-turbo",
|
| 584 |
+
messages=[
|
| 585 |
+
{"role": "system", "content": "You are a fraud detection expert helping analyze suspicious financial transactions."},
|
| 586 |
+
{"role": "user", "content": prompt}
|
| 587 |
+
],
|
| 588 |
+
max_tokens=800
|
| 589 |
+
)
|
| 590 |
+
|
| 591 |
+
# Return the AI analysis
|
| 592 |
+
return response.choices[0].message.content
|
| 593 |
+
|
| 594 |
+
except Exception as e:
|
| 595 |
+
import traceback
|
| 596 |
+
error_trace = traceback.format_exc()
|
| 597 |
+
return f"Error in AI analysis: {str(e)}\n\nTrace: {error_trace}"
|
| 598 |
+
|
| 599 |
def process_transactions(file):
|
| 600 |
"""Main function to process transaction data and detect fraud"""
|
| 601 |
try:
|