import pandas as pd import re def load_csv_data(file_path): """Load CSV data from file path""" try: return pd.read_csv(file_path) except Exception as e: print(f"Error loading CSV: {str(e)}") # Return a minimal dataframe for demonstration return pd.DataFrame({ 'Crop': ['Tomato', 'Apple'], 'Disease': ['Early Blight', 'Apple Scab'], 'Symptoms': ['Yellow spots on leaves', 'Dark scab-like lesions'], 'Treatment': ['Remove affected leaves', 'Prune affected branches'], 'Medicine/Chemical Control': ['Copper fungicide', 'Sulfur spray'] }) def analyze_csv_data(df): """Analyze CSV data and return statistics""" stats = { "total_entries": len(df), "unique_crops": df['Crop'].nunique(), "unique_diseases": df['Disease'].nunique(), "crops": df['Crop'].unique().tolist() } return stats def search_disease_info(df, query): """Search for disease information in the dataframe""" query = query.lower() results = [] # Search in all columns for _, row in df.iterrows(): score = 0 if query in str(row['Crop']).lower(): score += 3 if query in str(row['Disease']).lower(): score += 5 if query in str(row['Symptoms']).lower(): score += 2 if query in str(row['Treatment']).lower(): score += 1 if query in str(row['Medicine/Chemical Control']).lower(): score += 1 if score > 0: results.append({ 'score': score, 'Crop': row['Crop'], 'Disease': row['Disease'], 'Symptoms': row['Symptoms'], 'Treatment': row['Treatment'], 'Medicine/Chemical Control': row['Medicine/Chemical Control'] }) # Sort by relevance score results.sort(key=lambda x: x['score'], reverse=True) return results def extract_entities_from_question(question, df): """Extract crop and disease entities from a question""" question = question.lower() # Get all crops and diseases from the dataframe crops = set(df['Crop'].str.lower()) diseases = set(df['Disease'].str.lower()) # Find matches in the question found_crop = None found_disease = None for crop in crops: if crop in question: found_crop = crop break for disease in diseases: if disease in question: found_disease = disease break return found_crop, found_disease def get_treatment_for_disease(df, crop, disease): """Get treatment information for a specific crop and disease""" if crop and disease: matches = df[(df['Crop'].str.lower() == crop.lower()) & (df['Disease'].str.lower() == disease.lower())] if not matches.empty: row = matches.iloc[0] return { 'crop': row['Crop'], 'disease': row['Disease'], 'symptoms': row['Symptoms'], 'treatment': row['Treatment'], 'medicine': row['Medicine/Chemical Control'] } return None