ebhon commited on
Commit
20cffcd
·
verified ·
1 Parent(s): 6aef610

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -138
app.py CHANGED
@@ -1,139 +1,139 @@
1
- import streamlit as st
2
- import os
3
- from PIL import Image
4
- import torch
5
- from manga_translator import process_manga_pages
6
- import tempfile
7
- from dotenv import load_dotenv
8
-
9
- # Load environment variables
10
- load_dotenv()
11
-
12
- # Check for DeepL API key
13
- DEEPL_API_KEY = os.getenv('DEEPL_API_KEY')
14
- if not DEEPL_API_KEY:
15
- st.warning("⚠️ DeepL API key not found. Using free translation service which may have limitations.")
16
-
17
- st.set_page_config(
18
- page_title="Manga Translator",
19
- page_icon="📚",
20
- layout="wide"
21
- )
22
-
23
- st.title("🎯 Manga Translator")
24
-
25
- # Main description
26
- st.markdown("""
27
- This app uses custom YOLO detection, OCR, and machine translation to automatically translate manga pages from Japanese to English.
28
-
29
- ⚠️ **Note**: Translation accuracy may vary due to the complexity of Japanese text and manga-specific expressions.
30
- We're continuously working to improve the system!
31
- """)
32
-
33
- # Guidelines section before upload
34
- st.markdown("""
35
- ### 📋 Guidelines for Best Results
36
- 1. **Image Requirements**:
37
- - Clear, high-resolution manga page
38
- - Japanese text should be clearly visible
39
- - Text bubbles should not be cropped
40
- - Supported formats: PNG, JPG, JPEG
41
-
42
- 2. **For Best Results**:
43
- - Avoid pages with handwritten text
44
- - Ensure text bubbles are not overlapping
45
- - Image should be properly oriented
46
- - Avoid heavily compressed images
47
-
48
- 3. **Privacy & Copyright**:
49
- - Only upload content you have rights to use
50
- - We don't store any uploaded images
51
- - All processing is done in real-time
52
- """)
53
-
54
- # Technical demonstration
55
- st.markdown("""
56
- ### 🔍 How It Works
57
- 1. **Text Detection**: Custom YOLO model detects text bubbles
58
- 2. **OCR Processing**: Extracts Japanese text
59
- 3. **Translation**: Converts to English using DeepL API
60
- 4. **Text Insertion**: Places translated text back into the image
61
- """)
62
-
63
- # File uploader
64
- uploaded_file = st.file_uploader("Choose a manga page (PNG, JPG)", type=['png', 'jpg', 'jpeg'])
65
-
66
- if uploaded_file is not None:
67
- # Create temporary directories for processing
68
- with tempfile.TemporaryDirectory() as temp_dir:
69
- # Save uploaded file
70
- input_path = os.path.join(temp_dir, "input")
71
- output_path = os.path.join(temp_dir, "output")
72
- os.makedirs(input_path, exist_ok=True)
73
- os.makedirs(output_path, exist_ok=True)
74
-
75
- temp_file = os.path.join(input_path, uploaded_file.name)
76
- with open(temp_file, "wb") as f:
77
- f.write(uploaded_file.getbuffer())
78
-
79
- # Process the image
80
- with st.spinner("Translating manga... Please wait."):
81
- try:
82
- process_manga_pages(
83
- image_folder=input_path,
84
- translated_dir=output_path,
85
- show_results=False
86
- )
87
-
88
- # Display results
89
- col1, col2 = st.columns(2)
90
-
91
- with col1:
92
- st.subheader("Original")
93
- original_image = Image.open(temp_file)
94
- st.image(original_image, use_column_width=True)
95
-
96
- with col2:
97
- st.subheader("Translated")
98
- translated_file = os.path.join(output_path, uploaded_file.name)
99
- if os.path.exists(translated_file):
100
- translated_image = Image.open(translated_file)
101
- st.image(translated_image, use_column_width=True)
102
- else:
103
- st.error("Translation failed. Please try another image.")
104
-
105
- except Exception as e:
106
- st.error(f"An error occurred: {str(e)}")
107
-
108
- # Add information about the project
109
- st.markdown("""
110
- ---
111
- ## About the Project
112
-
113
- This manga translator uses:
114
- - Custom-trained YOLO model for text bubble detection
115
- - MangaOCR for Japanese text extraction
116
- - DeepL API for high-quality translation
117
- - OpenCV and PIL for image processing
118
-
119
- ### Technical Details
120
- - Supports single page translation
121
- - Real-time processing
122
- - No image storage
123
- - Privacy-focused design
124
-
125
- ### Links
126
- - [GitHub Repository](https://github.com/ebhon/manga_translator_project)
127
- - [YOLO Model Repository](https://github.com/ebhon/YOLO-manga-bubble-detector)
128
-
129
- ### Feedback
130
- Found a bug or have a suggestion? Please open an issue on GitHub!
131
- """)
132
-
133
- # Add footer with disclaimer
134
- st.markdown("""
135
- ---
136
- Created by [Handwitanto Abraham](https://www.linkedin.com/in/handwitanto-abraham/)
137
-
138
- *Disclaimer: This tool is for educational purposes only. Users are responsible for ensuring they have the right to translate any content they upload.*
139
  """)
 
