|
5 | 5 | import os |
6 | 6 | import json |
7 | 7 |
|
8 | | -load_dotenv() |
| 8 | +# load_dotenv() |
9 | 9 |
|
10 | 10 | # 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"] |
13 | 13 | # Configure |
14 | 14 | genai.configure(api_key=api_key) |
15 | 15 |
|
16 | 16 | model = genai.GenerativeModel("gemini-2.0-flash") |
17 | 17 |
|
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 | | - |
59 | 18 | def llm_response(df: pd.DataFrame, recommendations: list): |
60 | 19 | schema_info = df.dtypes.to_dict() |
61 | 20 | missing_info = df.isna().sum().to_dict() |
@@ -106,42 +65,6 @@ def llm_response(df: pd.DataFrame, recommendations: list): |
106 | 65 |
|
107 | 66 |
|
108 | 67 |
|
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 | | - |
145 | 68 | def check_data_quality(df): |
146 | 69 | schema_info = {col: str(dtype) for col, dtype in df.dtypes.items()} |
147 | 70 | sample_rows = df.head(3).to_dict(orient="records") |
@@ -179,7 +102,7 @@ def check_data_quality(df): |
179 | 102 | match = re.search(r"\{.*\}", raw_text, re.DOTALL) |
180 | 103 | if match: |
181 | 104 | try: |
182 | | - return json.loads(match.group()) # ✅ Always return dict |
| 105 | + return json.loads(match.group()) # Always return dict |
183 | 106 | except json.JSONDecodeError: |
184 | 107 | return { |
185 | 108 | "needs_cleaning": False, |
|
0 commit comments