File size: 11,969 Bytes
3ec6f9a
 
 
 
 
 
 
 
 
e7fdc76
 
 
 
 
 
 
 
 
 
 
 
 
 
89b36cc
e7fdc76
89b36cc
e7fdc76
89b36cc
e7fdc76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ec6f9a
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import streamlit as st
import PyPDF2
import openai
from io import BytesIO
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter, A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from weasyprint import HTML, CSS
from weasyprint.text.fonts import FontConfiguration
import arabic_reshaper
from bidi.algorithm import get_display
import os
import tempfile

# Get API key from Hugging Face secrets
api_key = os.environ.get('OPENAI_API_KEY')

def register_fonts():
    """Register fonts for different languages"""
    try:
        # Using Noto Nastaliq Urdu for Urdu
        pdfmetrics.registerFont(TTFont('NotoNastaliqUrdu', 'NafeesNastaleeqXX.ttf'))
        # Using Noto Naskh Arabic for Arabic
        pdfmetrics.registerFont(TTFont('NotoNaskhArabic', 'NotoNaskhArabic-Regular.ttf'))
        # Using Noto Sans for other languages
        pdfmetrics.registerFont(TTFont('NotoSans', 'NotoSans-Regular.ttf'))
    except Exception as e:
        st.warning(f"Font files not found. Default fonts will be used. Error: {str(e)}")

def extract_text_from_pdf(pdf_file):
    """Extract text from uploaded PDF file"""
    pdf_reader = PyPDF2.PdfReader(pdf_file)
    text = ""
    for page in pdf_reader.pages:
        text += page.extract_text()
    return text

def create_pdf(text, target_language):
    if target_language == "Urdu":
        font_config = FontConfiguration()
        
        # Process text to handle English and numbers differently
        processed_lines = []
        for line in text.split('\n'):
            # Split line into Urdu and non-Urdu parts
            processed_line = ""
            current_text = ""
            is_urdu = True
            
            for char in line:
                if '\u0600' <= char <= '\u06FF' or char in ['۔', '،']:  # Urdu character range
                    if not is_urdu:
                        if current_text:
                            processed_line += f'<span class="latin">{current_text}</span>'
                        current_text = ""
                        is_urdu = True
                    current_text += char
                else:
                    if is_urdu:
                        if current_text:
                            processed_line += current_text
                        current_text = ""
                        is_urdu = False
                    current_text += char
            
            if current_text:
                if is_urdu:
                    processed_line += current_text
                else:
                    processed_line += f'<span class="latin">{current_text}</span>'
            
            processed_lines.append(f'<p class="urdu-text">{processed_line}</p>')
        
        processed_text = '\n'.join(processed_lines)
        
        html_content = f"""
        <!DOCTYPE html>
        <html dir="rtl" lang="ur">
        <head>
            <meta charset="UTF-8">
            <style>
                @font-face {{
                    font-family: 'NotoNastaliqUrdu';
                    src: url('fonts/NotoNastaliqUrdu-Regular.ttf') format('truetype');
                    font-weight: normal;
                    font-style: normal;
                }}
                
                @page {{
                    size: A4;
                    margin: 3cm 2.5cm;
                }}
                
                body {{
                    font-family: 'NotoNastaliqUrdu', serif;
                    font-size: 16pt;
                    line-height: 3;
                    margin: 0;
                    padding: 0;
                    direction: rtl;
                    text-align: right;
                    text-rendering: optimizeLegibility;
                    -webkit-font-smoothing: antialiased;
                }}
                
                .content {{
                    width: 100%;
                    max-width: 18cm;
                    margin: 0 auto;
                }}
                
                .urdu-text {{
                    margin: 0 0 2em 0;
                    padding: 0;
                    text-align: right;
                    white-space: pre-wrap;
                    word-wrap: break-word;
                    font-feature-settings: "kern", "liga", "calt";
                    letter-spacing: 0.02em;
                }}
                
                .latin {{
                    font-family: Arial, sans-serif;
                    direction: ltr;
                    unicode-bidi: embed;
                    font-size: 14pt;
                }}
                
                /* Improve spacing around punctuation */
                .urdu-text::after {{
                    content: "";
                    display: block;
                    height: 1.5em;
                }}
            </style>
        </head>
        <body>
            <div class="content">
                {processed_text}
            </div>
        </body>
        </html>
        """
        
        # Create a temporary HTML file
        with tempfile.NamedTemporaryFile(suffix='.html', mode='w', encoding='utf-8', delete=False) as f:
            f.write(html_content)
            temp_html = f.name

        # Convert HTML to PDF using WeasyPrint with improved settings
        buffer = BytesIO()
        HTML(temp_html).write_pdf(
            buffer,
            font_config=font_config,
            stylesheets=[CSS(string='''
                @page { 
                    size: A4; 
                    margin: 3cm 2.5cm;
                    @top-right {
                        content: "";
                        margin: 1cm 0;
                    }
                    @bottom-center {
                        content: counter(page);
                        font-family: Arial, sans-serif;
                    }
                }
            ''')]
        )
        buffer.seek(0)
        
        # Clean up temporary file
        os.unlink(temp_html)
        
        return buffer

    else:
        # Use ReportLab for other languages
        buffer = BytesIO()
        c = canvas.Canvas(buffer, pagesize=A4)
        width, height = A4
        y = height - 50
        margin = 50
        
        if target_language == "Arabic":
            try:
                c.setFont('NotoNaskhArabic', 14)
                text = arabic_reshaper.reshape(text)
                text = get_display(text)
                lines = text.split('\n')
                line_height = c._fontsize * 1.5
                
                for line in lines:
                    if y < 50:
                        c.showPage()
                        y = height - 50
                        c.setFont('NotoNaskhArabic', 14)
                    
                    line_width = c.stringWidth(line, c._fontname, c._fontsize)
                    x = width - margin - line_width
                    c.drawString(x, y, line)
                    y -= line_height
                    
            except Exception as e:
                st.warning(f"Arabic rendering error: {str(e)}")
                c.setFont('Helvetica', 12)
        else:
            try:
                c.setFont('NotoSans', 12)
                lines = text.split('\n')
                line_height = c._fontsize * 1.5
                
                for line in lines:
                    if y < 50:
                        c.showPage()
                        y = height - 50
                        c.setFont('NotoSans', 12)
                    
                    c.drawString(margin, y, line)
                    y -= line_height
                    
            except Exception as e:
                st.warning(f"Text rendering error: {str(e)}")
                c.setFont('Helvetica', 12)

        c.save()
        buffer.seek(0)
        return buffer

