Spaces:
Running
Running
| # export_utils.py | |
| # Klasa do eksportu wynik贸w analizy do TXT i Word | |
| import streamlit as st | |
| from docx import Document | |
| from docx.shared import Inches | |
| from datetime import datetime | |
| import io | |
| class ExportManager: | |
| def __init__(self): | |
| self.timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
| def prepare_export_content(self, concept_description, analysis_results, selected_personas=None, custom_target=None): | |
| """ | |
| Przygotowuje sformatowan膮 tre艣膰 do eksportu | |
| """ | |
| content = f""" | |
| ANALIZA KONCEPCJI REKLAMOWEJ/PRODUKTOWEJ | |
| ======================================== | |
| Data analizy: {datetime.now().strftime("%d.%m.%Y %H:%M")} | |
| OPIS KONCEPCJI: | |
| {concept_description} | |
| ANALIZOWANE GRUPY DOCELOWE: | |
| """ | |
| if selected_personas: | |
| content += f"- Wybrane persony: {', '.join(selected_personas)}\n" | |
| if custom_target: | |
| content += f"- W艂asna grupa docelowa: {custom_target}\n" | |
| content += f""" | |
| WYNIKI ANALIZY: | |
| =============== | |
| {analysis_results} | |
| --- | |
| Wygenerowano przez Market Research AI | |
| Powered by DeepSeek | |
| """ | |
| return content | |
| def export_to_txt(self, concept_description, analysis_results, selected_personas=None, custom_target=None): | |
| """ | |
| Eksportuje wyniki do pliku TXT | |
| """ | |
| try: | |
| content = self.prepare_export_content(concept_description, analysis_results, selected_personas, custom_target) | |
| # Tworzenie bufora do pobrania | |
| txt_buffer = io.StringIO() | |
| txt_buffer.write(content) | |
| txt_content = txt_buffer.getvalue() | |
| return txt_content, f"analiza_rynku_{self.timestamp}.txt" | |
| except Exception as e: | |
| st.error(f"B艂膮d podczas eksportu do TXT: {str(e)}") | |
| return None, None | |
| def export_to_word(self, concept_description, analysis_results, selected_personas=None, custom_target=None): | |
| """ | |
| Eksportuje wyniki do dokumentu Word | |
| """ | |
| try: | |
| # Tworzenie nowego dokumentu Word | |
| doc = Document() | |
| # Tytu艂 | |
| title = doc.add_heading('ANALIZA KONCEPCJI REKLAMOWEJ/PRODUKTOWEJ', 0) | |
| title.alignment = 1 # Wy艣rodkowanie | |
| # Data | |
| doc.add_paragraph(f"Data analizy: {datetime.now().strftime('%d.%m.%Y %H:%M')}") | |
| doc.add_paragraph("") # Pusta linia | |
| # Opis koncepcji | |
| doc.add_heading('OPIS KONCEPCJI:', level=1) | |
| doc.add_paragraph(concept_description) | |
| doc.add_paragraph("") | |
| # Analizowane grupy | |
| doc.add_heading('ANALIZOWANE GRUPY DOCELOWE:', level=1) | |
| if selected_personas: | |
| p = doc.add_paragraph("Wybrane persony: ") | |
| p.add_run(', '.join(selected_personas)).bold = True | |
| if custom_target: | |
| p = doc.add_paragraph("W艂asna grupa docelowa: ") | |
| p.add_run(custom_target).bold = True | |
| doc.add_paragraph("") | |
| # Wyniki analizy | |
| doc.add_heading('WYNIKI ANALIZY:', level=1) | |
| # Podzia艂 wynik贸w na sekcje dla lepszej czytelno艣ci | |
| results_lines = analysis_results.split('\n') | |
| current_paragraph = "" | |
| for line in results_lines: | |
| if line.strip().startswith('**') and line.strip().endswith('**'): | |
| # To jest nag艂贸wek persony | |
| if current_paragraph: | |
| doc.add_paragraph(current_paragraph) | |
| current_paragraph = "" | |
| # Dodaj nag艂贸wek persony | |
| persona_name = line.strip().replace('**', '') | |
| doc.add_heading(persona_name, level=2) | |
| elif line.strip().startswith('**') and ':' in line: | |
| # To jest podsekcja (Co my艣l臋, Co czuj臋, Co zrobi臋) | |
| if current_paragraph: | |
| doc.add_paragraph(current_paragraph) | |
| current_paragraph = "" | |
| subsection = line.strip().replace('**', '') | |
| p = doc.add_paragraph() | |
| p.add_run(subsection).bold = True | |
| elif line.strip() == '---': | |
| # Separator | |
| if current_paragraph: | |
| doc.add_paragraph(current_paragraph) | |
| current_paragraph = "" | |
| doc.add_paragraph("") | |
| else: | |
| # Zwyk艂y tekst | |
| if line.strip(): | |
| current_paragraph += line + "\n" | |
| else: | |
| if current_paragraph: | |
| doc.add_paragraph(current_paragraph.strip()) | |
| current_paragraph = "" | |
| doc.add_paragraph("") | |
| # Dodaj ostatni paragraph je艣li istnieje | |
| if current_paragraph: | |
| doc.add_paragraph(current_paragraph.strip()) | |
| # Stopka | |
| doc.add_paragraph("") | |
| footer = doc.add_paragraph("---") | |
| footer.add_run("\nWygenerowano przez Market Research AI").italic = True | |
| footer.add_run("\nPowered by DeepSeek").italic = True | |
| # Zapisanie do bufora | |
| doc_buffer = io.BytesIO() | |
| doc.save(doc_buffer) | |
| doc_buffer.seek(0) | |
| return doc_buffer.getvalue(), f"analiza_rynku_{self.timestamp}.docx" | |
| except Exception as e: | |
| st.error(f"B艂膮d podczas eksportu do Word: {str(e)}") | |
| return None, None | |
| def create_download_buttons(self, concept_description, analysis_results, selected_personas=None, custom_target=None): | |
| """ | |
| Tworzy przyciski do pobrania plik贸w w Streamlit | |
| """ | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| # Export do TXT | |
| txt_content, txt_filename = self.export_to_txt( | |
| concept_description, analysis_results, selected_personas, custom_target | |
| ) | |
| if txt_content: | |
| st.download_button( | |
| label="馃搫 Pobierz jako TXT", | |
| data=txt_content, | |
| file_name=txt_filename, | |
| mime="text/plain" | |
| ) | |
| with col2: | |
| # Export do Word | |
| word_content, word_filename = self.export_to_word( | |
| concept_description, analysis_results, selected_personas, custom_target | |
| ) | |
| if word_content: | |
| st.download_button( | |
| label="馃摑 Pobierz jako Word", | |
| data=word_content, | |
| file_name=word_filename, | |
| mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document" | |
| ) | |
| # Test modu艂u | |
| if __name__ == "__main__": | |
| print("Export Utils - modu艂 testowy") | |
| export_manager = ExportManager() | |
| print(f"Timestamp: {export_manager.timestamp}") | |