Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import streamlit as st
|
| 3 |
import PyPDF2
|
| 4 |
import openai
|
| 5 |
from io import BytesIO
|
|
@@ -22,11 +21,11 @@ def register_fonts():
|
|
| 22 |
"""Register fonts for different languages"""
|
| 23 |
try:
|
| 24 |
# Using Noto Nastaliq Urdu for Urdu
|
| 25 |
-
pdfmetrics.registerFont(TTFont('NotoNastaliqUrdu','NafeesNastaleeqXX.ttf'))
|
| 26 |
# Using Noto Naskh Arabic for Arabic
|
| 27 |
-
pdfmetrics.registerFont(TTFont('NotoNaskhArabic','NotoNaskhArabic-Regular.ttf'))
|
| 28 |
# Using Noto Sans for other languages
|
| 29 |
-
pdfmetrics.registerFont(TTFont('NotoSans','NotoSans-Regular.ttf'))
|
| 30 |
except Exception as e:
|
| 31 |
st.warning(f"Font files not found. Default fonts will be used. Error: {str(e)}")
|
| 32 |
|
|
@@ -355,238 +354,4 @@ st.markdown("""
|
|
| 355 |
4. Click 'Translate' to get your translation
|
| 356 |
5. Review the translation
|
| 357 |
6. Click 'Download Translated PDF' to save as PDF
|
| 358 |
-
""")port PyPDF2
|
| 359 |
-
import openai
|
| 360 |
-
from io import BytesIO
|
| 361 |
-
import io
|
| 362 |
-
from reportlab.pdfgen import canvas
|
| 363 |
-
from reportlab.lib.pagesizes import letter, A4
|
| 364 |
-
from reportlab.pdfbase import pdfmetrics
|
| 365 |
-
from reportlab.pdfbase.ttfonts import TTFont
|
| 366 |
-
from reportlab.lib.utils import simpleSplit
|
| 367 |
-
from reportlab.lib.colors import black
|
| 368 |
-
import arabic_reshaper
|
| 369 |
-
from bidi.algorithm import get_display
|
| 370 |
-
import os
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
# Get API key from Hugging Face secrets
|
| 374 |
-
api_key = os.environ.get('OPENAI_API_KEY')
|
| 375 |
-
|
| 376 |
-
def register_fonts():
|
| 377 |
-
"""Register fonts for different languages"""
|
| 378 |
-
try:
|
| 379 |
-
# Using Jameel Noori Nastaleeq for Urdu
|
| 380 |
-
pdfmetrics.registerFont(TTFont('Jameel','Jameel Noori Nastaleeq.ttf'))
|
| 381 |
-
# Using Noto Naskh Arabic for Arabic
|
| 382 |
-
pdfmetrics.registerFont(TTFont('NotoNaskhArabic','NotoNaskhArabic-Regular.ttf'))
|
| 383 |
-
# Using Noto Sans for other languages
|
| 384 |
-
pdfmetrics.registerFont(TTFont('NotoSans','NotoSans-Regular.ttf'))
|
| 385 |
-
except Exception as e:
|
| 386 |
-
st.warning(f"Font files not found. Default fonts will be used. Error: {str(e)}")
|
| 387 |
-
|
| 388 |
-
def extract_text_from_pdf(pdf_file):
|
| 389 |
-
"""Extract text from uploaded PDF file"""
|
| 390 |
-
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 391 |
-
text = ""
|
| 392 |
-
for page in pdf_reader.pages:
|
| 393 |
-
text += page.extract_text()
|
| 394 |
-
return text
|
| 395 |
-
|
| 396 |
-
def create_pdf(text, target_language):
|
| 397 |
-
"""Create a PDF file from text with proper language support"""
|
| 398 |
-
buffer = BytesIO()
|
| 399 |
-
c = canvas.Canvas(buffer, pagesize=A4)
|
| 400 |
-
width, height = A4
|
| 401 |
-
|
| 402 |
-
# Set initial Y position from top
|
| 403 |
-
y = height - 50
|
| 404 |
-
margin = 50
|
| 405 |
-
|
| 406 |
-
# Special handling for Urdu
|
| 407 |
-
if target_language == "Urdu":
|
| 408 |
-
try:
|
| 409 |
-
# Use Jameel Noori Nastaleeq font for Urdu
|
| 410 |
-
c.setFont('Jameel', 16)
|
| 411 |
-
# For Urdu text, don't reshape but apply BIDI
|
| 412 |
-
text = text.strip()
|
| 413 |
-
lines = text.split('\n')
|
| 414 |
-
except:
|
| 415 |
-
c.setFont('Helvetica', 12)
|
| 416 |
-
st.warning("Urdu font not loaded properly. Using default font.")
|
| 417 |
-
elif target_language == "Arabic":
|
| 418 |
-
try:
|
| 419 |
-
c.setFont('NotoNaskhArabic', 14)
|
| 420 |
-
text = arabic_reshaper.reshape(text)
|
| 421 |
-
text = get_display(text)
|
| 422 |
-
lines = text.split('\n')
|
| 423 |
-
except:
|
| 424 |
-
c.setFont('Helvetica', 12)
|
| 425 |
-
lines = text.split('\n')
|
| 426 |
-
else:
|
| 427 |
-
try:
|
| 428 |
-
c.setFont('NotoSans', 12)
|
| 429 |
-
lines = text.split('\n')
|
| 430 |
-
except:
|
| 431 |
-
c.setFont('Helvetica', 12)
|
| 432 |
-
lines = text.split('\n')
|
| 433 |
-
|
| 434 |
-
# Draw text with proper spacing
|
| 435 |
-
line_height = c._fontsize * 2.5 if target_language == "Urdu" else c._fontsize * 1.5
|
| 436 |
-
|
| 437 |
-
for line in lines:
|
| 438 |
-
if y < 50: # If near bottom of page
|
| 439 |
-
c.showPage()
|
| 440 |
-
y = height - 50
|
| 441 |
-
# Reset font for new page
|
| 442 |
-
if target_language == "Urdu":
|
| 443 |
-
c.setFont('Jameel', 16)
|
| 444 |
-
elif target_language == "Arabic":
|
| 445 |
-
c.setFont('NotoNaskhArabic', 14)
|
| 446 |
-
else:
|
| 447 |
-
c.setFont('NotoSans', 12)
|
| 448 |
-
|
| 449 |
-
# Calculate line width for RTL positioning
|
| 450 |
-
line_width = c.stringWidth(line, c._fontname, c._fontsize)
|
| 451 |
-
|
| 452 |
-
# Position text based on language
|
| 453 |
-
if target_language in ['Arabic', 'Urdu']:
|
| 454 |
-
x = width - margin - line_width # Right-aligned
|
| 455 |
-
# For Urdu, we'll write the original text without reshaping
|
| 456 |
-
if target_language == "Urdu":
|
| 457 |
-
c.drawRightString(width - margin, y, line)
|
| 458 |
-
else:
|
| 459 |
-
c.drawString(x, y, line)
|
| 460 |
-
else:
|
| 461 |
-
x = margin # Left-aligned
|
| 462 |
-
c.drawString(x, y, line)
|
| 463 |
-
|
| 464 |
-
y -= line_height
|
| 465 |
-
|
| 466 |
-
c.save()
|
| 467 |
-
buffer.seek(0)
|
| 468 |
-
return buffer
|
| 469 |
-
|
| 470 |
-
def translate_text(text, target_language,api_key):
|
| 471 |
-
"""Translate text using OpenAI API with improved prompting"""
|
| 472 |
-
try:
|
| 473 |
-
client = openai.OpenAI(api_key=api_key)
|
| 474 |
-
|
| 475 |
-
# Enhanced prompt for better translation
|
| 476 |
-
system_prompt = f"""You are a professional translator specializing in {target_language}.
|
| 477 |
-
Translate the following text to {target_language}, ensuring:
|
| 478 |
-
1. Technical terms are accurately translated
|
| 479 |
-
2. Maintain formal language and proper grammar
|
| 480 |
-
3. Preserve formatting and structure
|
| 481 |
-
4. Keep proper nouns and technical terms like 'AI', 'LLMs', 'Python' in English where appropriate
|
| 482 |
-
5. Use culturally appropriate expressions
|
| 483 |
-
6. For Urdu/Arabic, ensure proper character connections and diacritics
|
| 484 |
-
7. Maintain professional and accurate technical translations
|
| 485 |
-
"""
|
| 486 |
-
|
| 487 |
-
response = client.chat.completions.create(
|
| 488 |
-
model="gpt-3.5-turbo",
|
| 489 |
-
messages=[
|
| 490 |
-
{"role": "system", "content": system_prompt},
|
| 491 |
-
{"role": "user", "content": text}
|
| 492 |
-
],
|
| 493 |
-
temperature=0.3
|
| 494 |
-
)
|
| 495 |
-
return response.choices[0].message.content
|
| 496 |
-
except Exception as e:
|
| 497 |
-
return f"Translation error: {str(e)}"
|
| 498 |
-
|
| 499 |
-
# Set page config
|
| 500 |
-
st.set_page_config(page_title="PDF Translator", layout="wide")
|
| 501 |
-
|
| 502 |
-
# Try to register fonts at startup
|
| 503 |
-
register_fonts()
|
| 504 |
-
|
| 505 |
-
# Main app interface
|
| 506 |
-
st.title("PDF Document Translator")
|
| 507 |
-
|
| 508 |
-
# Add custom CSS for better text display
|
| 509 |
-
st.markdown("""
|
| 510 |
-
<style>
|
| 511 |
-
.stTextArea textarea {
|
| 512 |
-
font-size: 16px !important;
|
| 513 |
-
}
|
| 514 |
-
</style>
|
| 515 |
-
""", unsafe_allow_html=True)
|
| 516 |
-
|
| 517 |
-
# # API Key input with better security
|
| 518 |
-
# api_key = st.text_input("Enter your OpenAI API Key", type="password")
|
| 519 |
-
|
| 520 |
-
# Language selection
|
| 521 |
-
languages = {
|
| 522 |
-
"English": "English",
|
| 523 |
-
"Urdu": "Urdu",
|
| 524 |
-
"Arabic": "Arabic",
|
| 525 |
-
"Roman English": "Roman English",
|
| 526 |
-
"Roman Urdu": "Roman Urdu",
|
| 527 |
-
"Hindi": "Hindi",
|
| 528 |
-
"Spanish": "Spanish",
|
| 529 |
-
"French": "French"
|
| 530 |
-
}
|
| 531 |
-
|
| 532 |
-
# File uploader
|
| 533 |
-
uploaded_file = st.file_uploader("Upload your PDF file", type="pdf")
|
| 534 |
-
|
| 535 |
-
# Language selector
|
| 536 |
-
target_language = st.selectbox(
|
| 537 |
-
"Select target language",
|
| 538 |
-
options=list(languages.keys())
|
| 539 |
-
)
|
| 540 |
-
|
| 541 |
-
# Create two columns for original and translated text
|
| 542 |
-
col1, col2 = st.columns(2)
|
| 543 |
-
|
| 544 |
-
if uploaded_file is not None and api_key:
|
| 545 |
-
# Extract text from PDF
|
| 546 |
-
with st.spinner("Extracting text from PDF..."):
|
| 547 |
-
text = extract_text_from_pdf(uploaded_file)
|
| 548 |
-
|
| 549 |
-
# Show original text
|
| 550 |
-
with col1:
|
| 551 |
-
st.subheader("Original Text")
|
| 552 |
-
st.text_area("", value=text, height=400, key="original_text")
|
| 553 |
-
|
| 554 |
-
# Initialize session state for translated text
|
| 555 |
-
if 'translated_text' not in st.session_state:
|
| 556 |
-
st.session_state.translated_text = None
|
| 557 |
-
|
| 558 |
-
# Translate button
|
| 559 |
-
if st.button("Translate"):
|
| 560 |
-
with st.spinner("Translating..."):
|
| 561 |
-
translated_text = translate_text(text, languages[target_language], api_key)
|
| 562 |
-
st.session_state.translated_text = translated_text
|
| 563 |
-
|
| 564 |
-
# Show translated text
|
| 565 |
-
with col2:
|
| 566 |
-
st.subheader(f"Translated Text ({target_language})")
|
| 567 |
-
st.text_area("", value=translated_text, height=400, key="translated_text")
|
| 568 |
-
|
| 569 |
-
# Show download button if translation exists
|
| 570 |
-
if st.session_state.translated_text:
|
| 571 |
-
# Create PDF button
|
| 572 |
-
if st.download_button(
|
| 573 |
-
label="Download Translated PDF",
|
| 574 |
-
data=create_pdf(st.session_state.translated_text, target_language),
|
| 575 |
-
file_name=f"translated_{target_language}.pdf",
|
| 576 |
-
mime="application/pdf"
|
| 577 |
-
):
|
| 578 |
-
st.success("PDF downloaded successfully!")
|
| 579 |
-
|
| 580 |
-
elif not api_key:
|
| 581 |
-
st.warning("Please enter your OpenAI API key to proceed.")
|
| 582 |
-
|
| 583 |
-
# Add instructions and notes
|
| 584 |
-
st.markdown("""
|
| 585 |
-
### Instructions:
|
| 586 |
-
1. Upload your PDF file
|
| 587 |
-
2. Select your target language
|
| 588 |
-
3. Click 'Translate' to get your translation
|
| 589 |
-
4. Review the translation
|
| 590 |
-
5. Click 'Download Translated PDF' to save as PDF
|
| 591 |
-
|
| 592 |
""")
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import PyPDF2
|
| 3 |
import openai
|
| 4 |
from io import BytesIO
|
|
|
|
| 21 |
"""Register fonts for different languages"""
|
| 22 |
try:
|
| 23 |
# Using Noto Nastaliq Urdu for Urdu
|
| 24 |
+
pdfmetrics.registerFont(TTFont('NotoNastaliqUrdu', 'fonts/NafeesNastaleeqXX.ttf'))
|
| 25 |
# Using Noto Naskh Arabic for Arabic
|
| 26 |
+
pdfmetrics.registerFont(TTFont('NotoNaskhArabic', 'fonts/NotoNaskhArabic-Regular.ttf'))
|
| 27 |
# Using Noto Sans for other languages
|
| 28 |
+
pdfmetrics.registerFont(TTFont('NotoSans', 'fonts/NotoSans-Regular.ttf'))
|
| 29 |
except Exception as e:
|
| 30 |
st.warning(f"Font files not found. Default fonts will be used. Error: {str(e)}")
|
| 31 |
|
|
|
|
| 354 |
4. Click 'Translate' to get your translation
|
| 355 |
5. Review the translation
|
| 356 |
6. Click 'Download Translated PDF' to save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