louiecerv commited on
Commit
49f249c
·
1 Parent(s): 3f93684

improved the file part handling

Browse files
Files changed (1) hide show
  1. app.py +20 -15
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 session state for the uploaded PDF and its path
 
 
 
 
33
  if "uploaded_pdf_path" not in st.session_state:
34
  st.session_state.uploaded_pdf_path = download_pdf()
35
- if "conversation_history" not in st.session_state:
36
- st.session_state.conversation_history = [] # Store the conversation history
37
 
38
  def multimodal_prompt(pdf_path, text_prompt):
39
  """
40
- Sends a multimodal prompt (PDF + text) to Gemini for the *first* message.
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
- pdf_part = genai.upload_file(pdf_path, mime_type="application/pdf")
52
-
53
- prompt = [
54
- text_prompt,
55
- pdf_part
56
- ]
 
57
 
58
  response = chat.send_message(prompt)
59
- st.session_state.conversation_history.append({"role": "user", "content": text_prompt, "has_pdf": True}) # Add to history
60
- st.session_state.conversation_history.append({"role": "assistant", "content": response.text}) # Add to history
 
 
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