File size: 1,921 Bytes
3cd105e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Configuration Manager - Obsługa konfiguracji dla Streamlit

Używa st.session_state do przechowywania ustawień

"""

import streamlit as st


class ConfigManager:
    """Zarządza konfiguracją aplikacji w Streamlit"""

    def __init__(self):
        # Initialize session state if not exists
        if 'config' not in st.session_state:
            st.session_state.config = self._default_config()

    def _default_config(self):
        """Domyślna konfiguracja"""
        return {
            "api_key": "",
            "model": "gpt-4o",
            "temperature": 0.1,
            "max_tokens": 2000,
            "top_p": 1.0,
            "num_responses": 5
        }

    def get(self, key, default=None):
        """Pobiera wartość z konfiguracji"""
        return st.session_state.config.get(key, default)

    def set(self, key, value):
        """Ustawia wartość w konfiguracji"""
        st.session_state.config[key] = value

    def get_api_key(self):
        """

        Pobiera klucz API - priorytet:

        1. Streamlit secrets (production)

        2. Session state (user input)

        """
        # Try Streamlit secrets first (for Hugging Face deployment)
        try:
            if hasattr(st, 'secrets') and 'OPENAI_API_KEY' in st.secrets:
                return st.secrets['OPENAI_API_KEY']
        except:
            pass

        # Fallback to session state
        return st.session_state.config.get("api_key", "")

    def set_api_key(self, api_key):
        """Zapisuje klucz API do session state"""
        st.session_state.config["api_key"] = api_key

    def get_all_settings(self):
        """Zwraca wszystkie ustawienia"""
        return st.session_state.config.copy()

    def update_settings(self, settings):
        """Aktualizuje wiele ustawień jednocześnie"""
        st.session_state.config.update(settings)