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 = "gpt-4o-search-preview" #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) 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.') #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')