Spaces:
Sleeping
Sleeping
from typing import Dict, List, Optional | |
import openai | |
import config | |
class ContentGenerator: | |
def __init__(self): | |
openai.api_key = config.OPENAI_API_KEY | |
def generate_content( | |
self, | |
topic: str, | |
keywords: Optional[List[str]] = None, | |
language: str = config.DEFAULT_LANGUAGE | |
) -> Dict: | |
""" | |
Generates blog post content using OpenAI's API. | |
Args: | |
topic (str): The main topic for the blog post | |
keywords (List[str], optional): Specific keywords to include | |
language (str): Target language code | |
Returns: | |
Dict: Generated content with title and body | |
""" | |
if language not in config.SUPPORTED_LANGUAGES: | |
raise ValueError( | |
config.ERROR_MESSAGES["invalid_language"].format( | |
", ".join(config.SUPPORTED_LANGUAGES) | |
) | |
) | |
prompt = self._create_prompt(topic, keywords, language) | |
try: | |
# Use more focused and efficient prompting | |
if language == "tr": | |
system_prompt = """Siz deneyimli bir haber editörüsünüz. Ses kaydından profesyonel bir haber/makale oluşturacaksınız. | |
Yazım kuralları: | |
1. Resmi ve profesyonel dil kullanın | |
2. Tekrarlardan kaçının | |
3. Önemli bilgileri vurgulayın | |
4. Alıntıları doğru formatta kullanın | |
5. İstatistikleri ve sayısal verileri öne çıkarın | |
6. Akıcı ve anlaşılır bir dil kullanın | |
7. Paragraflar arası geçişleri düzgün yapın""" | |
else: | |
system_prompt = """You are an experienced news editor. You will create a professional article from the audio recording. | |
Writing rules: | |
1. Use formal and professional language | |
2. Avoid repetitions | |
3. Emphasize important information | |
4. Use quotes in correct format | |
5. Highlight statistics and numerical data | |
6. Use clear and flowing language | |
7. Ensure smooth transitions between paragraphs""" | |
response = openai.chat.completions.create( | |
model=config.MODEL_NAME, | |
messages=[ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": prompt} | |
], | |
temperature=0.2, # Even lower temperature for faster and more consistent output | |
max_tokens=800, # Further reduced for faster response | |
presence_penalty=-0.2, # More focus on key information | |
frequency_penalty=0.5, # Stronger repetition avoidance | |
top_p=0.8, # More focused token selection | |
n=1 # Single completion for speed | |
) | |
content = response.choices[0].message.content | |
# Parse the content into title and body | |
lines = content.split("\n") | |
title = lines[0].replace("# ", "") | |
body = "\n".join(lines[1:]).strip() | |
return { | |
"title": title, | |
"content": body, | |
"language": language | |
} | |
except Exception as e: | |
raise Exception(f"{config.ERROR_MESSAGES['api_error']} Details: {str(e)}") | |
def _create_prompt(self, topic: str, keywords: Optional[List[str]], language: str) -> str: | |
"""Creates a detailed prompt for the OpenAI API.""" | |
if language == "tr": | |
base_prompt = f"""Aşağıdaki ses kaydı transkripsiyonunu profesyonel bir haber/makaleye dönüştürün: | |
{topic} | |
Yazım Formatı: | |
1. Başlık: | |
- Çarpıcı ve konuyu yansıtan bir başlık (maksimum 8 kelime) | |
- Alt başlık: Konuyu detaylandıran bir cümle | |
2. Giriş Paragrafı: | |
- Kim, ne, nerede, ne zaman, neden, nasıl sorularını yanıtlayan özet | |
- En önemli bilgiyi vurgulayan spot cümle | |
3. Gelişme: | |
- Her paragraf tek bir konuya odaklanmalı | |
- Önemli alıntılar: "..." şeklinde ve konuşmacının unvanıyla birlikte | |
- Sayısal veriler ve istatistikler vurgulanmalı | |
- Karşılaştırmalar ve analizler eklenmelidir | |
4. Sonuç: | |
- Konunun etkilerini ve önemini vurgulayan kapanış | |
- Varsa gelecek adımlar veya beklentiler | |
Metin profesyonel, akıcı ve gazetecilik standartlarına uygun olmalıdır.""" | |
else: | |
base_prompt = f"""Transform the following audio transcript into a professional article: | |
{topic} | |
Writing Format: | |
1. Title: | |
- Impactful and reflective headline (maximum 8 words) | |
- Subheading: One sentence elaborating the topic | |
2. Introduction: | |
- Summary answering who, what, where, when, why, how | |
- Lead sentence emphasizing the most important information | |
3. Body: | |
- Each paragraph focused on a single topic | |
- Important quotes: In "..." format with speaker's title | |
- Numerical data and statistics should be highlighted | |
- Include comparisons and analysis | |
4. Conclusion: | |
- Closing emphasizing impact and importance | |
- Future steps or expectations if applicable | |
Text should be professional, flowing, and adherent to journalistic standards.""" | |
if keywords: | |
if language == "tr": | |
base_prompt += f"\n\nBu anahtar noktaları vurgulayın: {', '.join(keywords)}" | |
else: | |
base_prompt += f"\n\nEmphasize these key points: {', '.join(keywords)}" | |
return base_prompt |