Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -169,17 +169,6 @@ def home():
|
|
169 |
return render_template('index.html')
|
170 |
|
171 |
|
172 |
-
@app.route('/api-status', methods=['GET'])
|
173 |
-
def api_status():
|
174 |
-
"""Endpoint to check API status"""
|
175 |
-
api_working = test_gemini_api()
|
176 |
-
return jsonify({
|
177 |
-
'status': 'ok' if api_working else 'error',
|
178 |
-
'api_working': api_working,
|
179 |
-
'message': 'Gemini API is working correctly' if api_working else 'Gemini API is not working'
|
180 |
-
})
|
181 |
-
|
182 |
-
|
183 |
@app.route('/upload', methods=['POST'])
|
184 |
def upload_file():
|
185 |
print("Received upload request")
|
@@ -200,20 +189,33 @@ def upload_file():
|
|
200 |
|
201 |
temp_path = None
|
202 |
try:
|
203 |
-
# Check API status before proceeding
|
204 |
-
if not test_gemini_api():
|
205 |
-
return jsonify({'error': 'Gemini API is not working. Please check your API key and try again.'}), 500
|
206 |
-
|
207 |
# Create temp directory if it doesn't exist
|
208 |
temp_dir = "temp"
|
209 |
-
|
210 |
-
|
211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
|
213 |
# Save the uploaded file temporarily with a unique name
|
214 |
-
|
|
|
|
|
|
|
|
|
215 |
file.save(temp_path)
|
216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
217 |
|
218 |
# Extract text using Gemini
|
219 |
print("Starting text extraction...")
|
@@ -244,6 +246,7 @@ def upload_file():
|
|
244 |
print(f"Removed temporary file: {temp_path}")
|
245 |
except Exception as e:
|
246 |
print(f"Failed to remove temporary file: {str(e)}")
|
|
|
247 |
|
248 |
|
249 |
if __name__ == '__main__':
|
|
|
169 |
return render_template('index.html')
|
170 |
|
171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
@app.route('/upload', methods=['POST'])
|
173 |
def upload_file():
|
174 |
print("Received upload request")
|
|
|
189 |
|
190 |
temp_path = None
|
191 |
try:
|
|
|
|
|
|
|
|
|
192 |
# Create temp directory if it doesn't exist
|
193 |
temp_dir = "temp"
|
194 |
+
os.makedirs(temp_dir, exist_ok=True)
|
195 |
+
print(f"Ensuring temp directory exists: {temp_dir}")
|
196 |
+
|
197 |
+
# Make sure the temp directory has write permissions
|
198 |
+
try:
|
199 |
+
if not os.access(temp_dir, os.W_OK):
|
200 |
+
os.chmod(temp_dir, 0o755) # chmod to ensure write permissions
|
201 |
+
print(f"Updated permissions for temp directory: {temp_dir}")
|
202 |
+
except Exception as perm_error:
|
203 |
+
print(f"Warning: Could not update permissions: {str(perm_error)}")
|
204 |
|
205 |
# Save the uploaded file temporarily with a unique name
|
206 |
+
temp_filename = f"temp_image_{int(time.time())}.png"
|
207 |
+
temp_path = os.path.join(temp_dir, temp_filename)
|
208 |
+
print(f"Saving uploaded file to {temp_path}")
|
209 |
+
|
210 |
+
# Save in a way that ensures we have write permissions
|
211 |
file.save(temp_path)
|
212 |
+
|
213 |
+
# Ensure the file has appropriate permissions
|
214 |
+
try:
|
215 |
+
os.chmod(temp_path, 0o644) # Make the file readable
|
216 |
+
print(f"Updated permissions for file: {temp_path}")
|
217 |
+
except Exception as file_perm_error:
|
218 |
+
print(f"Warning: Could not update file permissions: {str(file_perm_error)}")
|
219 |
|
220 |
# Extract text using Gemini
|
221 |
print("Starting text extraction...")
|
|
|
246 |
print(f"Removed temporary file: {temp_path}")
|
247 |
except Exception as e:
|
248 |
print(f"Failed to remove temporary file: {str(e)}")
|
249 |
+
# Don't let this failure affect the response
|
250 |
|
251 |
|
252 |
if __name__ == '__main__':
|