Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,56 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from PIL import Image
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
st.
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
st.
|
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.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|