File size: 5,693 Bytes
a9209e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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