improved the file part handling
Browse files
app.py
CHANGED
@@ -29,36 +29,41 @@ def download_pdf():
|
|
29 |
st.error(f"Failed to download PDF from Hugging Face Hub: {e}")
|
30 |
st.stop() # Stop if the download fails
|
31 |
|
32 |
-
# Initialize
|
|
|
|
|
|
|
|
|
33 |
if "uploaded_pdf_path" not in st.session_state:
|
34 |
st.session_state.uploaded_pdf_path = download_pdf()
|
35 |
-
|
36 |
-
|
37 |
|
38 |
def multimodal_prompt(pdf_path, text_prompt):
|
39 |
"""
|
40 |
-
Sends a multimodal prompt
|
41 |
-
|
42 |
Args:
|
43 |
pdf_path: The path to the PDF file.
|
44 |
text_prompt: The text prompt for the model.
|
45 |
-
|
46 |
Returns:
|
47 |
The model's response as a string, or an error message.
|
48 |
"""
|
49 |
-
|
50 |
try:
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
text_prompt,
|
55 |
-
|
56 |
-
|
|
|
57 |
|
58 |
response = chat.send_message(prompt)
|
59 |
-
|
60 |
-
|
|
|
|
|
61 |
return response.text
|
|
|
62 |
except Exception as e:
|
63 |
return f"An error occurred: {e}"
|
64 |
|
|
|
29 |
st.error(f"Failed to download PDF from Hugging Face Hub: {e}")
|
30 |
st.stop() # Stop if the download fails
|
31 |
|
32 |
+
# Initialize conversation history in Streamlit session state
|
33 |
+
if "conversation_history" not in st.session_state:
|
34 |
+
st.session_state.conversation_history = []
|
35 |
+
if "uploaded_file_part" not in st.session_state: # Store the file *part*
|
36 |
+
st.session_state.uploaded_file_part = None
|
37 |
if "uploaded_pdf_path" not in st.session_state:
|
38 |
st.session_state.uploaded_pdf_path = download_pdf()
|
39 |
+
|
40 |
+
|
41 |
|
42 |
def multimodal_prompt(pdf_path, text_prompt):
|
43 |
"""
|
44 |
+
Sends a multimodal prompt to Gemini, handling file uploads efficiently.
|
|
|
45 |
Args:
|
46 |
pdf_path: The path to the PDF file.
|
47 |
text_prompt: The text prompt for the model.
|
|
|
48 |
Returns:
|
49 |
The model's response as a string, or an error message.
|
50 |
"""
|
|
|
51 |
try:
|
52 |
+
if st.session_state.uploaded_file_part is None: # First time, upload
|
53 |
+
pdf_part = genai.upload_file(pdf_path, mime_type="application/pdf")
|
54 |
+
st.session_state.uploaded_file_part = pdf_part
|
55 |
+
prompt = [text_prompt, pdf_part] # First turn includes the actual file
|
56 |
+
else: # Subsequent turns, reference the file
|
57 |
+
|
58 |
+
prompt = [text_prompt, st.session_state.uploaded_file_part] # Subsequent turns include the file reference
|
59 |
|
60 |
response = chat.send_message(prompt)
|
61 |
+
|
62 |
+
# Update conversation history
|
63 |
+
st.session_state.conversation_history.append({"role": "user", "content": text_prompt, "has_pdf": True})
|
64 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": response.text})
|
65 |
return response.text
|
66 |
+
|
67 |
except Exception as e:
|
68 |
return f"An error occurred: {e}"
|
69 |
|