Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from docx import Document
|
| 5 |
+
import openpyxl
|
| 6 |
+
from pdfminer.high_level import extract_text
|
| 7 |
+
import csv
|
| 8 |
+
from pptx import Presentation
|
| 9 |
+
from io import StringIO # For handling string-based CSV
|
| 10 |
+
|
| 11 |
+
# Configure Gemini API
|
| 12 |
+
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 13 |
+
if not GOOGLE_API_KEY:
|
| 14 |
+
st.error("Please set the GOOGLE_API_KEY environment variable.")
|
| 15 |
+
st.stop()
|
| 16 |
+
|
| 17 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 18 |
+
model = genai.GenerativeModel('gemini-1.5-flash') # Or another Gemini model
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def load_and_extract_text(file_path):
|
| 22 |
+
try:
|
| 23 |
+
file_extension = file_path.split('.')[-1].lower()
|
| 24 |
+
|
| 25 |
+
if file_extension == 'docx':
|
| 26 |
+
doc = Document(file_path)
|
| 27 |
+
text = '\n'.join([paragraph.text for paragraph in doc.paragraphs])
|
| 28 |
+
elif file_extension == 'xlsx':
|
| 29 |
+
workbook = openpyxl.load_workbook(file_path)
|
| 30 |
+
text = ""
|
| 31 |
+
for sheet in workbook:
|
| 32 |
+
for row in sheet.iter_rows():
|
| 33 |
+
text += ' '.join([str(cell.value) for cell in row if cell.value is not None]) + '\n'
|
| 34 |
+
elif file_extension == 'pdf':
|
| 35 |
+
text = extract_text(file_path)
|
| 36 |
+
elif file_extension == 'csv':
|
| 37 |
+
with open(file_path, 'r', encoding='utf-8') as csvfile: # Explicit encoding
|
| 38 |
+
reader = csv.reader(csvfile)
|
| 39 |
+
text = '\n'.join([' '.join(row) for row in reader]) # Join rows with spaces
|
| 40 |
+
elif file_extension == 'txt':
|
| 41 |
+
with open(file_path, 'r', encoding='utf-8') as txtfile: #Explicit encoding
|
| 42 |
+
text = txtfile.read()
|
| 43 |
+
elif file_extension == 'pptx':
|
| 44 |
+
prs = Presentation(file_path)
|
| 45 |
+
text = '\n'.join([shape.text for slide in prs.slides for shape in slide.shapes if hasattr(shape, 'text')])
|
| 46 |
+
else:
|
| 47 |
+
return "Unsupported file format."
|
| 48 |
+
|
| 49 |
+
return text
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Error processing file: {e}"
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def process_document_and_answer(extracted_text, question):
|
| 55 |
+
if "Error processing file" in extracted_text or "Unsupported file format" in extracted_text:
|
| 56 |
+
return extracted_text
|
| 57 |
+
|
| 58 |
+
prompt = f"Context: {extracted_text}\n\nQuestion: {question}\n\nAnswer:"
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
response = model.generate_content(prompt)
|
| 62 |
+
return response.text
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return f"Error generating answer: {e}"
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# Streamlit UI
|
| 68 |
+
st.title("Gemini Document Q&A")
|
| 69 |
+
|
| 70 |
+
uploaded_file = st.file_uploader("Upload a document", type=["docx", "xlsx", "pdf", "csv", "txt", "pptx"])
|
| 71 |
+
|
| 72 |
+
question = st.text_input("Ask a question about the document:")
|
| 73 |
+
|
| 74 |
+
if uploaded_file is not None and question:
|
| 75 |
+
# Save the uploaded file to a temporary location
|
| 76 |
+
temp_file_path = "temp." + uploaded_file.name.split('.')[-1].lower()
|
| 77 |
+
with open(temp_file_path, "wb") as temp_file:
|
| 78 |
+
temp_file.write(uploaded_file.read())
|
| 79 |
+
|
| 80 |
+
extracted_text = load_and_extract_text(temp_file_path)
|
| 81 |
+
|
| 82 |
+
os.remove(temp_file_path) # Clean up the temporary file
|
| 83 |
+
|
| 84 |
+
answer = process_document_and_answer(extracted_text, question)
|
| 85 |
+
st.write("Answer:", answer)
|