Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -157,10 +157,36 @@ def merge_markdown_contents(contents):
|
|
| 157 |
Merge multiple Markdown contents into a single Markdown string.
|
| 158 |
"""
|
| 159 |
merged_content = "\n\n---\n\n".join(contents)
|
| 160 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
def upload_to_firebase(user_id, file):
|
| 163 |
-
content =
|
| 164 |
if not content:
|
| 165 |
return None, "Failed to convert file to content."
|
| 166 |
|
|
|
|
| 157 |
Merge multiple Markdown contents into a single Markdown string.
|
| 158 |
"""
|
| 159 |
merged_content = "\n\n---\n\n".join(contents)
|
| 160 |
+
return
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def extract_text_from_file(file):
|
| 165 |
+
"""
|
| 166 |
+
Extract text content from an uploaded file based on its type.
|
| 167 |
+
Args:
|
| 168 |
+
file: The uploaded file object.
|
| 169 |
+
Returns:
|
| 170 |
+
str: Extracted text content.
|
| 171 |
+
"""
|
| 172 |
+
try:
|
| 173 |
+
if file.name.endswith(".pdf"):
|
| 174 |
+
pdf_reader = PdfReader(file)
|
| 175 |
+
return "\n".join(page.extract_text() for page in pdf_reader.pages if page.extract_text())
|
| 176 |
+
elif file.name.endswith(".docx"):
|
| 177 |
+
doc = docx.Document(file)
|
| 178 |
+
return "\n".join(paragraph.text for paragraph in doc.paragraphs if paragraph.text)
|
| 179 |
+
elif file.name.endswith(".txt"):
|
| 180 |
+
return file.read().decode("utf-8")
|
| 181 |
+
else:
|
| 182 |
+
return None
|
| 183 |
+
except Exception as e:
|
| 184 |
+
st.error(f"Error extracting text: {e}")
|
| 185 |
+
return None
|
| 186 |
+
|
| 187 |
|
| 188 |
def upload_to_firebase(user_id, file):
|
| 189 |
+
content = extract_text_from_file (file)
|
| 190 |
if not content:
|
| 191 |
return None, "Failed to convert file to content."
|
| 192 |
|