Spaces:
Sleeping
Sleeping
| 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() |