Fix the logic of this code. updating parseddf isn't right: """ if name_ == 'main': """…
- User
Fix the logic of this code. updating parsed_df isn't right:
""" if name == 'main': """ load the csv of discussions - one row per dialogue: "proccessing_data/merged_by_discussion_2025-07-03.csv" Initialize an empty DataFrame to store parsed results: parsed_df with the columns: "Discussion", "Title", "Theme", "eTU_id", "DU_id", "eTU_type", "Text", "Turn", "Author" for each discussion, prompt the LLM with PARSING_DIALOGUE save the output as an additional column of the csv file in addition, parse each output and append it to the parsed_df dataframe
example arguments: --model GPT4_1 --temperature 0.0 --input_csv /home/esther/PycharmProjects/Dialogos/processing_data/merged_by_discussion_2025-07-07.csv --output_csv parsed_output.csv --split dev """ parser = argparse.ArgumentParser(description="Parse dialogues using an LLM.") parser.add_argument('--model', type=str, required=True, help="Model signature (e.g. gpt-4, claude-3-opus, gemini-1.5-pro)") parser.add_argument('--temperature', type=float, default=0.0, help="Sampling temperature (default: 0.0 for deterministic output)") parser.add_argument('--input_csv', type=str, default="proccessing_data/merged_by_discussion_2025-07-03.csv", help="Path to input CSV") parser.add_argument('--output_csv', type=str, default="parsed_output.csv", help="Path to save the parsed CSV output") parser.add_argument('--split', type=str, default="",) args = parser.parse_args() input_df = pd.read_csv(args.input_csv) # mark 10 random samples as dev set input_df["split"] = "test" input_df.loc[input_df.sample(n=10, random_state=SEED).index, "split"] = "dev" if args.split == "dev": # filter the input_df to only include dev samples input_df = input_df[input_df["split"] == "dev"] # update the output_csv to include the split in the name args.output_csv = args.output_csv.replace(".csv", "_dev.csv") parsed_df = pd.DataFrame( columns=["Discussion", "Title", "Theme", "DU_id", "eTU_id", "eTU_type", "Text", "Turn", "Author", "split"]) for index, row in input_df.iterrows(): discussion = row['Discussion'] sample = row['Formatted'] try: response = prompt_llm(sys_prompt=PARSING_DIALOGUE, u_prompt=sample, model=MODELS[args.model], temperature=args.temperature) # Save raw output to input_df input_df.at[index, 'llm_output'] = response # Parse and append structured JSON parsed_data = json.loads(response) # the parsed_data is a dict of 3 keys: ['title', 'themes', 'discussion'] # the Discussion is a list of dictionaries, each representing an eTU res_title = parsed_data.get("title", "") res_themes = parsed_data.get("themes", "") for item in parsed_data.get("discussion", []): item.update({ "Discussion": discussion, "Title": res_title, "split": input_df.at[index, "split"], "eTU_type": item.get("eTU_type", ""), # Default to Unclassified "Text": item.get("Text", ""), "Turn": item.get("Turn", -1), # Default to -1 if not present "Author": item.get("Author", ""), "Theme": item.get("Theme", ""), }) parsed_df = parsed_df.append(item, ignore_index=True) parsed_df.to_csv(args.output_csv, index=False) except json.JSONDecodeError as e: print(f"[ERROR] JSON parsing failed for discussion {discussion}: {e}") input_df.at[index, 'llm_output'] = f"[ERROR] JSON parse error: {e}" except Exception as e: print(f"[ERROR] Failed processing discussion {discussion}: {e}") input_df.at[index, 'llm_output'] = f"[ERROR] LLM failure: {e}" # Save outputs input_df.to_csv(args.output_csv.replace(".csv", "_with_raw_output.csv"), index=False) parsed_df.to_csv(args.output_csv, index=False) - Assistant
- User
I want the output csv to be saved for each iteration of prompting the model
- Assistant