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/forestry/farming news, precision agriculture, remote sensing, NDVI, satellite imagery, crop monitoring, yield estimation, soil moisture, irrigation advisory, farm management, agri-tech, climate-smart agriculture, sustainable agriculture, agri-input optimization, agricultural IoT, GPS mapping, weather forecasting, drought monitoring, land use mapping, pest detection, crop health, forestry monitoring, carbon sequestration, agroforestry, biomass estimation, drone mapping, smart farming, geospatial analytics, carbon credits, deforestation tracking, land degradation, crop insurance, agricultural subsidies, regenerative agriculture, organic farming, climate change, water resource management, AI in agriculture, ML for crop prediction, agricultural policy, smart irrigation, field scouting, weed detection, fertilizer advisory, farm automation, agronomy, agri-fintech, farm-to-fork traceability, agricultural extension, rural development, ESG in agriculture, remote field diagnostics, plant disease detection, agricultural marketplaces, agricultural data platforms, precision forestry, sustainable land management, government agri schemes, digital agriculture, food security, global food supply, agricultural exports, food price inflation, commodity markets, land monitoring, harvest forecasting''' model = "gpt-4.1-mini-2025-04-14" prompt = f'Output YES if the provided text is related to any of the following topics: {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" 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. 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')