def translate_text(text, target_language, api_key):
    """Translate text using OpenAI API with improved prompting"""
    try:
        client = openai.OpenAI(api_key=api_key)
        
        # Enhanced prompt for better translation
        system_prompt = f"""You are a professional translator specializing in {target_language}. 
        Translate the following text to {target_language}, ensuring:
        1. Technical terms are accurately translated
        2. Maintain formal language and proper grammar
        3. Preserve formatting and structure
        4. Keep proper nouns and technical terms like 'AI', 'LLMs', 'Python' in English where appropriate
        5. Use culturally appropriate expressions
        6. For Urdu/Arabic, ensure proper character connections and diacritics
        7. Maintain professional and accurate technical translations
        8. Preserve line breaks and paragraph structure
        """
        
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": text}
            ],
            temperature=0.3
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"Translation error: {str(e)}"

# Set page config
st.set_page_config(page_title="PDF Translator", layout="wide")

# Try to register fonts at startup
register_fonts()

# Main app interface
st.title("PDF Document Translator")

# Add custom CSS for better text display
st.markdown("""
<style>
    .stTextArea textarea {
        font-size: 16px !important;
    }
</style>
""", unsafe_allow_html=True)

# Language selection
languages = {
    "English": "English",
    "Urdu": "Urdu",
    "Arabic": "Arabic",
    "Roman English": "Roman English",
    "Roman Urdu": "Roman Urdu",
    "Hindi": "Hindi",
    "Spanish": "Spanish",
    "French": "French"
}

# File uploader
uploaded_file = st.file_uploader("Upload your PDF file", type="pdf")

# API Key input field
api_key_input = st.text_input("Enter OpenAI API Key:", type="password", key="api_key_input")
if api_key_input:
    api_key = api_key_input

# Language selector
target_language = st.selectbox(
    "Select target language",
    options=list(languages.keys())
)

# Create two columns for original and translated text
col1, col2 = st.columns(2)

if uploaded_file is not None and api_key:
    # Extract text from PDF
    with st.spinner("Extracting text from PDF..."):
        text = extract_text_from_pdf(uploaded_file)
    
    # Show original text
    with col1:
        st.subheader("Original Text")
        st.text_area("", value=text, height=400, key="original_text")
    
    # Initialize session state for translated text
    if 'translated_text' not in st.session_state:
        st.session_state.translated_text = None
    
    # Translate button
    if st.button("Translate"):
        with st.spinner("Translating..."):
            translated_text = translate_text(text, languages[target_language], api_key)
            st.session_state.translated_text = translated_text
        
        # Show translated text
        with col2:
            st.subheader(f"Translated Text ({target_language})")
            st.text_area("", value=translated_text, height=400, key="translated_text")
    
    # Show download button if translation exists
    if st.session_state.translated_text:
        # Create PDF button
        if st.download_button(
            label="Download Translated PDF",
            data=create_pdf(st.session_state.translated_text, target_language),
            file_name=f"translated_{target_language}.pdf",
            mime="application/pdf"
        ):
            st.success("PDF downloaded successfully!")

elif not api_key:
    st.warning("Please enter your OpenAI API key to proceed.")

# Add instructions and notes
st.markdown("""
### Instructions:
1. Enter your OpenAI API key
2. Upload your PDF file
3. Select your target language
4. Click 'Translate' to get your translation
5. Review the translation
6. Click 'Download Translated PDF' to save as PDF
""")