1
+ import streamlit as st
2
+ import os
3
+ from PIL import Image
4
+ import torch
5
+ from manga_translator import process_manga_pages
6
+ import tempfile
7
+ from dotenv import load_dotenv
8
+
9
+ # Load environment variables
10
+ load_dotenv()
11
+
12
+ # Check for DeepL API key
13
+ DEEPL_API_KEY = os.getenv('DEEPL_API_KEY')
14
+ if not DEEPL_API_KEY:
15
+ st.warning("⚠️ DeepL API key not found. Using free translation service which may have limitations.")
16
+
17
+ st.set_page_config(
18
+ page_title="Manga Translator",
19
+ page_icon="📚",
20
+ layout="wide"
21
+ )
22
+
23
+ st.title("🎯 Manga Translator")
24
+
25
+ # Main description
26
+ st.markdown("""
27
+ This app uses custom YOLO detection, OCR, and machine translation to automatically translate manga pages from Japanese to English.
28
+
29
+ ⚠️ **Note**: Translation accuracy may vary due to the complexity of Japanese text and manga-specific expressions.
30
+ We're continuously working to improve the system!
31
+ """)
32
+
33
+ # Guidelines section before upload
34
+ st.markdown("""
35
+ ### 📋 Guidelines for Best Results
36
+ 1. **Image Requirements**:
37
+ - Clear, high-resolution manga page
38
+ - Japanese text should be clearly visible
39
+ - Text bubbles should not be cropped
40
+ - Supported formats: PNG, JPG, JPEG
41
+
42
+ 2. **For Best Results**:
43
+ - Avoid pages with handwritten text
44
+ - Ensure text bubbles are not overlapping
45
+ - Image should be properly oriented
46
+ - Avoid heavily compressed images
47
+
48
+ 3. **Privacy & Copyright**:
49
+ - Only upload content you have rights to use
50
+ - We don't store any uploaded images
51
+ - All processing is done in real-time
52
+ """)
53
+
54
+ # Technical demonstration
55
+ st.markdown("""
56
+ ### 🔍 How It Works
57
+ 1. **Text Detection**: Custom YOLO model detects text bubbles
58
+ 2. **OCR Processing**: Extracts Japanese text
59
+ 3. **Translation**: Converts to English using DeepL API
60
+ 4. **Text Insertion**: Places translated text back into the image
61
+ """)
62
+
63
+ # File uploader
64
+ uploaded_file = st.file_uploader("Choose a manga page (PNG, JPG)", type=['png', 'jpg', 'jpeg'])
65
+
66
+ if uploaded_file is not None:
67
+ # Create temporary directories for processing
68
+ with tempfile.TemporaryDirectory() as temp_dir:
69
+ # Save uploaded file
70
+ input_path = os.path.join(temp_dir, "input")
71
+ output_path = os.path.join(temp_dir, "output")
72
+ os.makedirs(input_path, exist_ok=True)
73
+ os.makedirs(output_path, exist_ok=True)
74
+
75
+ temp_file = os.path.join(input_path, uploaded_file.name)
76
+ with open(temp_file, "wb") as f:
77
+ f.write(uploaded_file.getbuffer())
78
+
79
+ # Process the image
80
+ with st.spinner("Translating manga... Please wait."):
81
+ try:
82
+ process_manga_pages(
83
+ image_folder=input_path,
84
+ translated_dir=output_path,
85
+ show_results=False
86
+ )
87
+
88
+ # Display results
89
+ col1, col2 = st.columns(2)
90
+
91
+ with col1:
92
+ st.subheader("Original")
93
+ original_image = Image.open(temp_file)
94
+ st.image(original_image, use_container_width=True)
95
+
96
+ with col2:
97
+ st.subheader("Translated")
98
+ translated_file = os.path.join(output_path, uploaded_file.name)
99
+ if os.path.exists(translated_file):
100
+ translated_image = Image.open(translated_file)
101
+ st.image(translated_image, use_container_width=True)
102
+ else:
103
+ st.error("Translation failed. Please try another image.")
104
+
105
+ except Exception as e:
106
+ st.error(f"An error occurred: {str(e)}")
107
+
108
+ # Add information about the project
109
+ st.markdown("""
110
+ ---
111
+ ## About the Project
112
+
113
+ This manga translator uses:
114
+ - Custom-trained YOLO model for text bubble detection
115
+ - MangaOCR for Japanese text extraction
116
+ - DeepL API for high-quality translation
117
+ - OpenCV and PIL for image processing
118
+
119
+ ### Technical Details
120
+ - Supports single page translation
121
+ - Real-time processing
122
+ - No image storage
123
+ - Privacy-focused design
124
+
125
+ ### Links
126
+ - [GitHub Repository](https://github.com/ebhon/manga_translator_project)
127
+ - [YOLO Model Repository](https://github.com/ebhon/YOLO-manga-bubble-detector)
128
+
129
+ ### Feedback
130
+ Found a bug or have a suggestion? Please open an issue on GitHub!
131
+ """)
132
+
133
+ # Add footer with disclaimer
134
+ st.markdown("""
135
+ ---
136
+ Created by Abraham
137
+
138
+ *Disclaimer: This tool is for educational purposes only. Users are responsible for ensuring they have the right to translate any content they upload.*
139
  """)