Update app.py
Browse files
app.py
CHANGED
|
@@ -662,9 +662,20 @@ def upload_file():
|
|
| 662 |
|
| 663 |
# Upload to Gemini File API
|
| 664 |
try:
|
| 665 |
-
#
|
| 666 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 667 |
print(f"Document uploaded successfully: {document.name}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
except Exception as upload_error:
|
| 669 |
print(f"Upload error: {upload_error}")
|
| 670 |
return jsonify({'success': False, 'error': f'Failed to upload file to Gemini: {str(upload_error)}'})
|
|
@@ -815,9 +826,21 @@ def upload_from_url():
|
|
| 815 |
|
| 816 |
# Upload to Gemini File API
|
| 817 |
try:
|
| 818 |
-
#
|
| 819 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 820 |
print(f"Document uploaded successfully: {document.name}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 821 |
except Exception as upload_error:
|
| 822 |
print(f"Upload error: {upload_error}")
|
| 823 |
return jsonify({'success': False, 'error': f'Failed to upload file to Gemini: {str(upload_error)}'})
|
|
|
|
| 662 |
|
| 663 |
# Upload to Gemini File API
|
| 664 |
try:
|
| 665 |
+
# Save the BytesIO to a temporary file since the API expects a file path
|
| 666 |
+
import tempfile
|
| 667 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as temp_file:
|
| 668 |
+
temp_file.write(file_content)
|
| 669 |
+
temp_file_path = temp_file.name
|
| 670 |
+
|
| 671 |
+
# Upload using file path
|
| 672 |
+
document = client.files.upload(path=temp_file_path)
|
| 673 |
print(f"Document uploaded successfully: {document.name}")
|
| 674 |
+
|
| 675 |
+
# Clean up temporary file
|
| 676 |
+
import os
|
| 677 |
+
os.unlink(temp_file_path)
|
| 678 |
+
|
| 679 |
except Exception as upload_error:
|
| 680 |
print(f"Upload error: {upload_error}")
|
| 681 |
return jsonify({'success': False, 'error': f'Failed to upload file to Gemini: {str(upload_error)}'})
|
|
|
|
| 826 |
|
| 827 |
# Upload to Gemini File API
|
| 828 |
try:
|
| 829 |
+
# Save the BytesIO to a temporary file since the API expects a file path
|
| 830 |
+
import tempfile
|
| 831 |
+
import os
|
| 832 |
+
|
| 833 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as temp_file:
|
| 834 |
+
temp_file.write(response.content)
|
| 835 |
+
temp_file_path = temp_file.name
|
| 836 |
+
|
| 837 |
+
# Upload using file path
|
| 838 |
+
document = client.files.upload(path=temp_file_path)
|
| 839 |
print(f"Document uploaded successfully: {document.name}")
|
| 840 |
+
|
| 841 |
+
# Clean up temporary file
|
| 842 |
+
os.unlink(temp_file_path)
|
| 843 |
+
|
| 844 |
except Exception as upload_error:
|
| 845 |
print(f"Upload error: {upload_error}")
|
| 846 |
return jsonify({'success': False, 'error': f'Failed to upload file to Gemini: {str(upload_error)}'})
|