leyla95 commited on
Commit
38477c8
ยท
verified ยท
1 Parent(s): eeb18e3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from ultralytics import YOLO
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
5
+ import torch
6
+
7
+ # Load YOLO model
8
+ yolo_model = YOLO('runs/detect/makkah-yolo/weights/best.pt') # Replace with your relative path or uploaded model if needed
9
+
10
+ # Load ALLaM in 8-bit to reduce memory
11
+ bnb_config = BitsAndBytesConfig(load_in_8bit=True)
12
+ llm_model = AutoModelForCausalLM.from_pretrained(
13
+ "ALLaM-AI/ALLaM-7B-Instruct-preview",
14
+ quantization_config=bnb_config,
15
+ device_map="auto",
16
+ trust_remote_code=True
17
+ )
18
+ tokenizer = AutoTokenizer.from_pretrained("ALLaM-AI/ALLaM-7B-Instruct-preview", trust_remote_code=True)
19
+
20
+ # Streamlit App UI
21
+ st.title("๐Ÿ”Ž๐Ÿ•‹ Makkah Landmark Detection + Arabic Cultural Explanation")
22
+ st.write("Upload an image to detect landmarks in Makkah and receive a cultural explanation in Arabic using ALLaM.")
23
+
24
+ uploaded_file = st.file_uploader("๐Ÿ“ท Upload an image...", type=["jpg", "jpeg", "png"])
25
+
26
+ if uploaded_file:
27
+ img = Image.open(uploaded_file)
28
+ st.image(img, caption="Uploaded Image", use_column_width=True)
29
+
30
+ temp_image_path = "temp_uploaded_image.jpg"
31
+ img.save(temp_image_path)
32
+
33
+ with st.spinner("๐Ÿ” Detecting landmarks..."):
34
+ results = yolo_model.predict(temp_image_path, conf=0.25, save=False)
35
+
36
+ detections = results[0].names
37
+ boxes = results[0].boxes.cls
38
+ detected_classes = [detections[int(cls_id)] for cls_id in boxes]
39
+
40
+ if detected_classes:
41
+ landmarks = 'ุŒ '.join(detected_classes)
42
+ user_prompt = f"ู…ู† ูุถู„ูƒุŒ ุชุญุฏุซ ุนู† ุฃู‡ู…ูŠุฉ ุงู„ู…ุนุงู„ู… ุงู„ุชุงู„ูŠุฉ ููŠ ู…ูƒุฉ ุงู„ู…ูƒุฑู…ุฉ: {landmarks}."
43
+
44
+ st.write("๐Ÿ“œ **Arabic Prompt:**")
45
+ st.info(user_prompt)
46
+
47
+ with st.spinner("๐Ÿค– Generating Arabic explanation..."):
48
+ messages = [{"role": "user", "content": user_prompt}]
49
+ inputs = tokenizer.apply_chat_template(messages, tokenize=False)
50
+ inputs = tokenizer(inputs, return_tensors='pt', return_token_type_ids=False)
51
+ inputs = {k: v.to('cuda') for k, v in inputs.items()}
52
+
53
+ response = llm_model.generate(
54
+ **inputs,
55
+ max_new_tokens=512,
56
+ do_sample=True,
57
+ top_k=50,
58
+ top_p=0.95,
59
+ temperature=0.6
60
+ )
61
+
62
+ output_text = tokenizer.batch_decode(response, skip_special_tokens=True)[0]
63
+ st.write("๐Ÿ•Œ **Explanation in Arabic:**")
64
+ st.success(output_text)
65
+ else:
66
+ st.warning("๐Ÿšซ No landmarks detected in this image.")