From 662370ca609e3c0951e4bdf8a97f95071c189ea9 Mon Sep 17 00:00:00 2001 From: Floke Date: Sun, 30 Mar 2025 10:36:32 +0000 Subject: [PATCH] =?UTF-8?q?Log=20erweitert:=20Zeitstempel=20in=20gpt=5Fant?= =?UTF-8?q?worten=5Flog.csv=20erg=C3=A4nzt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- brancheneinstufung.py | 62 ++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/brancheneinstufung.py b/brancheneinstufung.py index 147b4175..f4d8fd8e 100644 --- a/brancheneinstufung.py +++ b/brancheneinstufung.py @@ -91,43 +91,16 @@ system_prompt = { "Ziel ist es, Unternehmen mit >50 Technikern im Außeneinsatz zu identifizieren.\n\n" "Struktur: Firmenname; Website; Ort; Aktuelle Einstufung; Beschreibung der Branche Extern\n\n" "Gib deine Antwort im CSV-Format (1 Zeile, 8 Spalten, durch Semikolon getrennt):\n" - "Wikipedia-Branche;LinkedIn-Branche;Umsatz (Mio €);Empfohlene Neueinstufung;Begründung;FSM-Relevanz (Ja/Nein/k.A. mit Begründung);Techniker-Einschätzung;Techniker-Begründung\n\n" + "Wikipedia-Branche;LinkedIn-Branche;Umsatz (Mio €);Empfohlene Neueinstufung;Begründung;" + "FSM-Relevanz (Ja/Nein/k.A. mit Begründung);Techniker-Einschätzung;Techniker-Begründung\n\n" "Falls ein Wikipedia-Link angegeben ist, vertraue ausschließlich den Angaben aus diesem Artikel für Branche und Umsatz.\n" "Falls kein Wikipedia-Link existiert, gib für 'Wikipedia-Branche' und 'Umsatz (Mio €)' bitte 'k.A.' aus.\n\n" "Ziel-Branchenschema:\n" + "\n".join(branches) ) } -# === GPT BEWERTUNG === -def classify_company(row, wikipedia_url=""): - user_prompt = { - "role": "user", - "content": ( - f"{row[0]};{row[1]};{row[2]};{row[4]};{row[5]}\n" - f"Wikipedia-Link: {wikipedia_url}" - ) - } - response = openai.chat.completions.create( - model="gpt-3.5-turbo", - messages=[system_prompt, user_prompt], - temperature=0 - ) - full_text = response.choices[0].message.content.strip() - lines = full_text.splitlines() - csv_line = next((l for l in lines if ";" in l and not l.lower().startswith("wikipedia-branche")), "") - parts = [v.strip().strip('"') for v in csv_line.split(";")] if csv_line else [] - if len(parts) != 8: - print("⚠️ Antwort unvollständig → Setze alles auf 'k.A.'") - parts = ["k.A."] * 8 - with open(LOG_CSV, "a", newline="", encoding="utf-8") as log: - writer = csv.writer(log, delimiter=";") - writer.writerow([row[0], *parts, full_text]) - return parts - -# === WIKIPEDIA DATEN LADEN === -WHITELIST_KATEGORIEN = [ - "Unternehmen", "Hersteller", "Produktion", "Industrie", "Maschinenbau", "Technik", "Dienstleistungsunternehmen" -] +# === WIKIPEDIA FUNKTION === +WHITELIST_KATEGORIEN = ["Unternehmen", "Hersteller", "Produktion", "Industrie", "Maschinenbau", "Technik", "Dienstleistungsunternehmen"] def get_wikipedia_data(name, website_hint=""): begriffe = [name.strip(), " ".join(name.split()[:2])] @@ -184,6 +157,29 @@ def get_wikipedia_data(name, website_hint=""): continue return "", "k.A.", "k.A." +# === GPT BEWERTUNG === +def classify_company(row, wikipedia_url=""): + user_prompt = { + "role": "user", + "content": f"{row[0]};{row[1]};{row[2]};{row[4]};{row[5]}\nWikipedia-Link: {wikipedia_url}" + } + response = openai.chat.completions.create( + model="gpt-3.5-turbo", + messages=[system_prompt, user_prompt], + temperature=0 + ) + full_text = response.choices[0].message.content.strip() + lines = full_text.splitlines() + csv_line = next((l for l in lines if ";" in l and not l.lower().startswith("wikipedia-branche")), "") + parts = [v.strip().strip('"') for v in csv_line.split(";")] if csv_line else [] + if len(parts) != 8: + print("⚠️ Antwort unvollständig → Setze alles auf 'k.A.'") + parts = ["k.A."] * 8 + with open(LOG_CSV, "a", newline="", encoding="utf-8") as log: + writer = csv.writer(log, delimiter=";") + writer.writerow([datetime.now().strftime("%Y-%m-%d %H:%M:%S"), row[0], *parts, full_text]) + return parts + # === VERARBEITUNG === for i in range(start, min(start + DURCHLÄUFE, len(sheet_values))): row = sheet_values[i] @@ -192,8 +188,8 @@ for i in range(start, min(start + DURCHLÄUFE, len(sheet_values))): url, wiki_branche, umsatz = get_wikipedia_data(row[0], row[1]) wiki, linkedin, umsatz_chat, new_cat, reason, fsm, techniker, techniker_reason = classify_company(row, wikipedia_url=url) - wiki_final = wiki_branche if wiki_branche != "k.A." else wiki - umsatz_final = umsatz if umsatz != "k.A." else umsatz_chat + wiki_final = wiki_branche if url else "k.A." + umsatz_final = umsatz if url else "k.A." values = [ wiki_final,