Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,14 +29,14 @@ LANGUAGES = {
|
|
| 29 |
}
|
| 30 |
|
| 31 |
def configure_gemini():
|
| 32 |
-
"""Configure Gemini API"""
|
| 33 |
-
api_key = st.session_state.get('api_key')
|
| 34 |
-
if not api_key:
|
| 35 |
-
return False
|
| 36 |
-
|
| 37 |
try:
|
|
|
|
| 38 |
genai.configure(api_key=api_key)
|
| 39 |
return True
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
st.error(f"Failed to configure Gemini API: {str(e)}")
|
| 42 |
return False
|
|
@@ -96,28 +96,33 @@ def get_image_description(image: Image.Image, language: str) -> Optional[str]:
|
|
| 96 |
except Exception as e:
|
| 97 |
return f"Error describing image: {str(e)}"
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
def main():
|
| 100 |
st.title("πΌοΈ Image Q&A Assistant")
|
| 101 |
st.markdown("Upload an image and ask questions about it in multiple languages!")
|
| 102 |
|
| 103 |
-
#
|
|
|
|
|
|
|
|
|
|
| 104 |
with st.sidebar:
|
| 105 |
st.header("βοΈ Settings")
|
| 106 |
|
| 107 |
-
# API
|
| 108 |
-
|
| 109 |
-
"Gemini API
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
key="api_key_input"
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
if api_key:
|
| 116 |
-
st.session_state.api_key = api_key
|
| 117 |
-
if configure_gemini():
|
| 118 |
-
st.success("β
API key configured successfully!")
|
| 119 |
-
else:
|
| 120 |
-
st.error("β Invalid API key")
|
| 121 |
|
| 122 |
st.markdown("---")
|
| 123 |
|
|
@@ -174,7 +179,7 @@ def main():
|
|
| 174 |
with col2:
|
| 175 |
st.subheader("π¬ Ask Questions")
|
| 176 |
|
| 177 |
-
if 'current_image' in st.session_state and
|
| 178 |
# Question input
|
| 179 |
question = st.text_area(
|
| 180 |
"Your question about the image:",
|
|
@@ -204,6 +209,9 @@ def main():
|
|
| 204 |
LANGUAGES[selected_language]
|
| 205 |
)
|
| 206 |
|
|
|
|
|
|
|
|
|
|
| 207 |
st.subheader("π― Analysis Result")
|
| 208 |
st.write(result)
|
| 209 |
|
|
@@ -214,6 +222,9 @@ def main():
|
|
| 214 |
LANGUAGES[selected_language]
|
| 215 |
)
|
| 216 |
|
|
|
|
|
|
|
|
|
|
| 217 |
st.subheader("π Image Description")
|
| 218 |
st.write(description)
|
| 219 |
|
|
@@ -223,8 +234,8 @@ def main():
|
|
| 223 |
elif 'current_image' not in st.session_state:
|
| 224 |
st.info("π· Please upload an image first.")
|
| 225 |
|
| 226 |
-
elif not
|
| 227 |
-
st.warning("π Please
|
| 228 |
|
| 229 |
# Results history (optional feature)
|
| 230 |
if st.checkbox("π Show Analysis History"):
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
def configure_gemini():
|
| 32 |
+
"""Configure Gemini API using secrets"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
try:
|
| 34 |
+
api_key = st.secrets["GEMINI_API_KEY"]
|
| 35 |
genai.configure(api_key=api_key)
|
| 36 |
return True
|
| 37 |
+
except KeyError:
|
| 38 |
+
st.error("β GEMINI_API_KEY not found in secrets. Please add it to your Streamlit secrets.")
|
| 39 |
+
return False
|
| 40 |
except Exception as e:
|
| 41 |
st.error(f"Failed to configure Gemini API: {str(e)}")
|
| 42 |
return False
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
return f"Error describing image: {str(e)}"
|
| 98 |
|
| 99 |
+
def save_to_history(question: str, answer: str, language: str):
|
| 100 |
+
"""Save analysis to history"""
|
| 101 |
+
if 'analysis_history' not in st.session_state:
|
| 102 |
+
st.session_state.analysis_history = []
|
| 103 |
+
|
| 104 |
+
st.session_state.analysis_history.append({
|
| 105 |
+
'question': question,
|
| 106 |
+
'answer': answer,
|
| 107 |
+
'language': language
|
| 108 |
+
})
|
| 109 |
+
|
| 110 |
def main():
|
| 111 |
st.title("πΌοΈ Image Q&A Assistant")
|
| 112 |
st.markdown("Upload an image and ask questions about it in multiple languages!")
|
| 113 |
|
| 114 |
+
# Configure Gemini API from secrets
|
| 115 |
+
api_configured = configure_gemini()
|
| 116 |
+
|
| 117 |
+
# Sidebar for settings
|
| 118 |
with st.sidebar:
|
| 119 |
st.header("βοΈ Settings")
|
| 120 |
|
| 121 |
+
# API Status
|
| 122 |
+
if api_configured:
|
| 123 |
+
st.success("β
Gemini API configured successfully!")
|
| 124 |
+
else:
|
| 125 |
+
st.error("β Please configure GEMINI_API_KEY in secrets")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
st.markdown("---")
|
| 128 |
|
|
|
|
| 179 |
with col2:
|
| 180 |
st.subheader("π¬ Ask Questions")
|
| 181 |
|
| 182 |
+
if 'current_image' in st.session_state and api_configured:
|
| 183 |
# Question input
|
| 184 |
question = st.text_area(
|
| 185 |
"Your question about the image:",
|
|
|
|
| 209 |
LANGUAGES[selected_language]
|
| 210 |
)
|
| 211 |
|
| 212 |
+
# Save to history
|
| 213 |
+
save_to_history(question, result, selected_language)
|
| 214 |
+
|
| 215 |
st.subheader("π― Analysis Result")
|
| 216 |
st.write(result)
|
| 217 |
|
|
|
|
| 222 |
LANGUAGES[selected_language]
|
| 223 |
)
|
| 224 |
|
| 225 |
+
# Save to history
|
| 226 |
+
save_to_history("General Description", description, selected_language)
|
| 227 |
+
|
| 228 |
st.subheader("π Image Description")
|
| 229 |
st.write(description)
|
| 230 |
|
|
|
|
| 234 |
elif 'current_image' not in st.session_state:
|
| 235 |
st.info("π· Please upload an image first.")
|
| 236 |
|
| 237 |
+
elif not api_configured:
|
| 238 |
+
st.warning("π Please configure GEMINI_API_KEY in your Streamlit secrets.")
|
| 239 |
|
| 240 |
# Results history (optional feature)
|
| 241 |
if st.checkbox("π Show Analysis History"):
|