Skip to content

Commit f8a502b

Browse files
authored
LLM_Update_for_streamlit_deployment.py
1 parent 83e94d1 commit f8a502b

1 file changed

Lines changed: 4 additions & 81 deletions

File tree

LLM.py

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,16 @@
55
import os
66
import json
77

8-
load_dotenv()
8+
# load_dotenv()
99

1010
# Get the API key
11-
api_key = os.getenv("GEMINI_API_KEY")
12-
11+
# api_key = os.getenv("GEMINI_API_KEY")
12+
api_key = st.secrets["GEMINI_API_KEY"]
1313
# Configure
1414
genai.configure(api_key=api_key)
1515

1616
model = genai.GenerativeModel("gemini-2.0-flash")
1717

18-
# def llm_response(df: pd.DataFrame):
19-
# schema_info = df.dtypes.to_dict()
20-
# missing_info = df.isna().sum().to_dict()
21-
# sample_data = df.sample(min(len(df), 20), random_state=42).to_dict()
22-
23-
# prompt = f"""
24-
# I have a dataset with the following information:
25-
26-
# Column data types:
27-
# {schema_info}
28-
29-
# Missing value counts:
30-
# {missing_info}
31-
32-
# Sample rows:
33-
# {sample_data}
34-
35-
# Instructions:
36-
# - Write ONLY Python code that cleans the entire DataFrame `df`.
37-
# - Do NOT subset the DataFrame using head(), tail(), sample(), or similar — cleaning must apply to all rows.
38-
# - Correct data types where possible (convert datetime columns, numeric columns, etc.).
39-
# - Fill missing values (mean/median for numeric, mode for categorical, etc.).
40-
# - Remove duplicates if any.
41-
# - The last line must be: `df_cleaned = df`
42-
# - Assume pandas is imported as pd, numpy as np.
43-
# - Do not print anything, do not create fake data.
44-
# - Do not create a new DataFrame.
45-
# """
46-
47-
48-
# response = model.generate_content(prompt)
49-
50-
# # Extract code between triple backticks if present
51-
# code_blocks = re.findall(r"```(?:python)?\n(.*?)```", response.text, re.DOTALL)
52-
# if code_blocks:
53-
# return code_blocks[0].strip()
54-
# else:
55-
# # No code fences — return raw but stripped text
56-
# return response.text.strip()
57-
58-
5918
def llm_response(df: pd.DataFrame, recommendations: list):
6019
schema_info = df.dtypes.to_dict()
6120
missing_info = df.isna().sum().to_dict()
@@ -106,42 +65,6 @@ def llm_response(df: pd.DataFrame, recommendations: list):
10665

10766

10867

109-
# def check_data_quality(df):
110-
# schema_info = {col: str(dtype) for col, dtype in df.dtypes.items()}
111-
# sample_rows = df.head(3).to_dict(orient="records")
112-
# null_counts = df.isnull().sum().to_dict()
113-
# basic_stats = df.describe(include="all").to_dict()
114-
115-
# llm_prompt = f"""
116-
# You are a data quality checker.
117-
# Dataset schema: {schema_info}
118-
# Sample rows: {sample_rows}
119-
# Missing values: {null_counts}
120-
# Stats: {basic_stats}
121-
122-
# Analyze the dataset and tell:
123-
# 1. Does this dataset need cleaning? (yes/no)
124-
# 2. If yes, list specific issues found (e.g., missing values, wrong datatypes, duplicates).
125-
# 3. Identify issues like inconsistent categories, spelling variations, or invalid entries.
126-
# 4. Do NOT recommend changing values that are already valid and consistent.
127-
# 5. Recommend mappings only when you detect clear duplicates (e.g., "navy blue" and "dark blue" → "blue").
128-
# 6. If a value looks valid but unfamiliar (e.g., a rare city name), mark it as "unverified" instead of forcing a mapping.
129-
# 7. Suggest cleaning actions in plain English.
130-
131-
# Respond in JSON format:
132-
# {{
133-
# "needs_cleaning": true/false,
134-
# "issues": [...],
135-
# "recommendations": [...]
136-
# }}
137-
# """
138-
# response = model.generate_content(llm_prompt)
139-
# return response.text
140-
141-
142-
import re
143-
import json
144-
14568
def check_data_quality(df):
14669
schema_info = {col: str(dtype) for col, dtype in df.dtypes.items()}
14770
sample_rows = df.head(3).to_dict(orient="records")
@@ -179,7 +102,7 @@ def check_data_quality(df):
179102
match = re.search(r"\{.*\}", raw_text, re.DOTALL)
180103
if match:
181104
try:
182-
return json.loads(match.group()) # Always return dict
105+
return json.loads(match.group()) # Always return dict
183106
except json.JSONDecodeError:
184107
return {
185108
"needs_cleaning": False,

0 commit comments

Comments
 (0)