Update app.py
Browse files
app.py
CHANGED
|
@@ -6,11 +6,12 @@ import openpyxl
|
|
| 6 |
from pdfminer.high_level import extract_text
|
| 7 |
import csv
|
| 8 |
from pptx import Presentation
|
| 9 |
-
import base64
|
| 10 |
-
from io import BytesIO
|
| 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")
|
|
@@ -69,6 +70,26 @@ 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")
|
|
@@ -86,6 +107,10 @@ if 'question_count' not in st.session_state:
|
|
| 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"])
|
| 91 |
|
|
@@ -102,35 +127,30 @@ if uploaded_file is not None:
|
|
| 102 |
st.error(st.session_state.document_content)
|
| 103 |
st.session_state.document_content = None
|
| 104 |
|
| 105 |
-
|
| 106 |
if st.session_state.document_content:
|
| 107 |
-
st.subheader("Document Content Preview:")
|
| 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 |
-
#
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 119 |
-
st.session_state.display_answer = True
|
| 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 |
-
|
| 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
|
|
@@ -151,5 +171,5 @@ if st.session_state.document_content:
|
|
| 151 |
c.save()
|
| 152 |
pdf_buffer.seek(0)
|
| 153 |
|
| 154 |
-
download_link = create_download_link(pdf_buffer.read(), "
|
| 155 |
st.markdown(download_link, unsafe_allow_html=True)
|
|
|
|
| 6 |
from pdfminer.high_level import extract_text
|
| 7 |
import csv
|
| 8 |
from pptx import Presentation
|
| 9 |
+
import base64
|
| 10 |
+
from io import BytesIO
|
| 11 |
from reportlab.pdfgen import canvas
|
| 12 |
from reportlab.lib.pagesizes import letter
|
| 13 |
from reportlab.lib.colors import blue, red
|
| 14 |
+
import streamlit.components.v1 as components
|
| 15 |
|
| 16 |
# Configure Gemini API
|
| 17 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
|
|
| 70 |
b64 = base64.b64encode(val) # val looks like b'...'
|
| 71 |
return f'<a href="data:application/octet-stream;base64,{b64.decode()}" download="{filename}">Download conversation as PDF</a>'
|
| 72 |
|
| 73 |
+
def move_element(element, location="bottom"):
|
| 74 |
+
js_code = f"""
|
| 75 |
+
<script>
|
| 76 |
+
window.onload = function() {{
|
| 77 |
+
var element = document.getElementById('{element}');
|
| 78 |
+
if (element) {{
|
| 79 |
+
element.style.position = 'fixed';
|
| 80 |
+
element.style.bottom = '0';
|
| 81 |
+
element.style.left = '0';
|
| 82 |
+
element.style.width = '100%';
|
| 83 |
+
element.style.backgroundColor = 'white';
|
| 84 |
+
element.style.zIndex = '9999';
|
| 85 |
+
element.style.padding = '10px';
|
| 86 |
+
element.style.boxShadow = '0px -2px 5px #888888';
|
| 87 |
+
document.body.appendChild(element);
|
| 88 |
+
}}
|
| 89 |
+
}}
|
| 90 |
+
</script>
|
| 91 |
+
"""
|
| 92 |
+
components.html(js_code, height=0)
|
| 93 |
|
| 94 |
# Streamlit UI
|
| 95 |
st.title("Gemini Document Q&A")
|
|
|
|
| 107 |
if 'display_answer' not in st.session_state:
|
| 108 |
st.session_state.display_answer = False
|
| 109 |
|
| 110 |
+
if 'answers' not in st.session_state:
|
| 111 |
+
st.session_state.answers = []
|
| 112 |
+
|
| 113 |
+
|
| 114 |
# File Upload
|
| 115 |
uploaded_file = st.file_uploader("Upload a document", type=["docx", "xlsx", "pdf", "csv", "txt", "pptx"])
|
| 116 |
|
|
|
|
| 127 |
st.error(st.session_state.document_content)
|
| 128 |
st.session_state.document_content = None
|
| 129 |
|
|
|
|
| 130 |
if st.session_state.document_content:
|
|
|
|
|
|
|
| 131 |
|
| 132 |
+
# Display Answers above
|
| 133 |
+
if st.session_state.answers:
|
| 134 |
+
for i, answer in enumerate(st.session_state.answers):
|
| 135 |
+
st.markdown(f"<p style='color:green;'><b>Answer {i+1}:</b> {answer}</p>", unsafe_allow_html=True)
|
| 136 |
+
|
| 137 |
+
# Question Input at bottom using javascript to stick at the bottom
|
| 138 |
+
move_element("question_input")
|
| 139 |
+
question = st.text_input("**Ask a question (type 'exit' to end):**", key="question_input", value="")
|
| 140 |
|
| 141 |
if question:
|
| 142 |
if question.lower() == "exit":
|
| 143 |
st.write("Ending conversation.")
|
| 144 |
+
st.stop() # this one is more appropiate to stop
|
| 145 |
else:
|
| 146 |
answer = process_document_and_answer(st.session_state.document_content, question, st.session_state.conversation_history)
|
| 147 |
+
st.session_state.answers.append(answer)
|
| 148 |
+
st.session_state.display_answer = True
|
| 149 |
|
| 150 |
# Update conversation history and question count
|
| 151 |
st.session_state.conversation_history += f"\nQuestion: {question}\nAnswer: {answer}"
|
| 152 |
st.session_state.question_count += 1
|
| 153 |
+
st.rerun() # Rerun to display new question at bottom and answer at top
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
|
| 156 |
# PDF Export
|
|
|
|
| 171 |
c.save()
|
| 172 |
pdf_buffer.seek(0)
|
| 173 |
|
| 174 |
+
download_link = create_download_link(pdf_buffer.read(), "conversation.pdf")
|
| 175 |
st.markdown(download_link, unsafe_allow_html=True)
|