Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import PyPDF2
|
|
| 4 |
from PIL import Image
|
| 5 |
import pytesseract
|
| 6 |
import speech_recognition as sr
|
|
|
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
|
|
@@ -42,18 +43,34 @@ def extract_text_from_video(video_file):
|
|
| 42 |
return r.recognize_google(audio_data)
|
| 43 |
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# Function to process files and generate embeddings
|
| 46 |
def process_file(file):
|
| 47 |
-
file_type = file.type.split('/')[
|
| 48 |
content = ""
|
| 49 |
|
| 50 |
try:
|
| 51 |
-
if
|
| 52 |
content = extract_text_from_pdf(file)
|
| 53 |
-
elif
|
| 54 |
content = extract_text_from_image(file)
|
| 55 |
-
elif
|
| 56 |
content = extract_text_from_video(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
st.error(f"Error processing file {file.name}: {e}")
|
| 59 |
|
|
@@ -70,7 +87,11 @@ st.title("AI-based Text Search for Files")
|
|
| 70 |
st.write("Upload documents, images, or videos, and perform AI-powered text search.")
|
| 71 |
|
| 72 |
# File upload section
|
| 73 |
-
uploaded_files = st.file_uploader(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
if uploaded_files:
|
| 75 |
for file in uploaded_files:
|
| 76 |
process_file(file)
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import pytesseract
|
| 6 |
import speech_recognition as sr
|
| 7 |
+
import docx
|
| 8 |
import tempfile
|
| 9 |
import os
|
| 10 |
|
|
|
|
| 43 |
return r.recognize_google(audio_data)
|
| 44 |
|
| 45 |
|
| 46 |
+
# Function to extract text from a TXT file
|
| 47 |
+
def extract_text_from_txt(txt_file):
|
| 48 |
+
return txt_file.read().decode("utf-8")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Function to extract text from a Word document
|
| 52 |
+
def extract_text_from_word(docx_file):
|
| 53 |
+
doc = docx.Document(docx_file)
|
| 54 |
+
text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
|
| 55 |
+
return text
|
| 56 |
+
|
| 57 |
+
|
| 58 |
# Function to process files and generate embeddings
|
| 59 |
def process_file(file):
|
| 60 |
+
file_type = file.type.split('/')[1]
|
| 61 |
content = ""
|
| 62 |
|
| 63 |
try:
|
| 64 |
+
if file.type == 'application/pdf': # PDF files
|
| 65 |
content = extract_text_from_pdf(file)
|
| 66 |
+
elif file.type in ['image/png', 'image/jpeg']: # Images
|
| 67 |
content = extract_text_from_image(file)
|
| 68 |
+
elif file.type == 'video/mp4': # Videos
|
| 69 |
content = extract_text_from_video(file)
|
| 70 |
+
elif file.type == 'text/plain': # TXT files
|
| 71 |
+
content = extract_text_from_txt(file)
|
| 72 |
+
elif file.name.endswith('.docx'): # Word documents
|
| 73 |
+
content = extract_text_from_word(file)
|
| 74 |
except Exception as e:
|
| 75 |
st.error(f"Error processing file {file.name}: {e}")
|
| 76 |
|
|
|
|
| 87 |
st.write("Upload documents, images, or videos, and perform AI-powered text search.")
|
| 88 |
|
| 89 |
# File upload section
|
| 90 |
+
uploaded_files = st.file_uploader(
|
| 91 |
+
"Upload your files",
|
| 92 |
+
type=['pdf', 'png', 'jpg', 'jpeg', 'mp4', 'txt', 'docx'],
|
| 93 |
+
accept_multiple_files=True
|
| 94 |
+
)
|
| 95 |
if uploaded_files:
|
| 96 |
for file in uploaded_files:
|
| 97 |
process_file(file)
|