sync with remote
Browse files
app.py
CHANGED
@@ -38,18 +38,29 @@ if "uploaded_file_part" not in st.session_state: # Store the file *part*
|
|
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
|
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
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
@@ -58,7 +69,7 @@ def multimodal_prompt(pdf_path, text_prompt):
|
|
58 |
response = chat.send_message(prompt)
|
59 |
|
60 |
# Update conversation history
|
61 |
-
st.session_state.conversation_history.append({"role": "user", "content": text_prompt, "
|
62 |
st.session_state.conversation_history.append({"role": "assistant", "content": response.text})
|
63 |
return response.text
|
64 |
|
@@ -91,61 +102,29 @@ Example JSON output:
|
|
91 |
]
|
92 |
"""
|
93 |
|
94 |
-
# Define a function to extract equations from the AI response
|
95 |
-
def extract_equations(response):
|
96 |
-
try:
|
97 |
-
if isinstance(response, str):
|
98 |
-
response = response.strip().replace("\n", "").replace("\r", "")
|
99 |
-
if response.lower().startswith("json"):
|
100 |
-
response = response[4:].strip()
|
101 |
-
if response.startswith("[") and response.endswith("]"):
|
102 |
-
return json.loads(response)
|
103 |
-
else:
|
104 |
-
st.error("Error: AI response is not in expected JSON list format.")
|
105 |
-
return []
|
106 |
-
elif isinstance(response, list):
|
107 |
-
return response
|
108 |
-
else:
|
109 |
-
st.error("Error: Unexpected response format from AI.")
|
110 |
-
return []
|
111 |
-
except json.JSONDecodeError:
|
112 |
-
st.error("Error: Failed to parse AI response as a list.")
|
113 |
-
return []
|
114 |
-
|
115 |
-
# Define a function to extract quadratic equations from the problems
|
116 |
-
def extract_quadratic_equations(problems):
|
117 |
-
equations = []
|
118 |
-
for problem in problems:
|
119 |
-
match = re.search(r'([0-9x\^\+\-\=\s]+)', problem)
|
120 |
-
if match:
|
121 |
-
equations.append(match.group(1).strip())
|
122 |
-
else:
|
123 |
-
st.warning(f"Could not extract equation from: '{problem}'")
|
124 |
-
return equations
|
125 |
-
|
126 |
# Main code
|
127 |
with st.spinner("AI is thinking..."):
|
128 |
if st.session_state.get("uploaded_pdf_path") is None:
|
129 |
st.session_state.uploaded_pdf_path = get_local_pdf_path()
|
130 |
|
131 |
filepath = st.session_state.uploaded_pdf_path
|
132 |
-
response = multimodal_prompt(filepath, TEXT_PROMPT)
|
|
|
|
|
|
|
|
|
133 |
|
134 |
-
#
|
135 |
-
|
136 |
|
137 |
-
#
|
138 |
-
|
139 |
-
|
140 |
-
equations = extract_quadratic_equations(problems)
|
141 |
-
st.write("Extracted Equations:")
|
142 |
-
for equation in equations:
|
143 |
-
st.write(equation)
|
144 |
-
else:
|
145 |
-
st.error("Error: No valid equations extracted.")
|
146 |
|
|
|
|
|
147 |
except Exception as e:
|
148 |
-
st.
|
149 |
|
150 |
|
151 |
st.markdown("Visit our Hugging Face Space!")
|
|
|
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, file_type="PDF"):
|
42 |
"""
|
43 |
Sends a multimodal prompt to Gemini, handling file uploads efficiently.
|
44 |
Args:
|
45 |
+
pdf_path: The path to the file (PDF or image).
|
46 |
text_prompt: The text prompt for the model.
|
47 |
+
file_type: "PDF" or "image" to specify the file type.
|
48 |
Returns:
|
49 |
The model's response as a string, or an error message.
|
50 |
"""
|
51 |
try:
|
52 |
+
if file_type == "PDF":
|
53 |
+
mime_type = "application/pdf"
|
54 |
+
elif file_type == "image":
|
55 |
+
import mimetypes
|
56 |
+
mime_type, _ = mimetypes.guess_type(pdf_path)
|
57 |
+
if mime_type is None:
|
58 |
+
return "Could not determine MIME type for image. Please check the file path or type."
|
59 |
+
else:
|
60 |
+
return "Invalid file_type. Must be 'PDF' or 'image'."
|
61 |
+
|
62 |
+
if st.session_state.get("uploaded_file_part") is None: # First time, upload
|
63 |
+
pdf_part = genai.upload_file(pdf_path, mime_type=mime_type)
|
64 |
st.session_state.uploaded_file_part = pdf_part
|
65 |
prompt = [text_prompt, pdf_part] # First turn includes the actual file
|
66 |
else: # Subsequent turns, reference the file
|
|
|
69 |
response = chat.send_message(prompt)
|
70 |
|
71 |
# Update conversation history
|
72 |
+
st.session_state.conversation_history.append({"role": "user", "content": text_prompt, "has_file": True})
|
73 |
st.session_state.conversation_history.append({"role": "assistant", "content": response.text})
|
74 |
return response.text
|
75 |
|
|
|
102 |
]
|
103 |
"""
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
# Main code
|
106 |
with st.spinner("AI is thinking..."):
|
107 |
if st.session_state.get("uploaded_pdf_path") is None:
|
108 |
st.session_state.uploaded_pdf_path = get_local_pdf_path()
|
109 |
|
110 |
filepath = st.session_state.uploaded_pdf_path
|
111 |
+
response = multimodal_prompt(filepath, TEXT_PROMPT, file_type="PDF")
|
112 |
+
|
113 |
+
# Remove the ```json and ``` and extra spaces.
|
114 |
+
try:
|
115 |
+
json_string = response.replace('```json', '').replace('```', '').strip()
|
116 |
|
117 |
+
# Parse the JSON string into a Python list.
|
118 |
+
problems_list = json.loads(json_string)
|
119 |
|
120 |
+
# Iterate over the list and print each item using st.write().
|
121 |
+
for item in problems_list:
|
122 |
+
st.write(item)
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
except json.JSONDecodeError:
|
125 |
+
st.write("Error: Invalid JSON format in the response.")
|
126 |
except Exception as e:
|
127 |
+
st.write(f"An unexpected error occurred: {e}")
|
128 |
|
129 |
|
130 |
st.markdown("Visit our Hugging Face Space!")
|