Imran08 commited on
Commit
8dd238c
Β·
verified Β·
1 Parent(s): 1ddb93c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -63
app.py CHANGED
@@ -1,63 +1,56 @@
1
- import streamlit as st
2
- from PIL import Image
3
- import easyocr
4
- import os
5
- import datetime
6
-
7
- # Initialize OCR reader
8
- reader = easyocr.Reader(['en', 'hi', 'te', 'ta', 'bn', 'ml', 'gu', 'mr'], gpu=False)
9
-
10
- # Create a directory to save uploads
11
- UPLOAD_DIR = "uploaded_data"
12
- os.makedirs(UPLOAD_DIR, exist_ok=True)
13
-
14
- st.set_page_config(page_title="Indian Landmark Mapper", layout="centered")
15
-
16
- st.title("πŸ“ Indian Landmark Mapper")
17
- st.write("Help preserve India's heritage. Upload a local landmark photo and describe it in your native language.")
18
-
19
- # File uploader
20
- image_file = st.file_uploader("Upload a photo of the landmark", type=['jpg', 'jpeg', 'png'])
21
-
22
- # Text input
23
- description = st.text_area("Write a short description in your local language")
24
-
25
- # Optional: OCR toggle
26
- run_ocr = st.checkbox("Run OCR on uploaded image (optional)")
27
-
28
- # Optional: Location
29
- location = st.text_input("Enter location (optional: village, town, district)")
30
-
31
- # Submission button
32
- if st.button("Submit"):
33
- if image_file is None or not description.strip():
34
- st.warning("Please upload an image and enter a description.")
35
- else:
36
- # Save image
37
- timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
38
- image_path = os.path.join(UPLOAD_DIR, f"{timestamp}_{image_file.name}")
39
- with open(image_path, "wb") as f:
40
- f.write(image_file.getbuffer())
41
-
42
- # Save metadata
43
- metadata = {
44
- "description": description.strip(),
45
- "location": location,
46
- "image_filename": image_path,
47
- "timestamp": timestamp
48
- }
49
-
50
- # In real deployment, replace this with saving to Hugging Face dataset or DB
51
- st.success("βœ… Submission received! Thank you for preserving your local heritage.")
52
- st.json(metadata)
53
-
54
- # OCR Section
55
- if image_file and run_ocr:
56
- st.subheader("🧠 OCR Result")
57
- img = Image.open(image_file)
58
- st.image(img, caption="Uploaded Image", use_column_width=True)
59
-
60
- with st.spinner("Running OCR..."):
61
- result = reader.readtext(np.array(img))
62
- extracted_text = "\n".join([line[1] for line in result])
63
- st.code(extracted_text)
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import pytesseract
4
+ from transformers import RagRetriever, RagTokenForGeneration, RagTokenizer
5
+ from datasets import load_dataset
6
+
7
+ # --- App Title ---
8
+ st.set_page_config(page_title="Landmark Mapper", layout="wide")
9
+ st.title("πŸ—ΊοΈ Landmark Mapper - Discover, Describe & Contribute")
10
+
11
+ # --- Image Upload ---
12
+ uploaded_file = st.file_uploader("Upload a landmark image", type=["jpg", "jpeg", "png"])
13
+
14
+ # --- OCR + Description Input ---
15
+ description = ""
16
+ if uploaded_file:
17
+ image = Image.open(uploaded_file)
18
+ st.image(image, caption="Uploaded Image", use_column_width=True)
19
+
20
+ if st.checkbox("Run OCR to extract text from image"):
21
+ with st.spinner("Extracting text..."):
22
+ ocr_text = pytesseract.image_to_string(image)
23
+ st.text_area("Extracted Text", ocr_text, height=100)
24
+
25
+ description = st.text_area("Enter a description in your local language", height=150)
26
+
27
+ # --- RAG Integration ---
28
+ if st.button("Analyze with AI") and description:
29
+ with st.spinner("Running RAG model..."):
30
+ # Load RAG model components
31
+ tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-base")
32
+ model = RagTokenForGeneration.from_pretrained("facebook/rag-token-base")
33
+ retriever = RagRetriever.from_pretrained("facebook/rag-token-base", index_name="legacy")
34
+
35
+ # Encode and retrieve
36
+ input_dict = tokenizer.prepare_seq2seq_batch(description, return_tensors="pt")
37
+ input_dict["input_ids"] = input_dict["input_ids"][:, :128] # limit input length
38
+ input_dict["retrieval_kwargs"] = {"n_docs": 5}
39
+
40
+ generated = model.generate(
41
+ input_ids=input_dict["input_ids"],
42
+ context_input_ids=None,
43
+ context_attention_mask=None,
44
+ num_beams=2,
45
+ min_length=30,
46
+ max_length=128
47
+ )
48
+
49
+ output = tokenizer.batch_decode(generated, skip_special_tokens=True)
50
+ st.subheader("πŸ“˜ AI-Enhanced Landmark Info")
51
+ st.write(output[0])
52
+
53
+ # --- Corpus Contribution ---
54
+ if description:
55
+ st.success("βœ… Thank you! Your description is now part of the landmark language corpus.")
56
+ st.markdown("Help us map Indian culture, one landmark at a time.")