Update app.py
Browse files
app.py
CHANGED
|
@@ -11,9 +11,10 @@ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
|
| 11 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 12 |
from bs4 import BeautifulSoup
|
| 13 |
import re
|
|
|
|
| 14 |
|
| 15 |
# Set Gemini API Key
|
| 16 |
-
os.environ["GEMINI_API_KEY"] = "AIzaSyAQLUStrjXv2_zrAjssapa_sYXQWOqVeiE" # Ensure this is set securely in
|
| 17 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 18 |
|
| 19 |
# Function to extract text from different file formats
|
|
@@ -56,6 +57,17 @@ def extract_text_from_tex(tex_file):
|
|
| 56 |
content = re.sub(r'\\[a-zA-Z]+', '', content) # Remove LaTeX commands like \section, \textbf, etc.
|
| 57 |
return content
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
def export_conversation_to_pdf(conversation_history):
|
| 60 |
pdf_path = "conversation_clean.pdf"
|
| 61 |
doc = SimpleDocTemplate(pdf_path, pagesize=A4)
|
|
@@ -82,8 +94,10 @@ def main():
|
|
| 82 |
if "chat_active" not in st.session_state:
|
| 83 |
st.session_state.chat_active = True
|
| 84 |
|
| 85 |
-
uploaded_file = st.file_uploader(
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
if uploaded_file and not st.session_state.document_text:
|
| 89 |
filetype = uploaded_file.name.split(".")[-1].lower()
|
|
@@ -104,6 +118,11 @@ def main():
|
|
| 104 |
st.session_state.document_text = extract_text_from_html(uploaded_file)
|
| 105 |
elif filetype == "tex":
|
| 106 |
st.session_state.document_text = extract_text_from_tex(uploaded_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
else:
|
| 108 |
st.error("Unsupported file format.")
|
| 109 |
except Exception as e:
|
|
@@ -126,30 +145,41 @@ def main():
|
|
| 126 |
st.success("Chat ended. Reload to start again.")
|
| 127 |
else:
|
| 128 |
with st.spinner("Gemini is thinking..."):
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
)
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
model="gemini-2.0-flash",
|
| 146 |
-
contents=
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
|
| 152 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
if st.session_state.conversation:
|
| 155 |
pdf_path = export_conversation_to_pdf(st.session_state.conversation)
|
|
|
|
| 11 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 12 |
from bs4 import BeautifulSoup
|
| 13 |
import re
|
| 14 |
+
import base64
|
| 15 |
|
| 16 |
# Set Gemini API Key
|
| 17 |
+
os.environ["GEMINI_API_KEY"] = "AIzaSyAQLUStrjXv2_zrAjssapa_sYXQWOqVeiE" # Ensure this is set securely in your environment
|
| 18 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 19 |
|
| 20 |
# Function to extract text from different file formats
|
|
|
|
| 57 |
content = re.sub(r'\\[a-zA-Z]+', '', content) # Remove LaTeX commands like \section, \textbf, etc.
|
| 58 |
return content
|
| 59 |
|
| 60 |
+
def process_image(image_file):
|
| 61 |
+
# Convert the image to base64
|
| 62 |
+
image_bytes = image_file.read()
|
| 63 |
+
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
|
| 64 |
+
return {
|
| 65 |
+
"inline_data": {
|
| 66 |
+
"mime_type": image_file.type,
|
| 67 |
+
"data": encoded_image
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
def export_conversation_to_pdf(conversation_history):
|
| 72 |
pdf_path = "conversation_clean.pdf"
|
| 73 |
doc = SimpleDocTemplate(pdf_path, pagesize=A4)
|
|
|
|
| 94 |
if "chat_active" not in st.session_state:
|
| 95 |
st.session_state.chat_active = True
|
| 96 |
|
| 97 |
+
uploaded_file = st.file_uploader(
|
| 98 |
+
"Upload a file (.txt, .docx, .csv, .xlsx, .pptx, .pdf, .html, .htm, .tex, .jpg, .jpeg, .png):",
|
| 99 |
+
type=["txt", "docx", "csv", "xlsx", "pptx", "pdf", "html", "htm", "tex", "jpg", "jpeg", "png"]
|
| 100 |
+
)
|
| 101 |
|
| 102 |
if uploaded_file and not st.session_state.document_text:
|
| 103 |
filetype = uploaded_file.name.split(".")[-1].lower()
|
|
|
|
| 118 |
st.session_state.document_text = extract_text_from_html(uploaded_file)
|
| 119 |
elif filetype == "tex":
|
| 120 |
st.session_state.document_text = extract_text_from_tex(uploaded_file)
|
| 121 |
+
elif filetype in ["jpg", "jpeg", "png"]:
|
| 122 |
+
image_data = process_image(uploaded_file)
|
| 123 |
+
st.session_state.document_text = "Image uploaded and processed."
|
| 124 |
+
st.image(uploaded_file, caption=uploaded_file.name, use_container_width=True)
|
| 125 |
+
st.session_state.image_data = image_data # Store the image data for later use
|
| 126 |
else:
|
| 127 |
st.error("Unsupported file format.")
|
| 128 |
except Exception as e:
|
|
|
|
| 145 |
st.success("Chat ended. Reload to start again.")
|
| 146 |
else:
|
| 147 |
with st.spinner("Gemini is thinking..."):
|
| 148 |
+
content_blocks = []
|
| 149 |
+
|
| 150 |
+
# Add prior Q&A as part of the context
|
| 151 |
+
if st.session_state.conversation:
|
| 152 |
+
history_text = "\n\n".join(
|
| 153 |
+
f"Q: {entry[0]}\nA: {entry[1]}"
|
| 154 |
+
for entry in st.session_state.conversation
|
| 155 |
)
|
| 156 |
+
content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
|
| 157 |
+
|
| 158 |
+
# Add file text content
|
| 159 |
+
if st.session_state.document_text.strip():
|
| 160 |
+
content_blocks.append({"text": f"Context:\n{st.session_state.document_text}"})
|
|
|
|
| 161 |
|
| 162 |
+
# Add image blocks (if image data exists)
|
| 163 |
+
if "image_data" in st.session_state:
|
| 164 |
+
content_blocks.append(st.session_state.image_data)
|
| 165 |
+
|
| 166 |
+
# Add current question
|
| 167 |
+
content_blocks.append({"text": f"Question: {user_input}"})
|
| 168 |
+
|
| 169 |
+
response = client.models.generate_content(
|
| 170 |
model="gemini-2.0-flash",
|
| 171 |
+
contents=content_blocks
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
st.session_state.conversation.append((user_input, response.text))
|
| 175 |
+
st.success("💡 Answer:")
|
| 176 |
+
st.write(response.text)
|
| 177 |
|
| 178 |
+
# Save to session history
|
| 179 |
+
st.session_state.chat_history.append({
|
| 180 |
+
"question": user_input,
|
| 181 |
+
"answer": response.text
|
| 182 |
+
})
|
| 183 |
|
| 184 |
if st.session_state.conversation:
|
| 185 |
pdf_path = export_conversation_to_pdf(st.session_state.conversation)
|