import openai
import csv
import json
import os
from time import sleep
# 🔑 Pune aici cheia ta OpenAI
openai.api_key = "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"
# Folder pentru output
output_folder = "faq_output"
os.makedirs(output_folder, exist_ok=True)
# Fișier CSV cu produsele: coloane "name", "description"
csv_file = "produse.csv"
# Funcție pentru generare FAQ OpenAI
def generate_faq(product_name, product_description, n_questions=10):
prompt = f"""
Ești un expert SEO pentru magazine online.
Creează {n_questions} întrebări și răspunsuri pentru pagina de produs:
Nume produs: {product_name}
Descriere: {product_description}
Formatează răspunsul în JSON astfel:
[
{{"question": "Întrebare 1", "answer": "Răspuns 1"}},
...
]
"""
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
temperature=0.7
)
try:
return json.loads(response.choices[0].message.content)
except json.JSONDecodeError:
print(f"⚠️ Eroare la produs: {product_name}")
return []
# Funcție pentru JSON-LD schema
def create_json_ld(faq_list):
json_ld = {
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": []
}
for item in faq_list:
json_ld["mainEntity"].append({
"@type": "Question",
"name": item["question"],
"acceptedAnswer": {
"@type": "Answer",
"text": item["answer"]
}
})
return json.dumps(json_ld, indent=2, ensure_ascii=False)
# Funcție pentru HTML FAQ ready-to-paste în Gomag
def create_html_faq(faq_list):
html = '
'
for item in faq_list:
html += f'
{item["question"]}
{item["answer"]}
'
html += '
'
return html
# Script principal
faq_csv_rows = []
with open(csv_file, newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
product_name = row["name"]
product_description = row["description"]
print(f"🔹 Generez FAQ pentru: {product_name}")
faq_list = generate_faq(product_name, product_description)
if faq_list:
# Salvează schema JSON-LD individual
schema_file = os.path.join(output_folder, f"{product_name}_faq_schema.json")
with open(schema_file, "w", encoding="utf-8") as sf:
sf.write(create_json_ld(faq_list))
# Salvează HTML FAQ individual
html_file = os.path.join(output_folder, f"{product_name}_faq.html")
with open(html_file, "w", encoding="utf-8") as hf:
hf.write(create_html_faq(faq_list))
# Pregătește CSV pentru import în Gomag
questions = "|".join([q["question"] for q in faq_list])
answers = "|".join([q["answer"] for q in faq_list])
faq_csv_rows.append({
"product_name": product_name,
"faq_questions": questions,
"faq_answers": answers
})
sleep(1) # evită rate-limit API
# Salvează CSV-ul final
csv_output_file = os.path.join(output_folder, "faq_for_gomag.csv")
with open(csv_output_file, "w", newline="", encoding="utf-8") as csvfile:
fieldnames = ["product_name", "faq_questions", "faq_answers"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(faq_csv_rows)
print(f"🎯 Toate FAQ-urile și schema JSON-LD + HTML au fost generate în folderul {output_folder}")