Krixter commited on
Commit
1be134f
·
verified ·
1 Parent(s): 4b2748d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -82
app.py CHANGED
@@ -1,82 +1,17 @@
1
- import gradio as gr
2
- import tempfile
3
- import os
4
-
5
- # ----------------------------
6
- # Load replacement dictionary
7
- # ----------------------------
8
- def load_dictionary(dictionary_file):
9
- replacements = []
10
-
11
- with open(dictionary_file.name, "r", encoding="utf-8") as f:
12
- for line in f:
13
- line = line.strip()
14
-
15
- if not line:
16
- continue
17
-
18
- if "->" not in line:
19
- continue
20
-
21
- wrong, correct = line.split("->", 1)
22
-
23
- wrong = wrong.strip()
24
- correct = correct.strip()
25
-
26
- replacements.append((wrong, correct))
27
-
28
- # Longest words first
29
- replacements.sort(key=lambda x: len(x[0]), reverse=True)
30
-
31
- return replacements
32
-
33
-
34
- # ----------------------------
35
- # Replace text
36
- # ----------------------------
37
- def correct_text(txt_file, dictionary_file):
38
-
39
- replacements = load_dictionary(dictionary_file)
40
-
41
- with open(txt_file.name, "r", encoding="utf-8") as f:
42
- text = f.read()
43
-
44
- total = 0
45
-
46
- for wrong, correct in replacements:
47
-
48
- count = text.count(wrong)
49
-
50
- if count:
51
- text = text.replace(wrong, correct)
52
- total += count
53
-
54
- output_path = os.path.join(
55
- tempfile.gettempdir(),
56
- "corrected_text.txt"
57
- )
58
-
59
- with open(output_path, "w", encoding="utf-8") as f:
60
- f.write(text)
61
-
62
- return output_path, f"Done!\n\nTotal replacements: {total}"
63
-
64
-
65
- # ----------------------------
66
- # Interface
67
- # ----------------------------
68
- demo = gr.Interface(
69
- fn=correct_text,
70
- inputs=[
71
- gr.File(label="Upload TXT File"),
72
- gr.File(label="Upload Correction Dictionary"),
73
- ],
74
- outputs=[
75
- gr.File(label="Download Corrected TXT"),
76
- gr.Textbox(label="Status"),
77
- ],
78
- title="Novel Word Corrector",
79
- description="Upload a TXT file and a dictionary in the format 'wrong -> correct'."
80
- )
81
-
82
- demo.launch()
 
1
+ 📂 Upload Books
2
+
3
+
4
+ Dimensional_Codex_System_Im_really_not_a_cultist
5
+ .epub
6
+ 1.2 MB ⇣
7
+ ×
8
+ Food_Randomly_set_up_a_stall_customers_chase_me_ten_blocks_away
9
+ .epub
10
+ 505.4 KB ⇣
11
+ ×
12
+ Doomsday_Pillars (1)
13
+ .epub
14
+ 1.1 MB ⇣
15
+ ×
16
+ Surviving_the_Zombie_Apocalypse_With_My_Military_System
17
+ .epub