Files
DBAdmin/inbox/Python/ai/post_gen.py
T
2026-05-18 06:40:19 +00:00

72 lines
2.9 KiB
Python

import openai
import requests
# Definiujemy klasę agenta do generowania artykułów na bloga
class BlogArticleGenerator:
def __init__(self, api_key):
self.api_key = api_key
openai.api_key = self.api_key
def generate_keywords(self, topic, number_of_keywords=10):
prompt = f"Wygeneruj {number_of_keywords} słów kluczowych na temat: {topic}"
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
max_tokens=50
)
keywords = response.choices[0].text.strip().split(', ')
return keywords
def generate_article(self, topic, keywords, number_of_sections=3, max_words_per_section=200):
outline_prompt = f"Utwórz konspekt artykułu na temat '{topic}', użyj słów kluczowych: {', '.join(keywords)}. Utwórz {number_of_sections} sekcje."
outline_response = openai.Completion.create(
engine="davinci",
prompt=outline_prompt,
max_tokens=150
)
outline = outline_response.choices[0].text.strip().split('\n')
article = f"# {topic}\n\n"
for section in outline:
discussion_prompt = f"Rozwiń punkt '{section}' artykułu na temat '{topic}', wprowadź szczegółowe informacje na ten temat, maksymalnie {max_words_per_section} słów."
discussion_response = openai.Completion.create(
engine="davinci",
prompt=discussion_prompt,
max_tokens=max_words_per_section
)
section_content = discussion_response.choices[0].text.strip()
article += f"## {section}\n\n{section_content}\n\n"
return article
def generate_image_description(self, topic):
prompt = f"Opisz obraz, który najlepiej przedstawia artykuł o temacie: {topic}. Opis powinien być kreatywny i pasować do tematyki."
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
max_tokens=50
)
return response.choices[0].text.strip()
# Przykład użycia agenta
if __name__ == "__main__":
api_key = "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"
agent = BlogArticleGenerator(api_key)
# Parametry artykułu
topic = "Bezpieczeństwo danych AI"
number_of_keywords = 10
number_of_sections = 3
max_words_per_section = 150
# Generowanie słów kluczowych
keywords = agent.generate_keywords(topic, number_of_keywords)
print("Słowa kluczowe:", keywords)
# Generowanie artykułu
article = agent.generate_article(topic, keywords, number_of_sections, max_words_per_section)
print("\nArtykuł:\n", article)
# Generowanie opisu do obrazu
image_description = agent.generate_image_description(topic)
print("\nOpis obrazu:", image_description)