Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,11 @@ import openpyxl
|
|
| 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")
|
|
@@ -60,6 +65,11 @@ def process_document_and_answer(context, question, history=""):
|
|
| 60 |
return f"Error generating answer: {e}"
|
| 61 |
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# Streamlit UI
|
| 64 |
st.title("Gemini Document Q&A")
|
| 65 |
|
|
@@ -73,6 +83,8 @@ if 'document_content' not in st.session_state:
|
|
| 73 |
if 'question_count' not in st.session_state:
|
| 74 |
st.session_state.question_count = 0
|
| 75 |
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# File Upload
|
| 78 |
uploaded_file = st.file_uploader("Upload a document", type=["docx", "xlsx", "pdf", "csv", "txt", "pptx"])
|
|
@@ -96,21 +108,48 @@ if st.session_state.document_content:
|
|
| 96 |
st.text(st.session_state.document_content[:500] + "..." if len(st.session_state.document_content) > 500 else st.session_state.document_content)
|
| 97 |
|
| 98 |
# Question Input
|
| 99 |
-
question = st.text_input("Ask a question about the document (type 'exit' to end):", key=f"question_{st.session_state.question_count}")
|
| 100 |
|
| 101 |
if question:
|
| 102 |
if question.lower() == "exit":
|
| 103 |
st.write("Ending conversation.")
|
| 104 |
else:
|
| 105 |
answer = process_document_and_answer(st.session_state.document_content, question, st.session_state.conversation_history)
|
| 106 |
-
st.
|
|
|
|
| 107 |
|
| 108 |
# Update conversation history and question count
|
| 109 |
st.session_state.conversation_history += f"\nQuestion: {question}\nAnswer: {answer}"
|
| 110 |
st.session_state.question_count += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
|
| 113 |
-
#
|
| 114 |
if st.session_state.conversation_history:
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from pdfminer.high_level import extract_text
|
| 7 |
import csv
|
| 8 |
from pptx import Presentation
|
| 9 |
+
import base64 # For creating PDF download link
|
| 10 |
+
from io import BytesIO # For PDF generation in memory
|
| 11 |
+
from reportlab.pdfgen import canvas
|
| 12 |
+
from reportlab.lib.pagesizes import letter
|
| 13 |
+
from reportlab.lib.colors import blue, red
|
| 14 |
|
| 15 |
# Configure Gemini API
|
| 16 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
|
|
| 65 |
return f"Error generating answer: {e}"
|
| 66 |
|
| 67 |
|
| 68 |
+
def create_download_link(val, filename):
|
| 69 |
+
b64 = base64.b64encode(val) # val looks like b'...'
|
| 70 |
+
return f'<a href="data:application/octet-stream;base64,{b64.decode()}" download="{filename}">Download conversation as PDF</a>'
|
| 71 |
+
|
| 72 |
+
|
| 73 |
# Streamlit UI
|
| 74 |
st.title("Gemini Document Q&A")
|
| 75 |
|
|
|
|
| 83 |
if 'question_count' not in st.session_state:
|
| 84 |
st.session_state.question_count = 0
|
| 85 |
|
| 86 |
+
if 'display_answer' not in st.session_state:
|
| 87 |
+
st.session_state.display_answer = False
|
| 88 |
|
| 89 |
# File Upload
|
| 90 |
uploaded_file = st.file_uploader("Upload a document", type=["docx", "xlsx", "pdf", "csv", "txt", "pptx"])
|
|
|
|
| 108 |
st.text(st.session_state.document_content[:500] + "..." if len(st.session_state.document_content) > 500 else st.session_state.document_content)
|
| 109 |
|
| 110 |
# Question Input
|
| 111 |
+
question = st.text_input("**Ask a question about the document (type 'exit' to end):**", key=f"question_{st.session_state.question_count}")
|
| 112 |
|
| 113 |
if question:
|
| 114 |
if question.lower() == "exit":
|
| 115 |
st.write("Ending conversation.")
|
| 116 |
else:
|
| 117 |
answer = process_document_and_answer(st.session_state.document_content, question, st.session_state.conversation_history)
|
| 118 |
+
st.markdown(f"<p style='color:green;'><b>Answer:</b> {answer}</p>", unsafe_allow_html=True) # Colored Answer
|
| 119 |
+
st.session_state.display_answer = True # set trigger to allow auto scroll
|
| 120 |
|
| 121 |
# Update conversation history and question count
|
| 122 |
st.session_state.conversation_history += f"\nQuestion: {question}\nAnswer: {answer}"
|
| 123 |
st.session_state.question_count += 1
|
| 124 |
+
if st.session_state.display_answer:
|
| 125 |
+
js = f"""
|
| 126 |
+
<script>
|
| 127 |
+
var textarea = document.getElementById('question_{st.session_state.question_count -1 }');
|
| 128 |
+
textarea.scrollIntoView();
|
| 129 |
+
</script>
|
| 130 |
+
"""
|
| 131 |
+
st.components.v1.html(js, height=0)
|
| 132 |
+
st.session_state.display_answer = False # set back to false after scroll
|
| 133 |
+
|
| 134 |
|
| 135 |
|
| 136 |
+
# PDF Export
|
| 137 |
if st.session_state.conversation_history:
|
| 138 |
+
pdf_buffer = BytesIO()
|
| 139 |
+
c = canvas.Canvas(pdf_buffer, pagesize=letter)
|
| 140 |
+
textobject = c.beginText()
|
| 141 |
+
textobject.setTextOrigin(10, 730)
|
| 142 |
+
textobject.setFont("Helvetica", 12)
|
| 143 |
+
|
| 144 |
+
for line in st.session_state.conversation_history.split('\n'):
|
| 145 |
+
textobject.setFillColor(blue)
|
| 146 |
+
if line.startswith("Answer:"):
|
| 147 |
+
textobject.setFillColor(red)
|
| 148 |
+
textobject.textLine(line)
|
| 149 |
+
|
| 150 |
+
c.drawText(textobject)
|
| 151 |
+
c.save()
|
| 152 |
+
pdf_buffer.seek(0)
|
| 153 |
+
|
| 154 |
+
download_link = create_download_link(pdf_buffer.read(), "Conversation.pdf")
|
| 155 |
+
st.markdown(download_link, unsafe_allow_html=True)
|