Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import fitz
|
| 3 |
from transformers import pipeline, MBart50TokenizerFast, MBartForConditionalGeneration
|
| 4 |
from multiprocessing import Pool, cpu_count
|
|
|
|
| 5 |
|
| 6 |
# Load summarization pipeline
|
| 7 |
summarizer = pipeline("summarization", model="Falconsai/text_summarization")
|
|
@@ -62,10 +63,17 @@ def translate_summary(summary, lang):
|
|
| 62 |
|
| 63 |
return " ".join(translated_chunks)
|
| 64 |
|
|
|
|
|
|
|
| 65 |
# Function to read PDF and summarize and translate chunk by chunk
|
| 66 |
def summarize_and_translate_pdf(uploaded_file, lang):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
try:
|
| 68 |
-
doc = fitz.open(
|
| 69 |
except FileNotFoundError:
|
| 70 |
st.error("File not found. Please make sure the file path is correct.")
|
| 71 |
return []
|
|
@@ -82,6 +90,9 @@ def summarize_and_translate_pdf(uploaded_file, lang):
|
|
| 82 |
with Pool(cpu_count()) as pool:
|
| 83 |
translated_chunks = pool.starmap(summarize_and_translate_chunk, [(chunk, lang) for chunk in chunks])
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
return translated_chunks
|
| 86 |
|
| 87 |
|
|
|
|
| 2 |
import fitz
|
| 3 |
from transformers import pipeline, MBart50TokenizerFast, MBartForConditionalGeneration
|
| 4 |
from multiprocessing import Pool, cpu_count
|
| 5 |
+
import tempfile
|
| 6 |
|
| 7 |
# Load summarization pipeline
|
| 8 |
summarizer = pipeline("summarization", model="Falconsai/text_summarization")
|
|
|
|
| 63 |
|
| 64 |
return " ".join(translated_chunks)
|
| 65 |
|
| 66 |
+
|
| 67 |
+
|
| 68 |
# Function to read PDF and summarize and translate chunk by chunk
|
| 69 |
def summarize_and_translate_pdf(uploaded_file, lang):
|
| 70 |
+
# Save uploaded PDF to a temporary file
|
| 71 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
| 72 |
+
temp_file.write(uploaded_file.read())
|
| 73 |
+
temp_file_path = temp_file.name
|
| 74 |
+
|
| 75 |
try:
|
| 76 |
+
doc = fitz.open(temp_file_path)
|
| 77 |
except FileNotFoundError:
|
| 78 |
st.error("File not found. Please make sure the file path is correct.")
|
| 79 |
return []
|
|
|
|
| 90 |
with Pool(cpu_count()) as pool:
|
| 91 |
translated_chunks = pool.starmap(summarize_and_translate_chunk, [(chunk, lang) for chunk in chunks])
|
| 92 |
|
| 93 |
+
# Delete temporary file
|
| 94 |
+
os.unlink(temp_file_path)
|
| 95 |
+
|
| 96 |
return translated_chunks
|
| 97 |
|
| 98 |
|