| 202 | |
| 203 | |
| 204 | def read_data_file(file_path: Path, max_rows: int = 10) -> str: |
| 205 | |
| 206 | try: |
| 207 | if file_path.suffix.lower() == '.csv': |
| 208 | df = pd.read_csv(file_path) |
| 209 | elif file_path.suffix.lower() == '.xlsx': |
| 210 | df = pd.read_excel(file_path) |
| 211 | else: |
| 212 | return f"Unsupported file format: {file_path.suffix}" |
| 213 | |
| 214 | info = f"Dataset: {file_path.name}\n" |
| 215 | info += f"Shape: {df.shape}\n" |
| 216 | info += f"Columns: {list(df.columns)}\n\n" |
| 217 | |
| 218 | info += "Data types:\n" |
| 219 | for col, dtype in df.dtypes.items(): |
| 220 | info += f" {col}: {dtype}\n" |
| 221 | info += "\n" |
| 222 | |
| 223 | info += f"First {min(max_rows, len(df))} rows:\n" |
| 224 | info += df.head(max_rows).to_string(index=False) |
| 225 | |
| 226 | return info |
| 227 | |
| 228 | except Exception as e: |
| 229 | return f"Error reading {file_path.name}: {str(e)}" |
| 230 | |
| 231 | |
| 232 | def create_user_prompt(category: str, instruction: str, data_info: Dict[str, str]) -> str: |