Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,6 +23,8 @@ genai.configure(api_key=GOOGLE_API_KEY)
|
|
| 23 |
model = genai.GenerativeModel(model_name="gemini-2.0-flash") # Supports both doc & image queries
|
| 24 |
|
| 25 |
# Helper: Extract text from various documents
|
|
|
|
|
|
|
| 26 |
def load_and_extract_text(file_path):
|
| 27 |
try:
|
| 28 |
ext = file_path.split('.')[-1].lower()
|
|
@@ -54,6 +56,15 @@ def load_and_extract_text(file_path):
|
|
| 54 |
prs = Presentation(file_path)
|
| 55 |
return '\n'.join(shape.text for slide in prs.slides for shape in slide.shapes if hasattr(shape, 'text'))
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
else:
|
| 58 |
return "Unsupported file format."
|
| 59 |
|
|
@@ -61,7 +72,7 @@ def load_and_extract_text(file_path):
|
|
| 61 |
return f"Error processing file: {e}"
|
| 62 |
|
| 63 |
# File uploader
|
| 64 |
-
uploaded_file = st.file_uploader("Upload a document or image", type=["docx", "xlsx", "pdf", "csv", "txt", "pptx", "jpg", "jpeg", "png"])
|
| 65 |
|
| 66 |
# Question input
|
| 67 |
question = st.text_input("💬 Enter your question:")
|
|
|
|
| 23 |
model = genai.GenerativeModel(model_name="gemini-2.0-flash") # Supports both doc & image queries
|
| 24 |
|
| 25 |
# Helper: Extract text from various documents
|
| 26 |
+
import re # Add this at the top for LaTeX cleaning
|
| 27 |
+
|
| 28 |
def load_and_extract_text(file_path):
|
| 29 |
try:
|
| 30 |
ext = file_path.split('.')[-1].lower()
|
|
|
|
| 56 |
prs = Presentation(file_path)
|
| 57 |
return '\n'.join(shape.text for slide in prs.slides for shape in slide.shapes if hasattr(shape, 'text'))
|
| 58 |
|
| 59 |
+
elif ext == 'tex':
|
| 60 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 61 |
+
content = f.read()
|
| 62 |
+
# Remove LaTeX commands (basic approach)
|
| 63 |
+
text = re.sub(r'\\[a-zA-Z]+\*?(?:\[[^\]]*\])?(?:\{[^}]*\})*', '', content)
|
| 64 |
+
# Remove comments
|
| 65 |
+
text = re.sub(r'%.*', '', text)
|
| 66 |
+
return text.strip()
|
| 67 |
+
|
| 68 |
else:
|
| 69 |
return "Unsupported file format."
|
| 70 |
|
|
|
|
| 72 |
return f"Error processing file: {e}"
|
| 73 |
|
| 74 |
# File uploader
|
| 75 |
+
uploaded_file = st.file_uploader("Upload a document or image", type=["docx", "xlsx", "pdf", "csv", "txt", "pptx", "tex", "jpg", "jpeg", "png"])
|
| 76 |
|
| 77 |
# Question input
|
| 78 |
question = st.text_input("💬 Enter your question:")
|