1+ import os
12import gradio as gr
23from core .anonymizer import anonymize_text
34import tempfile
45import json
5-
66from core .profile_config import PROFILES
77
8- def anonymize_interface ( file_obj , profile ):
9- if file_obj is None :
10- return "Proszę wgrać plik." , "Brak wyników." , None
8+ # Wyłącz analytics
9+ os . environ [ 'GRADIO_ANALYTICS_ENABLED' ] = 'False'
10+ os . environ [ 'HF_HUB_OFFLINE' ] = '1'
1111
12- original_text = file_obj .decode ('utf-8' )
12+ class AnonymizerInterface :
13+ """Wrapper dla interfejsu anonimizatora"""
1314
14- anonymized_text , substitution_map = anonymize_text (original_text , profile )
15+ @staticmethod
16+ def create_temp_file (content , filename , extension ):
17+ """Tworzy tymczasowy plik z contentem"""
18+ if not content :
19+ return None
20+
21+ with tempfile .NamedTemporaryFile (
22+ delete = False ,
23+ mode = "w" ,
24+ suffix = extension ,
25+ prefix = filename + "_" ,
26+ encoding = "utf-8"
27+ ) as tmp :
28+ if extension == ".json" :
29+ json .dump (content , tmp , ensure_ascii = False , indent = 2 )
30+ else :
31+ tmp .write (content )
32+ return tmp .name
1533
16- # Tworzenie pliku do pobrania
17- if substitution_map :
18- with tempfile .NamedTemporaryFile (delete = False , mode = "w" , suffix = ".json" , encoding = "utf-8" ) as tmp :
19- json .dump (substitution_map , tmp , ensure_ascii = False , indent = 2 )
20- map_file_path = tmp .name
21- else :
22- map_file_path = None
23-
24- return anonymized_text , substitution_map , map_file_path
34+ @staticmethod
35+ def process_file (file_obj , profile ):
36+ """Przetwarza plik i zwraca wyniki anonimizacji"""
37+ if file_obj is None :
38+ return "Proszę wgrać plik." , "Brak wyników." , None , None
39+
40+ original_text = file_obj .decode ('utf-8' )
41+ anonymized_text , substitution_map = anonymize_text (original_text , profile )
42+
43+ # Tworzenie plików do pobrania
44+ map_file_path = AnonymizerInterface .create_temp_file (
45+ substitution_map ,
46+ "mapowania" ,
47+ ".json"
48+ )
49+
50+ text_file_path = AnonymizerInterface .create_temp_file (
51+ anonymized_text ,
52+ "tekst_anonimizowany" ,
53+ ".txt"
54+ )
55+
56+ return anonymized_text , substitution_map , map_file_path , text_file_path
2557
26- def launch ():
27- with gr .Blocks () as demo :
28- gr .Markdown ("# Anonimizator Umów Notarialnych" )
58+ def create_ui ():
59+ """Tworzy interfejs użytkownika"""
60+ with gr .Blocks (
61+ css = """
62+ * {
63+ font-family: system-ui, -apple-system, BlinkMacSystemFont, sans-serif !important;
64+ }
65+ link[rel="manifest"] { display: none !important; }
66+ """ ,
67+ theme = gr .themes .Soft ()
68+ ) as demo :
2969
70+ gr .Markdown ("# Janusz Danych Rodo - Anonimizator Umów Notarialnych" )
71+
72+ # Wiersz 1: Wgranie pliku, profil, przycisk
73+ with gr .Row ():
74+ file_input = gr .File (
75+ label = "Wgraj plik (.txt, .md)" ,
76+ type = "binary" ,
77+ file_count = "single"
78+ )
79+ profile_dropdown = gr .Dropdown (
80+ choices = list (PROFILES .keys ()),
81+ value = "pseudonymized" ,
82+ label = "Profil Anonimizacji"
83+ )
84+ submit_btn = gr .Button ("Anonimizuj" )
85+
86+ # Wiersz 2: Tekst zanonimizowany
87+ with gr .Row ():
88+ output_text = gr .Textbox (
89+ label = "Tekst zanonimizowany" ,
90+ lines = 15 ,
91+ interactive = True
92+ )
93+
94+ # Wiersz 3: Słownik mapowań i pobieranie
3095 with gr .Row ():
3196 with gr .Column ():
32- file_input = gr .File (label = "Wgraj plik (.txt, .md)" , type = "binary" )
33- profile_dropdown = gr .Dropdown (
34- choices = list (PROFILES .keys ()),
35- value = "pseudonymized" ,
36- label = "Profil Anonimizacji"
37- )
38- submit_btn = gr .Button ("Anonimizuj" )
97+ output_map = gr .JSON (label = "Słownik mapowań" )
3998
4099 with gr .Column ():
41- output_text = gr .Textbox (label = "Tekst zanonimizowany" , lines = 15 )
42- output_map = gr .JSON (label = "Słownik mapowań" )
43- download_map_btn = gr .File (label = "Pobierz słownik mapowań" )
44-
100+ gr .Markdown ("### Pobierz pliki" )
101+ with gr .Group ():
102+ download_map_btn = gr .File (label = "Słownik mapowań" )
103+ download_text_btn = gr .File (label = "Tekst anonimizowany" )
104+
45105 submit_btn .click (
46- fn = anonymize_interface ,
106+ fn = AnonymizerInterface . process_file ,
47107 inputs = [file_input , profile_dropdown ],
48- outputs = [output_text , output_map , download_map_btn ]
108+ outputs = [output_text , output_map , download_map_btn , download_text_btn ]
49109 )
110+
111+ return demo
112+
113+ def launch ():
114+ """Uruchamia aplikację"""
115+ print ("Working directory:" , os .getcwd ())
116+ print ("Static dir exists:" , os .path .exists ("static" ))
117+ print ("Manifest exists:" , os .path .exists ("static/manifest.json" ))
118+ print (gr .__version__ )
119+ print (hasattr (gr , 'set_static_paths' ))
50120
51- demo .launch ()
121+ demo = create_ui ()
122+ demo .launch (
123+ share = False ,
124+ server_name = "127.0.0.1" ,
125+ server_port = 7860 ,
126+ inbrowser = True ,
127+ quiet = True ,
128+ show_error = True ,
129+ favicon_path = None ,
130+ )
52131
53132if __name__ == "__main__" :
54133 launch ()
0 commit comments