Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ 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")
|
|
@@ -34,11 +33,11 @@ def load_and_extract_text(file_path):
|
|
| 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:
|
| 38 |
reader = csv.reader(csvfile)
|
| 39 |
-
text = '\n'.join([' '.join(row) for row in reader])
|
| 40 |
elif file_extension == 'txt':
|
| 41 |
-
with open(file_path, 'r', encoding='utf-8') as txtfile:
|
| 42 |
text = txtfile.read()
|
| 43 |
elif file_extension == 'pptx':
|
| 44 |
prs = Presentation(file_path)
|
|
@@ -51,11 +50,8 @@ def load_and_extract_text(file_path):
|
|
| 51 |
return f"Error processing file: {e}"
|
| 52 |
|
| 53 |
|
| 54 |
-
def process_document_and_answer(
|
| 55 |
-
|
| 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)
|
|
@@ -67,19 +63,52 @@ def process_document_and_answer(extracted_text, question):
|
|
| 67 |
# Streamlit UI
|
| 68 |
st.title("Gemini Document Q&A")
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
-
if uploaded_file is not None
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
os.remove(temp_file_path) # Clean up the temporary file
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
| 6 |
from pdfminer.high_level import extract_text
|
| 7 |
import csv
|
| 8 |
from pptx import Presentation
|
|
|
|
| 9 |
|
| 10 |
# Configure Gemini API
|
| 11 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
|
|
| 33 |
elif file_extension == 'pdf':
|
| 34 |
text = extract_text(file_path)
|
| 35 |
elif file_extension == 'csv':
|
| 36 |
+
with open(file_path, 'r', encoding='utf-8') as csvfile:
|
| 37 |
reader = csv.reader(csvfile)
|
| 38 |
+
text = '\n'.join([' '.join(row) for row in reader])
|
| 39 |
elif file_extension == 'txt':
|
| 40 |
+
with open(file_path, 'r', encoding='utf-8') as txtfile:
|
| 41 |
text = txtfile.read()
|
| 42 |
elif file_extension == 'pptx':
|
| 43 |
prs = Presentation(file_path)
|
|
|
|
| 50 |
return f"Error processing file: {e}"
|
| 51 |
|
| 52 |
|
| 53 |
+
def process_document_and_answer(context, question, history=""):
|
| 54 |
+
prompt = f"Context: {context}\n\nConversation History: {history}\n\nQuestion: {question}\n\nAnswer:"
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
try:
|
| 57 |
response = model.generate_content(prompt)
|
|
|
|
| 63 |
# Streamlit UI
|
| 64 |
st.title("Gemini Document Q&A")
|
| 65 |
|
| 66 |
+
# Initialize session state for conversation history
|
| 67 |
+
if 'conversation_history' not in st.session_state:
|
| 68 |
+
st.session_state.conversation_history = ""
|
| 69 |
+
|
| 70 |
+
if 'document_content' not in st.session_state:
|
| 71 |
+
st.session_state.document_content = None
|
| 72 |
|
| 73 |
+
# File Upload
|
| 74 |
+
uploaded_file = st.file_uploader("Upload a document", type=["docx", "xlsx", "pdf", "csv", "txt", "pptx"])
|
| 75 |
|
| 76 |
+
if uploaded_file is not None:
|
| 77 |
+
# Save the uploaded file to a temporary location (important for Streamlit)
|
| 78 |
temp_file_path = "temp." + uploaded_file.name.split('.')[-1].lower()
|
| 79 |
with open(temp_file_path, "wb") as temp_file:
|
| 80 |
temp_file.write(uploaded_file.read())
|
| 81 |
|
| 82 |
+
st.session_state.document_content = load_and_extract_text(temp_file_path)
|
| 83 |
+
os.remove(temp_file_path) # Clean up
|
| 84 |
+
|
| 85 |
+
if "Error processing file" in st.session_state.document_content:
|
| 86 |
+
st.error(st.session_state.document_content)
|
| 87 |
+
st.session_state.document_content = None # Clear document content to prevent further processing
|
| 88 |
+
|
| 89 |
+
if st.session_state.document_content:
|
| 90 |
+
st.subheader("Document Content Preview:")
|
| 91 |
+
st.text(st.session_state.document_content[:500] + "..." if len(st.session_state.document_content) > 500 else st.session_state.document_content) # Display a snippet
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# Conversation Loop
|
| 95 |
+
while True:
|
| 96 |
+
question = st.text_input("Ask a question about the document (type 'exit' to end):", key=f"question_{len(st.session_state.conversation_history)}") # Unique key for each input
|
| 97 |
+
|
| 98 |
+
if question.lower() == "exit":
|
| 99 |
+
st.write("Ending conversation.")
|
| 100 |
+
break
|
| 101 |
+
|
| 102 |
+
if question and st.session_state.document_content:
|
| 103 |
+
answer = process_document_and_answer(st.session_state.document_content, question, st.session_state.conversation_history)
|
| 104 |
+
|
| 105 |
+
st.write("Answer:", answer)
|
| 106 |
+
|
| 107 |
+
# Update conversation history
|
| 108 |
+
st.session_state.conversation_history += f"\nQuestion: {question}\nAnswer: {answer}"
|
| 109 |
|
|
|
|
| 110 |
|
| 111 |
+
# Display conversation history
|
| 112 |
+
if st.session_state.conversation_history:
|
| 113 |
+
st.subheader("Conversation History:")
|
| 114 |
+
st.text(st.session_state.conversation_history)
|