louiecerv commited on
Commit
f28ca64
·
1 Parent(s): 8496e3f

sync with remote

Browse files
Files changed (3) hide show
  1. app.py +104 -0
  2. problems/problems.pdf +0 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import google.generativeai as genai
4
+ import json
5
+ from PIL import Image
6
+
7
+
8
+ MODEL_ID = "gemini-2.0-flash-exp" # Keep the model ID as is
9
+ try:
10
+ api_key = os.getenv("GEMINI_API_KEY")
11
+ model_id = MODEL_ID
12
+ genai.configure(api_key=api_key)
13
+ except Exception as e:
14
+ st.error(f"Error: {e}")
15
+ st.stop()
16
+
17
+ model = genai.GenerativeModel(MODEL_ID)
18
+ chat = model.start_chat()
19
+
20
+ def get_local_pdf_path():
21
+ """
22
+ Returns the path to the local PDF file.
23
+ """
24
+ try:
25
+ pdf_path = os.path.join("problems", "problems.pdf")
26
+ if not os.path.exists(pdf_path):
27
+ raise FileNotFoundError(f"{pdf_path} does not exist.")
28
+ return pdf_path
29
+ except Exception as e:
30
+ st.error(f"Failed to find the local PDF: {e}")
31
+ st.stop() # Stop if the file is not found
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 = get_local_pdf_path()
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
+ prompt = [text_prompt, st.session_state.uploaded_file_part] # Subsequent turns include the file reference
57
+
58
+ response = chat.send_message(prompt)
59
+
60
+ # Update conversation history
61
+ st.session_state.conversation_history.append({"role": "user", "content": text_prompt, "has_pdf": True})
62
+ st.session_state.conversation_history.append({"role": "assistant", "content": response.text})
63
+ return response.text
64
+
65
+ except Exception as e:
66
+ return f"An error occurred: {e}"
67
+
68
+
69
+ # --- Main Page ---
70
+ st.title("📚❓Problem Solving Tutor")
71
+ about = """
72
+ **How to use this App**
73
+ Replace this placeholder with the actual text.
74
+ """
75
+
76
+ with st.expander("How to use this App"):
77
+ st.markdown(about)
78
+
79
+ # --- Load the image ---
80
+ # image = Image.open("higher_ed.png")
81
+ # st.image(image, width=400)
82
+
83
+ st.header("Quadratic Equations")
84
+ #load the problems form the pdf
85
+ with st.spinner("AI is thinking..."):
86
+ if st.session_state.uploaded_pdf_path is None:
87
+ st.session_state.uploaded_pdf_path = get_local_pdf_path()
88
+
89
+ filepath = st.session_state.uploaded_pdf_path
90
+ text_prompt = f"Use the provided document. Create a list of math problems found in the document. Format the output as a JSON string"
91
+ response = multimodal_prompt(filepath, text_prompt) # Use the local filepath
92
+ st.markdown(response)
93
+
94
+ try:
95
+ problems = json.loads(response)
96
+ for problem in problems:
97
+ st.write(problem)
98
+ except json.JSONDecodeError:
99
+ st.write("Error: Invalid JSON format in the response.")
100
+ except Exception as e:
101
+ st.write(f"An unexpected error occurred: {e}")
102
+
103
+ st.markdown("Visit our Hugging Face Space!")
104
+ st.markdown("© 2025 WVSU AI Dev Team 🤖 ✨")
problems/problems.pdf ADDED
Binary file (54.2 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ Pillow
3
+ google-generativeai