Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +9 -44
src/streamlit_app.py
CHANGED
|
@@ -1,10 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
import requests
|
| 4 |
-
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 5 |
from io import BytesIO
|
| 6 |
-
import torch
|
| 7 |
-
import torchvision.transforms as T
|
| 8 |
|
| 9 |
st.set_page_config(page_title="WikiExplorer AR", layout="centered")
|
| 10 |
st.title("π· WikiExplorer AR (Streamlit Edition)")
|
|
@@ -23,47 +20,16 @@ lang = st.selectbox(
|
|
| 23 |
|
| 24 |
lang_code = lang[1]
|
| 25 |
|
| 26 |
-
# ---
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
processor, model = load_trocr()
|
| 34 |
-
|
| 35 |
-
# --- Camera input (main source of place name) ---
|
| 36 |
-
st.markdown("**πΈ Capture a place name from signage, poster, or board:**")
|
| 37 |
-
img_file_buffer = st.camera_input("Take a picture")
|
| 38 |
-
|
| 39 |
-
# --- Optional text input if OCR fails ---
|
| 40 |
-
place_name = st.text_input("π Or manually enter the place name (optional)")
|
| 41 |
-
|
| 42 |
-
# --- OCR from captured image ---
|
| 43 |
-
def run_trocr_ocr(image_data):
|
| 44 |
-
image = Image.open(image_data).convert("RGB")
|
| 45 |
-
transform = T.Compose([
|
| 46 |
-
T.Resize((384, 384)),
|
| 47 |
-
T.ToTensor()
|
| 48 |
-
])
|
| 49 |
-
pixel_values = transform(image).unsqueeze(0)
|
| 50 |
-
generated_ids = model.generate(pixel_values)
|
| 51 |
-
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 52 |
-
return text.strip()
|
| 53 |
|
| 54 |
if img_file_buffer is not None:
|
| 55 |
st.markdown("### π· Captured Image")
|
| 56 |
st.image(img_file_buffer, caption="Uploaded via camera", use_column_width=True)
|
| 57 |
-
try:
|
| 58 |
-
with st.spinner("π§ Running OCR..."):
|
| 59 |
-
ocr_text = run_trocr_ocr(BytesIO(img_file_buffer.getvalue()))
|
| 60 |
-
if ocr_text:
|
| 61 |
-
place_name = ocr_text
|
| 62 |
-
st.success(f"π§ OCR detected: **{place_name}**")
|
| 63 |
-
else:
|
| 64 |
-
st.warning("OCR ran but could not extract any meaningful text.")
|
| 65 |
-
except Exception as e:
|
| 66 |
-
st.error(f"OCR failed: {e}")
|
| 67 |
|
| 68 |
# --- Translation helpers ---
|
| 69 |
def translate_text(text, target_lang):
|
|
@@ -159,9 +125,8 @@ if place_name.strip():
|
|
| 159 |
# --- Footer ---
|
| 160 |
st.markdown("""
|
| 161 |
---
|
| 162 |
-
- πΈ
|
| 163 |
-
- βοΈ Optional manual input if OCR fails.
|
| 164 |
- π Wikipedia multilingual summary with fallback + sentence-level translation.
|
| 165 |
- πΌοΈ Commons image gallery integration.
|
| 166 |
-
- β
Works in Hugging Face Spaces with Streamlit
|
| 167 |
-
""")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
import requests
|
|
|
|
| 4 |
from io import BytesIO
|
|
|
|
|
|
|
| 5 |
|
| 6 |
st.set_page_config(page_title="WikiExplorer AR", layout="centered")
|
| 7 |
st.title("π· WikiExplorer AR (Streamlit Edition)")
|
|
|
|
| 20 |
|
| 21 |
lang_code = lang[1]
|
| 22 |
|
| 23 |
+
# --- Place name input ---
|
| 24 |
+
st.markdown("**π Enter a place or person name to learn more:**")
|
| 25 |
+
place_name = st.text_input("ποΈ For example: Charminar, Taj Mahal, Shah Jahan")
|
| 26 |
+
|
| 27 |
+
# --- Camera input (optional) ---
|
| 28 |
+
img_file_buffer = st.camera_input("πΈ Take a picture (optional)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
if img_file_buffer is not None:
|
| 31 |
st.markdown("### π· Captured Image")
|
| 32 |
st.image(img_file_buffer, caption="Uploaded via camera", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# --- Translation helpers ---
|
| 35 |
def translate_text(text, target_lang):
|
|
|
|
| 125 |
# --- Footer ---
|
| 126 |
st.markdown("""
|
| 127 |
---
|
| 128 |
+
- πΈ Capture photo optionally β main input is typed place name.
|
|
|
|
| 129 |
- π Wikipedia multilingual summary with fallback + sentence-level translation.
|
| 130 |
- πΌοΈ Commons image gallery integration.
|
| 131 |
+
- β
Works in Hugging Face Spaces with Streamlit.
|
| 132 |
+
""")
|