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