Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from textblob import TextBlob
|
| 3 |
+
import PyPDF2
|
| 4 |
+
|
| 5 |
+
def extract_text_from_pdf(file_path):
|
| 6 |
+
pdf_file_obj = open(file_path, 'rb')
|
| 7 |
+
pdf_reader = PyPDF2.PdfFileReader(pdf_file_obj)
|
| 8 |
+
text = ''
|
| 9 |
+
for page_num in range(pdf_reader.numPages):
|
| 10 |
+
page_obj = pdf_reader.getPage(page_num)
|
| 11 |
+
text += page_obj.extractText()
|
| 12 |
+
pdf_file_obj.close()
|
| 13 |
+
return text
|
| 14 |
+
|
| 15 |
+
def translate_text(text, dest_lang='en'):
|
| 16 |
+
blob = TextBlob(text)
|
| 17 |
+
translation = blob.translate(to=dest_lang)
|
| 18 |
+
return str(translation)
|
| 19 |
+
|
| 20 |
+
def translate_pdf(file):
|
| 21 |
+
text = extract_text_from_pdf(file.name)
|
| 22 |
+
translation = translate_text(text)
|
| 23 |
+
return translation
|
| 24 |
+
|
| 25 |
+
iface = gr.Interface(fn=translate_pdf, inputs='file', outputs='text')
|
| 26 |
+
iface.launch()
|