File size: 6,072 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import gradio as gr
from datetime import datetime
from docx import Document
from docx.shared import Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from audio_processor import AudioProcessor
import config

class NewsApp:
    def __init__(self):
        self.processor = AudioProcessor()
        
    def process_audio_file(self, audio_file, content_type="news", language="tr"):
        """Process audio file and generate content"""
        try:
            if audio_file is None:
                return "Lütfen bir ses dosyası yükleyin.", None
            
            # Print debug information
            print(f"Received audio file: {audio_file}")
            
            # Create temporary file to save the uploaded content
            temp_dir = "temp_audio"
            os.makedirs(temp_dir, exist_ok=True)
            
            # Generate a unique filename
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            temp_audio_path = os.path.join(temp_dir, f"temp_audio_{timestamp}.m4a")
            
            # Save the uploaded file
            with open(temp_audio_path, "wb") as f:
                f.write(audio_file)
            
            print(f"Saved temporary file to: {temp_audio_path}")
            
            # Process audio and generate content
            results = self.processor.process_audio(
                audio_path=temp_audio_path,
                language=language,
                content_type=content_type,
                generate_content=True
            )
            
            if not results.get("generated_content"):
                return "İçerik oluşturulamadı. Lütfen ses kaydını kontrol edin.", None
            
            # Create Word document
            doc = Document()
            
            # Add title
            title = doc.add_heading(results["generated_content"]["title"], 0)
            title.alignment = WD_ALIGN_PARAGRAPH.CENTER
            
            # Add date
            date_paragraph = doc.add_paragraph()
            date_paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
            date_run = date_paragraph.add_run(f"Tarih: {results['date']}")
            date_run.font.size = Pt(10)
            date_run.font.color.rgb = RGBColor(128, 128, 128)
            
            # Add separator
            doc.add_paragraph("").add_run("_" * 50)
            
            # Add content
            content_lines = results["generated_content"]["content"].split('\n')
            current_paragraph = None
            
            for line in content_lines:
                if line.strip():
                    if line.startswith('#'):  # Handle headers
                        level = line.count('#')
                        text = line.strip('#').strip()
                        doc.add_heading(text, level)
                    else:
                        if current_paragraph is None or line.startswith('*'):
                            current_paragraph = doc.add_paragraph()
                        current_paragraph.add_run(line)
                else:
                    current_paragraph = None
            
            # Save document
            output_dir = "data/output"
            os.makedirs(output_dir, exist_ok=True)
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            doc_path = os.path.join(output_dir, f"haber_{timestamp}.docx")
            doc.save(doc_path)
            
            # Return success message and document path
            return f"İçerik başarıyla oluşturuldu!", doc_path
            
        except Exception as e:
            return f"Hata oluştu: {str(e)}", None

def create_ui():
    """Create Gradio interface"""
    app = NewsApp()
    
    with gr.Blocks(title="Ses Dosyasından Haber Oluşturma", theme=gr.themes.Soft()) as interface:
        gr.Markdown("""
        # 🎙️ Ses Dosyasından Haber/Blog Oluşturma
        
        Ses kaydınızı yükleyin, yapay zeka destekli sistemimiz sizin için profesyonel bir haber metni veya blog yazısı oluştursun.
        
        ### Nasıl Kullanılır:
        1. Ses dosyanızı yükleyin (.mp3, .m4a, .wav formatları desteklenir)
        2. İçerik tipini seçin (Haber/Blog)
        3. Dili seçin
        4. "Oluştur" butonuna tıklayın
        5. Oluşturulan Word belgesini indirin
        
        ### Önemli Notlar:
        - Desteklenen ses formatları: MP3, M4A, WAV
        - Maksimum dosya boyutu: 25MB
        - İşlem süresi dosya boyutuna göre değişebilir
        - Türkçe ve İngilizce dilleri desteklenmektedir
        """)
        
        with gr.Row():
            with gr.Column():
                audio_input = gr.File(
                    label="Ses Dosyası",
                    file_types=[".mp3", ".m4a", ".wav"],
                    type="binary"
                )
                
                content_type = gr.Radio(
                    choices=["news", "blog"],
                    value="news",
                    label="İçerik Tipi",
                    info="Oluşturulacak içeriğin türünü seçin"
                )
                
                language = gr.Radio(
                    choices=["tr", "en"],
                    value="tr",
                    label="Dil",
                    info="İçeriğin dilini seçin"
                )
                
                submit_btn = gr.Button("Oluştur", variant="primary")
            
            with gr.Column():
                output_message = gr.Textbox(
                    label="Durum",
                    interactive=False
                )
                
                output_file = gr.File(
                    label="Oluşturulan Dosya",
                    interactive=False
                )
        
        submit_btn.click(
            fn=app.process_audio_file,
            inputs=[audio_input, content_type, language],
            outputs=[output_message, output_file]
        )
    
    return interface

if __name__ == "__main__":
    demo = create_ui()
    demo.launch()