import csv from openai import OpenAI # API keys and clients setup hf2 = "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" model_key = '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' client = OpenAI(api_key=model_key) import csv import csv def delete_first_element(csv_file_path): """ Deletes the first element (top-left cell) of a CSV file and adjusts the rest of the content so there are no blank cells left. Args: csv_file_path (str): Path to the CSV file. Returns: None """ # Read all data into a flat list with open(csv_file_path, mode='r', newline='', encoding='utf-8') as file: reader = list(csv.reader(file)) flat_data = [cell for row in reader for cell in row] # Remove the first element if it exists if flat_data: flat_data.pop(0) # Reconstruct the 2D list with the same number of columns as original if reader: num_cols = max(len(row) for row in reader) new_rows = [flat_data[i:i+num_cols] for i in range(0, len(flat_data), num_cols)] else: new_rows = [] # Write back to the CSV file with open(csv_file_path, mode='w', newline='', encoding='utf-8') as file: writer = csv.writer(file) writer.writerows(new_rows) def call_genai(prompt, model): #model = "gpt-4.1-mini-2025-04-14" completion = client.chat.completions.create( model=model, messages=[ { "role": "user", "content": prompt } ] ) # print(completion.choices[0].message.content) return completion.choices[0].message.content def read_first_element(csv_file_path): """ Reads the first element (top-left cell) of a CSV file. Args: csv_file_path (str): Path to the CSV file. Returns: str: The first element of the CSV file, or None if file is empty. """ with open(csv_file_path, mode='r', newline='', encoding='utf-8') as file: reader = csv.reader(file) for row in reader: if row: # Make sure the row is not empty return row[0] # Return the first element of the first row return None # In case the file is empty or has no valid rows def get_article_obj(file_path): main_keyword = read_first_element(file_path) print(main_keyword) relevant_keywords = '''[ "Agriculture", "Horticulture", "Forestry", "Farming News", "Mining", "Infrastructure", "Defense/Aerospace", "Remote Sensing", "AI/ML", "AI in Agriculture", "ML for Crop Prediction", "Satellite Imaging", "SAR Imaging", "Drone Mapping", "NDVI Analysis", "Precision Agriculture", "Precision Forestry", "Soil Moisture Monitoring", "Soil Health Monitoring", "Smart Irrigation", "Irrigation Advisory", "IoT Integration", "Agricultural IoT", "Climate-Smart Agriculture", "Sustainable Agriculture", "Regenerative Agriculture", "Organic Farming", "Smart Farming", "Farm Automation", "Farm Management", "Plantation Management", "Remote Field Diagnostics", "Field Scouting", "Crop Monitoring", "Crop Health Analysis", "Pest Detection", "Weed Detection", "Plant Disease Detection", "Yield Estimation", "Harvest Forecasting", "Fertilizer Advisory", "Agri-input Optimization", "Crop Insurance & Loans", "Crop Insurance", "Agricultural Subsidies", "Carbon Footprinting", "Carbon Credits", "Carbon Sequestration", "Blockchain Traceability", "Farm-to-Fork Traceability", "Fleet Management", "Agri-Fintech", "Agronomy", "GPS Mapping", "Geospatial Analytics", "GIS Mapping", "Weather Forecasting", "Drought Monitoring", "Disaster Management", "Flood Risk Assessment", "Land Use Mapping", "Land Monitoring", "Change Detection", "Land Degradation", "Land Parcel Identification", "Land Encroachment Detection", "Illegal Mining Monitoring", "Deforestation Monitoring", "Deforestation Tracking", "Forestry Monitoring", "Agroforestry", "Biomass Estimation", "Ecosystem Services Mapping", "Terrain Analysis", "Surface Temperature Analysis", "Water Resource Management", "Watershed Management", "Groundwater Assessment", "Climate Change", "Natural Resource Management", "Digital Agriculture", "Digital Land Records", "Sustainable Land Management", "Government Agri Schemes", "Agricultural Policy", "Agricultural Extension", "Agri-tech", "Agri-market Linkage", "Agri-Education", "Rural Development", "Food Security", "Global Food Supply", "Agricultural Exports", "Commodity Markets", "Food Price Inflation", "Supply Chain Management", "Agri-Logistics", "Agro-Advisory Services", "Smart Cities", "Infrastructure Planning", "Survey & Mapping", "Environmental Monitoring", "Climate Monitoring", "Digital Farming", "Custom Alert Systems", "Insurance Claim Validation", "Farmer Outreach", "Yield Mapping", "Crop Classification", "Crop Calendar Optimization", "Agrochemical Impact Tracking", "Agricultural Data Platforms", "Agricultural Marketplaces", "Drone Imagery", "Thermal Imaging", "Multispectral Imaging", "Hyperspectral Imaging", "Military Surveillance", "Border Monitoring", "Power Grid Monitoring", "Road Condition Mapping", "Railway Safety Surveillance", "Solar Farm Monitoring", "Wind Farm Optimization", "Biodiversity Tracking", "Rural Connectivity Assessment", "Food Traceability", "ESG in Agriculture", "Honey/ Hive" ] ''' model = "gpt-4.1-mini-2025-04-14" prompt = f'Output YES if the provided text is related to any of the following topics (exclude dairy/ cattle farming): {relevant_keywords}. Only answer YES OR NO. Text: {main_keyword}' is_relevant = call_genai(prompt, model) print('is relevant', is_relevant) if "no" in is_relevant.lower(): return None, None # model = "gpt-4o-mini-search-preview-2025-03-11" # model = "gpt-4.1-mini-2025-04-14" content = call_genai(f'I want to write an article on this topic in the context of agriculture/ farming/ forestry: {main_keyword} Can you cover everything about this topic in 1000 words being relevant in 2025. Output only the content.', model) #keywords = call_genai(f'I want 5 to 10 keywords/keyphrases that are related to this content: {content}. Output only the keywords/keyphrases in a comma separated list. No other text.') content_obj = { 'body': content } all_contents = [] all_contents.append(content_obj) #print(content_obj) #delete_first_element(file_path) return all_contents, main_keyword # get_article_obj('final_queries_list_for_blogs.csv')