Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import chardet
|
| 2 |
+
import fitz # PyMuPDF for PDFs
|
| 3 |
+
from docx import Document # For DOCX files
|
| 4 |
+
from flask import Flask, render_template, request
|
| 5 |
+
from googletrans import Translator, LANGUAGES
|
| 6 |
+
from striprtf.striprtf import rtf_to_text # Convert RTF to plain text
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
translator = Translator()
|
| 10 |
+
|
| 11 |
+
def detect_encoding(file):
|
| 12 |
+
"""Detects the encoding of a given file."""
|
| 13 |
+
raw_data = file.read()
|
| 14 |
+
result = chardet.detect(raw_data)
|
| 15 |
+
encoding = result.get('encoding') or 'utf-8' # Default to UTF-8 if detection fails
|
| 16 |
+
file.seek(0) # Reset file pointer for reading
|
| 17 |
+
return encoding
|
| 18 |
+
|
| 19 |
+
def extract_text(file, filename, encoding):
|
| 20 |
+
"""Extracts plain text from TXT, RTF, DOCX, and PDF files."""
|
| 21 |
+
if filename.endswith(".txt"):
|
| 22 |
+
return file.read().decode(encoding, errors="replace").strip()
|
| 23 |
+
|
| 24 |
+
elif filename.endswith(".rtf"):
|
| 25 |
+
contents = file.read().decode(encoding, errors="replace").strip()
|
| 26 |
+
return rtf_to_text(contents)
|
| 27 |
+
|
| 28 |
+
elif filename.endswith(".docx"):
|
| 29 |
+
doc = Document(file)
|
| 30 |
+
return "\n".join([para.text for para in doc.paragraphs]).strip()
|
| 31 |
+
|
| 32 |
+
elif filename.endswith(".pdf"):
|
| 33 |
+
pdf_document = fitz.open(stream=file.read(), filetype="pdf")
|
| 34 |
+
text = "\n".join([page.get_text("text") for page in pdf_document])
|
| 35 |
+
return text.strip()
|
| 36 |
+
|
| 37 |
+
else:
|
| 38 |
+
return None # Unsupported file type
|
| 39 |
+
|
| 40 |
+
@app.route("/", methods=["GET", "POST"])
|
| 41 |
+
def translate_file():
|
| 42 |
+
translated_text = ""
|
| 43 |
+
if request.method == "POST":
|
| 44 |
+
file = request.files.get("file")
|
| 45 |
+
target_language = request.form.get("target_language", "en")
|
| 46 |
+
|
| 47 |
+
if not file or file.filename == "":
|
| 48 |
+
return "Error: No file selected."
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
encoding = detect_encoding(file)
|
| 52 |
+
contents = extract_text(file, file.filename, encoding)
|
| 53 |
+
|
| 54 |
+
if not contents:
|
| 55 |
+
return "Error: Unsupported file type or empty file."
|
| 56 |
+
|
| 57 |
+
translated = translator.translate(contents, dest=target_language)
|
| 58 |
+
translated_text = translated.text if translated and translated.text else "Translation failed."
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
translated_text = f"Error during translation: {str(e)}"
|
| 62 |
+
|
| 63 |
+
return render_template("index.html", translated_text=translated_text, languages=LANGUAGES)
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
app.run(debug=True)
|