-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_gemini.py
More file actions
122 lines (103 loc) · 4.41 KB
/
Copy pathapp_gemini.py
File metadata and controls
122 lines (103 loc) · 4.41 KB
1
2
3
4
5
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import streamlit as st
import os
import base64
import pandas as pd
from PIL import Image
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.messages import HumanMessage
def run_gemini_app():
# إعداد المفتاح
GOOGLE_API_KEY = "AIzaSyCPjAE_mjkPZ7CF4om2VwTal68Ov-WTo1c"
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
st.title("📍 Landmark Identifier | التعرف على المعالم")
# 🌐 اختيار اللغة
lang = st.radio("🌐 Language / اللغة", ["🇸🇦 العربية", "🇺🇸 English"])
# 🏷️ قاموس لكل النصوص حسب اللغة
labels = {
"🇸🇦 العربية": {
"csv": "description_ar.csv",
"prompt": "ما اسم هذا المعلم التاريخي؟",
"upload": "📁 رفع صورة",
"camera": "📸 التقاط صورة",
"processing": "⏳ يتم التعرف على المعلم...",
"success": "✅ تم التعرف على المعلم:",
"not_found": "📌 لا توجد قصة محفوظة لهذا المعلم حتى الآن.",
"story": "📖 القصة:",
"video": "🎬 الفيديو:"
},
"🇺🇸 English": {
"csv": "description_en.csv",
"prompt": "What is the name of this historical site?",
"upload": "📁 Upload Image",
"camera": "📸 Capture Image",
"processing": "⏳ Identifying the landmark...",
"success": "✅ Identified:",
"not_found": "📌 No story is currently available for this site.",
"story": "📖 Story:",
"video": "🎬 Video:"
}
}
# اختصار النصوص
L = labels[lang]
csv_file = L["csv"]
# تحميل قاعدة البيانات
@st.cache_data
def load_knowledge(file_path):
df = pd.read_csv(file_path)
df.columns = df.columns.str.strip()
return {
row["name"]: {
"description": row["description"],
"video_url": row["video_url"]
} for _, row in df.iterrows()
}
knowledge_base = load_knowledge(csv_file)
# طريقة رفع الصورة
input_method = st.radio("🎯 Source", [L["upload"], L["camera"]])
uploaded_file = None
if input_method == L["upload"]:
uploaded_file = st.file_uploader(L["upload"], type=["jpg", "jpeg", "png"])
elif input_method == L["camera"]:
uploaded_file = st.camera_input(L["camera"])
if uploaded_file:
image_bytes = uploaded_file.getvalue()
image = Image.open(uploaded_file)
st.image(image, caption="📍 Image", use_container_width=True)
image_b64 = base64.b64encode(image_bytes).decode("utf-8")
mime_type = uploaded_file.type
st.info(L["processing"])
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
temperature=0.3,
google_api_key=GOOGLE_API_KEY,
)
msg = HumanMessage(
content=[
{"type": "image_url", "image_url": {"url": f"data:{mime_type};base64,{image_b64}"}},
{"type": "text", "text": L["prompt"]}
]
)
try:
response = llm([msg])
raw_response = response.content.strip()
st.success(f"{L['success']} {raw_response}")
# تطابق الاسم داخل الرد
matched_name = None
for name in knowledge_base.keys():
if name.lower() in raw_response.lower():
matched_name = name
break
if matched_name:
st.subheader(L["story"])
st.write(knowledge_base[matched_name]["description"])
st.subheader(L["video"])
video_url = knowledge_base[matched_name]["video_url"]
if "youtube.com/shorts/" in video_url:
video_id = video_url.split("/")[-1].split("?")[0]
video_url = f"https://www.youtube.com/embed/{video_id}"
st.video(video_url)
else:
st.warning(L["not_found"])
except Exception as e:
st.error("❌ Error during landmark identification.")
st.exception(e)