Upload 4 files
Browse files- cd.json +13 -0
- database.py +23 -0
- requirements.txt +0 -0
- upload.py +91 -0
cd.json
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"type": "service_account",
|
3 |
+
"project_id": "bijay-c173f",
|
4 |
+
"private_key_id": "9df1ed7f7c5f1c41055e509a480d8e8be1ae56f6",
|
5 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEugIBADANBgkqhkiG9w0BAQEFAASCBKQwggSgAgEAAoIBAQClV9jABJzcK3vs\n+Lpflu0eGzt7ioI7qZNffnl8mm11e0YqSSJ95ZfV6DSydTx3kdpgG1pXdgWy9Vbu\nqIWuB5Hqf7MkZbsCat2nzmP8it3BeGyMaPSbUMhplRHeniTOcDvkO7PAVY/PKoIU\nrEDEvQCkVlII6IatbowSTG+OWjWkobJtdPK68WzFKsbAPJ4B79ITfwa1MXFD3TNn\nlRKsmVxteIvEP4W0nguc8FhkRlzf6OgnoGEcqbuVQHEgwz3V7soxFYquXRJ+ZTOG\nwQ0mUe53U1y1aiYfrWMP0/Lg6YkSv7ZhFvWrghh2R1f6YJHtSGi2SGcLHRrcFsrw\nA/Fol+6jAgMBAAECgf8IvrCsmGTIu5vZjrvmrV9DOn/+BcKn/xrhJLOqBQqwmvwO\ntdqUp8YC7jH4O8acXLg8Yv3Vw4uJnpCQmXfJVhDUAnsPvrlu7UCHCyJk8SE3ILob\nuq29V+IsaWdNcFjzH2G4MWDbfKbBYIf1Q2eoRr6NpE34LTMGukfcMgbC4udSrBZy\n7bxACyATZVhEGMnY5ZYTUyZKkM/0bsMoO8VncXmswntvz2wSZFYmBac7H99BMBib\ngWmGgAxQ3i/Ye7rDNaFCQwOo4skkfbuLR3KwmZDsJdtSLJpeoWFH2Ah5k5HKPy1W\n6iXSuXIG+T3nwkWVofCpyq6nogHSyvgotMmf8ckCgYEA1P98mHQHbcCl7eGah6Ga\n++N8ald6u2CP80KYI0IcA58iW+T8qPelARGsGGspPLQ9cFrmI9Rox0GrHorR+L20\nKB7kQmJEPbLS/pU9ka+Zk3Hf1BMSfWuhi/99ByubbOAa++XYYGqlyyVTC+0uVuQ9\nkms95hfkakmcJUiQlJEfLfkCgYEAxrliQmshphRjNGBBVBSvkscAhcnEhKmPZx5Q\ni/ygKmIPwW+jkBqoZIu1zCIP9w0LcX5kYKCwKNSfEaZCawDCLa2aAxWLrsn61FQx\noMwnQHfvrzGqOID2WTON0w5wurjVdzVzTiyiJWnyUbumauCjg3g380cmxbhGaLEy\nSp4ZmHsCgYBLjpXUPtSjykMSL4Vxt5IS6I+p6X5QGeD2mhj96OWnIzw8/O3jwnV1\n+57gyqLGNenVgWlCLIc+p7zcCMKTSjguQ0cVhWZPvK+8P5SnqXMBKtQLlvhoJVYZ\nI3hmosFvzc/Eq7Ql/67wcT0Ubf6SNETjO8epZ7BPQ+1wDF29Vf2JgQKBgEunjAbp\nLjhCDrqD4psV/PmOFV7Zv50ZyGRjoBqLJJa+Gm10CPr1iyIsJhv+48EUEEU27xPy\nWN5Sg/R+LU77TPGC491EuJbkgj+Phg1Ha7IZdwBlkf5M6nxvKhEtsDEF6Q6nJKyg\nRWKlAQGoxORvfRCyFeriLZqcWwHEY/cQVYI9AoGAFYrYsujk9fvCX9mMU7qFe27d\n9PNEaLoSweezEQUtTxEtuIM49nyVmtg3zOUx9tH+B6EEKY66HB2J92EZJ8gK59U3\nfd0nNQ/sw+RkClIfH1Muv7NmAINpb9eva5H7EcfrCMZlBiS8//ABWXSVW3WgBvmT\n8oca5n/CHQHftfzZePM=\n-----END PRIVATE KEY-----\n",
|
6 |
+
"client_email": "[email protected]",
|
7 |
+
"client_id": "102116671708400313176",
|
8 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
9 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
10 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
11 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-fbsvc%40bijay-c173f.iam.gserviceaccount.com",
|
12 |
+
"universe_domain": "googleapis.com"
|
13 |
+
}
|
database.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import firebase_admin
|
2 |
+
from firebase_admin import credentials, firestore
|
3 |
+
|
4 |
+
def init_firebase():
|
5 |
+
"""Initialize Firebase Admin SDK if not already initialized."""
|
6 |
+
try:
|
7 |
+
# Check if Firebase app is already initialized
|
8 |
+
if not firebase_admin._apps:
|
9 |
+
# Load Firebase credentials
|
10 |
+
cred = credentials.Certificate("firebase-key.json")
|
11 |
+
firebase_admin.initialize_app(cred)
|
12 |
+
print("Firebase initialized successfully!")
|
13 |
+
else:
|
14 |
+
print("Firebase already initialized.")
|
15 |
+
|
16 |
+
# Return Firestore client
|
17 |
+
return firestore.client()
|
18 |
+
except Exception as e:
|
19 |
+
print(f"Error initializing Firebase: {e}")
|
20 |
+
raise
|
21 |
+
|
22 |
+
# Initialize Firestore
|
23 |
+
db = init_firebase()
|
requirements.txt
ADDED
Binary file (220 Bytes). View file
|
|
upload.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from database import db # Import Firestore client
|
3 |
+
from firebase_admin import firestore
|
4 |
+
from PyPDF2 import PdfReader
|
5 |
+
import easyocr
|
6 |
+
from PIL import Image
|
7 |
+
from io import BytesIO
|
8 |
+
import random
|
9 |
+
import string
|
10 |
+
|
11 |
+
def generate_readable_id(length=5):
|
12 |
+
"""Generate a random alphanumeric ID."""
|
13 |
+
characters = string.ascii_letters + string.digits # A-Z, a-z, 0-9
|
14 |
+
return ''.join(random.choice(characters) for _ in range(length))
|
15 |
+
|
16 |
+
def extract_text_from_pdf(file):
|
17 |
+
"""Extract text from a PDF file."""
|
18 |
+
reader = PdfReader(file)
|
19 |
+
text = ""
|
20 |
+
for page in reader.pages:
|
21 |
+
text += page.extract_text() or ""
|
22 |
+
return text
|
23 |
+
|
24 |
+
def extract_text_from_image(image):
|
25 |
+
"""Extract text from an image using EasyOCR."""
|
26 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
27 |
+
results = reader.readtext(image)
|
28 |
+
return " ".join([result[1] for result in results])
|
29 |
+
|
30 |
+
def save_text_to_firebase(text):
|
31 |
+
"""Save text to Firebase Firestore."""
|
32 |
+
try:
|
33 |
+
# Generate a readable ID for the text
|
34 |
+
text_id = generate_readable_id()
|
35 |
+
|
36 |
+
# Save the text to Firestore
|
37 |
+
db.collection("texts").document(text_id).set({
|
38 |
+
"content": text,
|
39 |
+
"created_at": firestore.SERVER_TIMESTAMP
|
40 |
+
})
|
41 |
+
return text_id
|
42 |
+
except Exception as e:
|
43 |
+
st.error(f"Error saving text to Firebase: {e}")
|
44 |
+
return None
|
45 |
+
|
46 |
+
def main():
|
47 |
+
st.title("Text Upload App")
|
48 |
+
|
49 |
+
# Select input method
|
50 |
+
option = st.radio("Choose input method", ("Upload Document", "Take Picture"))
|
51 |
+
|
52 |
+
if option == "Upload Document":
|
53 |
+
uploaded_file = st.file_uploader("Upload PDF or Image", type=["pdf", "png", "jpg", "jpeg"])
|
54 |
+
|
55 |
+
if uploaded_file:
|
56 |
+
if uploaded_file.type == "application/pdf":
|
57 |
+
# Extract text from PDF
|
58 |
+
text = extract_text_from_pdf(uploaded_file)
|
59 |
+
else:
|
60 |
+
# Extract text from image
|
61 |
+
image = Image.open(uploaded_file)
|
62 |
+
text = extract_text_from_image(image)
|
63 |
+
|
64 |
+
if text.strip():
|
65 |
+
# Save text to Firebase
|
66 |
+
text_id = save_text_to_firebase(text)
|
67 |
+
if text_id:
|
68 |
+
st.success(f"Text extracted! Your Document ID: **{text_id}**")
|
69 |
+
# st.image(f"https://api.qrserver.com/v1/create-qr-code/?size=150x150&data={text_id}")
|
70 |
+
else:
|
71 |
+
st.warning("No text could be extracted")
|
72 |
+
|
73 |
+
elif option == "Take Picture":
|
74 |
+
picture = st.camera_input("Take a picture")
|
75 |
+
|
76 |
+
if picture:
|
77 |
+
# Extract text from image
|
78 |
+
image = Image.open(BytesIO(picture.getvalue()))
|
79 |
+
text = extract_text_from_image(image)
|
80 |
+
|
81 |
+
if text.strip():
|
82 |
+
# Save text to Firebase
|
83 |
+
text_id = save_text_to_firebase(text)
|
84 |
+
if text_id:
|
85 |
+
st.success(f"Text extracted! Your Document ID: **{text_id}**")
|
86 |
+
# st.image(f"https://api.qrserver.com/v1/create-qr-code/?size=150x150&data={text_id}")
|
87 |
+
else:
|
88 |
+
st.warning("No text could be extracted")
|
89 |
+
|
90 |
+
if __name__ == "__main__":
|
91 |
+
main()
|