File size: 4,359 Bytes
ea4ebfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pip
import subprocess
import importlib
import sys
import logging # Loglama için
import streamlit as st # st.warning/error/info kullanmak için
import types # Monkey patch için

# Logging ayarları
# logger = logging.getLogger(__name__)
# Streamlit logları yeterli olabilir, ekstra logger'a gerek yok gibi

def install_package(package):
    """
    Belirtilen paketi pip ile yükler
    """
    st.info(f"{package} paketi yükleniyor... Bu biraz zaman alabilir.")
    try:
        # subprocess yerine pip.main kullanılabilir
        result = pip.main(['install', package])
        if result == 0:
            st.success(f"{package} paketi başarıyla yüklendi.")
            return True
        else:
            st.error(f"{package} paketi yüklenemedi. Hata kodu: {result}")
            return False
    except Exception as e:
        st.error(f"{package} paketi yüklenirken hata oluştu: {str(e)}")
        return False

def ensure_news_libraries():
    """
    Haber API'leri için gerekli kütüphanelerin yüklü olduğundan emin olur ve monkey patch uygular
    """
    required_packages = ['pygooglenews', 'newspaper3k', 'lxml_html_clean', 'bs4']
    missing_packages = []
    
    for package in required_packages:
        try:
            if package == 'newspaper3k':
                importlib.import_module('newspaper')
            elif package == 'lxml_html_clean':
                importlib.import_module('lxml_html_clean')
            elif package == 'bs4':
                importlib.import_module('bs4')
            else:
                importlib.import_module(package)
        except ImportError:
            missing_packages.append(package)
    
    if missing_packages:
        st.warning(f"Haber özelliğini kullanmak için gerekli kütüphaneler ({', '.join(missing_packages)}) eksik. Yükleniyor...")
        for package in missing_packages:
            try:
                install_package(package)
            except Exception as e:
                st.error(f"{package} yüklenirken hata oluştu: {str(e)}")
                return False
            
        st.info("Kurulum tamamlandı. Sayfa yenileniyor...")
        
        # Monkey patch'i uygula
        try:
            import lxml_html_clean
            
            # lxml.html için sahte bir modül oluşturun
            if 'lxml.html.clean' in sys.modules:
                del sys.modules['lxml.html.clean']
            
            if 'lxml' not in sys.modules:
                sys.modules['lxml'] = types.ModuleType('lxml')
            
            if 'lxml.html' not in sys.modules:
                sys.modules['lxml.html'] = types.ModuleType('lxml.html')
            
            # lxml.html.clean modülünü lxml_html_clean ile değiştirin
            sys.modules['lxml.html.clean'] = lxml_html_clean
            
            st.success("lxml.html.clean başarıyla lxml_html_clean ile değiştirildi.")
        except Exception as e:
            st.warning(f"Monkey patch uygulanamadı: {str(e)}")
            
        st.rerun()  # Sayfayı yenile
        
    return True

def ensure_ai_libraries():
    """
    Yapay zeka için gerekli kütüphanelerin yüklü olduğundan emin olur
    """
    required_packages = ['google-generativeai']
    missing_packages = []
    
    # Google GenerativeAI kontrolü
    try:
        import google.generativeai
    except ImportError:
        missing_packages = required_packages
    
    if missing_packages:
        st.warning(f"Yapay zeka özelliği için gerekli kütüphaneler ({', '.join(missing_packages)}) eksik. Yükleniyor...")
        for package in missing_packages:
            try:
                install_package(package)
                # Yükleme sonrası import'u kontrol et
                try:
                    import google.generativeai
                    st.success(f"{package} başarıyla yüklendi ve içe aktarıldı.")
                except ImportError:
                    st.error(f"{package} yüklendi ancak içe aktarılamadı! Python sürümü uyumsuz olabilir.")
                    return False
            except Exception as e:
                st.error(f"{package} yüklenirken hata oluştu: {str(e)}")
                return False
            
        st.info("Kurulum tamamlandı. Sayfa yenileniyor...")
        st.rerun()  # Sayfayı yenile
        
    return True