Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +19 -15
src/streamlit_app.py
CHANGED
|
@@ -6,9 +6,10 @@ os.environ["STREAMLIT_HOME"] = "/tmp"
|
|
| 6 |
import streamlit as st
|
| 7 |
import pandas as pd
|
| 8 |
import json
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
PROMPT_INSTRUCTIONS_TEXT = """
|
| 14 |
You are a forensic auditor AI with deep domain expertise and a sharp eye for irregularities. Your job is to identify **anomalies** in financial transaction data.
|
|
@@ -45,19 +46,21 @@ Output your findings in this JSON format:
|
|
| 45 |
"""
|
| 46 |
|
| 47 |
def query_openai(prompt: str) -> dict:
|
| 48 |
-
response = openai.ChatCompletion.create(
|
| 49 |
-
model="gpt-4-turbo",
|
| 50 |
-
messages=[
|
| 51 |
-
{"role": "system", "content": "You analyze financial transactions for anomalies."},
|
| 52 |
-
{"role": "user", "content": prompt}
|
| 53 |
-
],
|
| 54 |
-
temperature=0.2,
|
| 55 |
-
max_tokens=2048
|
| 56 |
-
)
|
| 57 |
try:
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except json.JSONDecodeError:
|
| 60 |
return {"error": "Failed to parse JSON from LLM response."}
|
|
|
|
|
|
|
| 61 |
|
| 62 |
st.set_page_config(page_title="LLM Financial Anomaly Detector", layout="wide")
|
| 63 |
st.title("LLM-Powered Financial Anomaly Detector")
|
|
@@ -80,8 +83,8 @@ if uploaded_file:
|
|
| 80 |
data_subset = df.to_dict(orient="records")
|
| 81 |
focused_values = df[selected_column].dropna().unique().tolist()
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
column_focus_note = f"\n\nFocus especially on anomalies involving the column `{selected_column}`.
|
| 85 |
|
| 86 |
prompt = PROMPT_INSTRUCTIONS_TEXT + column_focus_note + "DATA:\n" + json.dumps(data_subset[:100], indent=2)
|
| 87 |
result = query_openai(prompt)
|
|
@@ -91,7 +94,8 @@ if uploaded_file:
|
|
| 91 |
st.dataframe(pd.json_normalize(result["anomalies"]), use_container_width=True)
|
| 92 |
else:
|
| 93 |
st.warning("No anomalies found or the model response was invalid.")
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
st.error(f"Could not read CSV. Error: {e}")
|
| 96 |
else:
|
| 97 |
-
st.info("Please upload a CSV file to begin.")
|
|
|
|
| 6 |
import streamlit as st
|
| 7 |
import pandas as pd
|
| 8 |
import json
|
| 9 |
+
from openai import OpenAI
|
| 10 |
|
| 11 |
+
# Initialize OpenAI client
|
| 12 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 13 |
|
| 14 |
PROMPT_INSTRUCTIONS_TEXT = """
|
| 15 |
You are a forensic auditor AI with deep domain expertise and a sharp eye for irregularities. Your job is to identify **anomalies** in financial transaction data.
|
|
|
|
| 46 |
"""
|
| 47 |
|
| 48 |
def query_openai(prompt: str) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
+
response = client.chat.completions.create(
|
| 51 |
+
model="gpt-4-turbo",
|
| 52 |
+
messages=[
|
| 53 |
+
{"role": "system", "content": "You analyze financial transactions for anomalies."},
|
| 54 |
+
{"role": "user", "content": prompt}
|
| 55 |
+
],
|
| 56 |
+
temperature=0.2,
|
| 57 |
+
max_tokens=2048
|
| 58 |
+
)
|
| 59 |
+
return json.loads(response.choices[0].message.content)
|
| 60 |
except json.JSONDecodeError:
|
| 61 |
return {"error": "Failed to parse JSON from LLM response."}
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return {"error": str(e)}
|
| 64 |
|
| 65 |
st.set_page_config(page_title="LLM Financial Anomaly Detector", layout="wide")
|
| 66 |
st.title("LLM-Powered Financial Anomaly Detector")
|
|
|
|
| 83 |
data_subset = df.to_dict(orient="records")
|
| 84 |
focused_values = df[selected_column].dropna().unique().tolist()
|
| 85 |
|
| 86 |
+
# Highlight selected column in the prompt
|
| 87 |
+
column_focus_note = f"\n\nFocus especially on anomalies involving the column `{selected_column}`. Here are some example values:\n{json.dumps(focused_values[:30])[:1000]}\n\n"
|
| 88 |
|
| 89 |
prompt = PROMPT_INSTRUCTIONS_TEXT + column_focus_note + "DATA:\n" + json.dumps(data_subset[:100], indent=2)
|
| 90 |
result = query_openai(prompt)
|
|
|
|
| 94 |
st.dataframe(pd.json_normalize(result["anomalies"]), use_container_width=True)
|
| 95 |
else:
|
| 96 |
st.warning("No anomalies found or the model response was invalid.")
|
| 97 |
+
st.json(result)
|
| 98 |
except Exception as e:
|
| 99 |
st.error(f"Could not read CSV. Error: {e}")
|
| 100 |
else:
|
| 101 |
+
st.info("Please upload a CSV file to begin.")
|