Spaces:
Running
Running
Vatsal Gupta git commited on
Migrated from 'google-generativeai' to 'google-genai' (#29)
Browse files* Migrated from 'google-generativeai' to 'google-genai'
* Updated Dockerfile
* Fixing Errors
* Fixing Updating pip-tools
* Updated Dockerfile
* Updated Dockerfile
* Updated Dockerfile
* Revamped Dockerfile
* Updating Dockerfile
* Add non-root user Dockerfile
* Updated requirements
* Updated Dockerfile
* fix
* More Fix
* Fixing
* Updated requirements
* Update
* Fixed Dockerfile
---------
Co-authored-by: git <github@example.com>
- Dockerfile +22 -9
- Makefile +5 -2
- pyproject.toml +2 -2
- requirements.txt +53 -2
- src/app.py +22 -32
Dockerfile
CHANGED
|
@@ -15,30 +15,36 @@
|
|
| 15 |
# ##########################################################################
|
| 16 |
# Base Image
|
| 17 |
# ##########################################################################
|
| 18 |
-
FROM
|
| 19 |
-
|
| 20 |
-
RUN addgroup -S nonroot \
|
| 21 |
-
&& adduser -S nonroot -G nonroot
|
| 22 |
|
| 23 |
# ##########################################################################
|
| 24 |
# Maintainer
|
| 25 |
# ##########################################################################
|
| 26 |
LABEL maintainer="Vatsal Gupta (gvatsal60)"
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# ##########################################################################
|
| 29 |
# Set Working Directory
|
| 30 |
# ##########################################################################
|
| 31 |
WORKDIR /app
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
# ##########################################################################
|
| 34 |
# Copy Files
|
| 35 |
# ##########################################################################
|
| 36 |
-
|
| 37 |
-
COPY
|
| 38 |
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
# ##########################################################################
|
| 44 |
# Expose Port
|
|
@@ -48,4 +54,11 @@ EXPOSE 8501
|
|
| 48 |
# ##########################################################################
|
| 49 |
# Command to Run
|
| 50 |
# ##########################################################################
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# ##########################################################################
|
| 16 |
# Base Image
|
| 17 |
# ##########################################################################
|
| 18 |
+
FROM ghcr.io/astral-sh/uv:python3.12-trixie-slim
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# ##########################################################################
|
| 21 |
# Maintainer
|
| 22 |
# ##########################################################################
|
| 23 |
LABEL maintainer="Vatsal Gupta (gvatsal60)"
|
| 24 |
|
| 25 |
+
# Add non-root user
|
| 26 |
+
RUN addgroup --system nonroot \
|
| 27 |
+
&& adduser --system --ingroup nonroot nonroot
|
| 28 |
+
|
| 29 |
# ##########################################################################
|
| 30 |
# Set Working Directory
|
| 31 |
# ##########################################################################
|
| 32 |
WORKDIR /app
|
| 33 |
|
| 34 |
+
# Set cache directory to /tmp to avoid permission issues
|
| 35 |
+
ENV XDG_CACHE_HOME=/tmp/.cache
|
| 36 |
+
|
| 37 |
# ##########################################################################
|
| 38 |
# Copy Files
|
| 39 |
# ##########################################################################
|
| 40 |
+
# Copy dependency files first for better caching
|
| 41 |
+
COPY pyproject.toml ./
|
| 42 |
|
| 43 |
+
# Install dependencies into a local folder
|
| 44 |
+
RUN uv sync --no-cache
|
| 45 |
|
| 46 |
+
# Copy source code
|
| 47 |
+
COPY src/ ./
|
| 48 |
|
| 49 |
# ##########################################################################
|
| 50 |
# Expose Port
|
|
|
|
| 54 |
# ##########################################################################
|
| 55 |
# Command to Run
|
| 56 |
# ##########################################################################
|
| 57 |
+
# Switch to non-root user
|
| 58 |
+
USER nonroot
|
| 59 |
+
|
| 60 |
+
ENTRYPOINT [ "uv", "run", \
|
| 61 |
+
"streamlit", "run", "app.py", \
|
| 62 |
+
"--server.port=8501", \
|
| 63 |
+
"--server.address=0.0.0.0", \
|
| 64 |
+
"--browser.gatherUsageStats=false"]
|
Makefile
CHANGED
|
@@ -21,14 +21,17 @@ SRC_DIR := src
|
|
| 21 |
# **************************************************************************************/
|
| 22 |
.PHONY: all run test clean
|
| 23 |
|
| 24 |
-
all: sync run
|
| 25 |
|
| 26 |
sync:
|
| 27 |
-
@uv sync
|
|
|
|
|
|
|
| 28 |
run: sync
|
| 29 |
@uv run --directory $(SRC_DIR) streamlit run app.py --browser.gatherUsageStats false
|
| 30 |
test: sync
|
| 31 |
@echo "No tests available currently."
|
| 32 |
# @uv test
|
| 33 |
clean:
|
|
|
|
| 34 |
@uv clean
|
|
|
|
| 21 |
# **************************************************************************************/
|
| 22 |
.PHONY: all run test clean
|
| 23 |
|
| 24 |
+
all: clean sync run
|
| 25 |
|
| 26 |
sync:
|
| 27 |
+
@uv sync --no-cache
|
| 28 |
+
freeze: sync
|
| 29 |
+
@uv export --quiet --no-header --no-annotate --no-hashes --format requirements.txt --output-file requirements.txt
|
| 30 |
run: sync
|
| 31 |
@uv run --directory $(SRC_DIR) streamlit run app.py --browser.gatherUsageStats false
|
| 32 |
test: sync
|
| 33 |
@echo "No tests available currently."
|
| 34 |
# @uv test
|
| 35 |
clean:
|
| 36 |
+
@rm -rf __pycache__ .pytest_cache .mypy_cache .venv
|
| 37 |
@uv clean
|
pyproject.toml
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
[project]
|
| 2 |
name = "gemini-chatbot"
|
| 3 |
-
version = "0.
|
| 4 |
description = "A chatbot application using Google's Gemini API and Streamlit."
|
| 5 |
readme = "README.md"
|
| 6 |
requires-python = ">=3.12"
|
| 7 |
dependencies = [
|
| 8 |
-
"google-
|
| 9 |
"streamlit>=1.49.1",
|
| 10 |
]
|
|
|
|
| 1 |
[project]
|
| 2 |
name = "gemini-chatbot"
|
| 3 |
+
version = "0.2.0"
|
| 4 |
description = "A chatbot application using Google's Gemini API and Streamlit."
|
| 5 |
readme = "README.md"
|
| 6 |
requires-python = ">=3.12"
|
| 7 |
dependencies = [
|
| 8 |
+
"google-genai>=1.47.0",
|
| 9 |
"streamlit>=1.49.1",
|
| 10 |
]
|
requirements.txt
CHANGED
|
@@ -1,2 +1,53 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
altair==5.5.0
|
| 2 |
+
annotated-types==0.7.0
|
| 3 |
+
anyio==4.11.0
|
| 4 |
+
attrs==25.4.0
|
| 5 |
+
blinker==1.9.0
|
| 6 |
+
cachetools==6.2.1
|
| 7 |
+
certifi==2025.10.5
|
| 8 |
+
charset-normalizer==3.4.4
|
| 9 |
+
click==8.3.0
|
| 10 |
+
colorama==0.4.6 ; sys_platform == 'win32'
|
| 11 |
+
gitdb==4.0.12
|
| 12 |
+
gitpython==3.1.45
|
| 13 |
+
google-auth==2.42.1
|
| 14 |
+
google-genai==1.47.0
|
| 15 |
+
h11==0.16.0
|
| 16 |
+
httpcore==1.0.9
|
| 17 |
+
httpx==0.28.1
|
| 18 |
+
idna==3.11
|
| 19 |
+
jinja2==3.1.6
|
| 20 |
+
jsonschema==4.25.1
|
| 21 |
+
jsonschema-specifications==2025.9.1
|
| 22 |
+
markupsafe==3.0.3
|
| 23 |
+
narwhals==2.10.1
|
| 24 |
+
numpy==2.3.4
|
| 25 |
+
packaging==25.0
|
| 26 |
+
pandas==2.3.3
|
| 27 |
+
pillow==12.0.0
|
| 28 |
+
protobuf==6.33.0
|
| 29 |
+
pyarrow==21.0.0
|
| 30 |
+
pyasn1==0.6.1
|
| 31 |
+
pyasn1-modules==0.4.2
|
| 32 |
+
pydantic==2.12.3
|
| 33 |
+
pydantic-core==2.41.4
|
| 34 |
+
pydeck==0.9.1
|
| 35 |
+
python-dateutil==2.9.0.post0
|
| 36 |
+
pytz==2025.2
|
| 37 |
+
referencing==0.37.0
|
| 38 |
+
requests==2.32.5
|
| 39 |
+
rpds-py==0.28.0
|
| 40 |
+
rsa==4.9.1
|
| 41 |
+
six==1.17.0
|
| 42 |
+
smmap==5.0.2
|
| 43 |
+
sniffio==1.3.1
|
| 44 |
+
streamlit==1.51.0
|
| 45 |
+
tenacity==9.1.2
|
| 46 |
+
toml==0.10.2
|
| 47 |
+
tornado==6.5.2
|
| 48 |
+
typing-extensions==4.15.0
|
| 49 |
+
typing-inspection==0.4.2
|
| 50 |
+
tzdata==2025.2
|
| 51 |
+
urllib3==2.5.0
|
| 52 |
+
watchdog==6.0.0 ; sys_platform != 'darwin'
|
| 53 |
+
websockets==15.0.1
|
src/app.py
CHANGED
|
@@ -2,12 +2,12 @@
|
|
| 2 |
A robust chatbot application using Google Generative AI models with Streamlit.
|
| 3 |
'''
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
import streamlit as st
|
| 8 |
|
| 9 |
|
| 10 |
-
def initialize_session():
|
| 11 |
""" Initialize session state variables. """
|
| 12 |
if 'messages' not in st.session_state:
|
| 13 |
st.session_state['messages'] = [{
|
|
@@ -29,27 +29,19 @@ def validate_inputs(_api_key: str, _model_name: str) -> bool:
|
|
| 29 |
return True
|
| 30 |
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
""" Initialize and return the GenerativeModel. """
|
| 34 |
-
try:
|
| 35 |
-
genai.configure(api_key=_api_key)
|
| 36 |
-
return genai.GenerativeModel(model_name=_model_name)
|
| 37 |
-
except (gexceptions.GoogleAPICallError) as e:
|
| 38 |
-
st.error(
|
| 39 |
-
f"Failed to initialize the model. Please check your API key and model selection. \
|
| 40 |
-
Details: {e.message}"
|
| 41 |
-
)
|
| 42 |
-
return None
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def generate_response(_model: genai.GenerativeModel, _prompt: str) -> str | None:
|
| 46 |
""" Generate a response from the model based on the user prompt. """
|
| 47 |
try:
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return resp.text
|
| 50 |
-
except (
|
| 51 |
st.error(
|
| 52 |
-
f"Failed to get a response from the model. Details: {e.message}"
|
|
|
|
| 53 |
return None
|
| 54 |
|
| 55 |
|
|
@@ -95,15 +87,13 @@ if prompt and not INPUT_DISABLED:
|
|
| 95 |
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
| 96 |
st.chat_message('user').write(prompt)
|
| 97 |
|
| 98 |
-
|
| 99 |
-
if
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
st.chat_message('assistant').write(
|
| 109 |
-
"Sorry, I couldn't process your request.")
|
|
|
|
| 2 |
A robust chatbot application using Google Generative AI models with Streamlit.
|
| 3 |
'''
|
| 4 |
|
| 5 |
+
from google import genai
|
| 6 |
+
from google.genai.errors import APIError
|
| 7 |
import streamlit as st
|
| 8 |
|
| 9 |
|
| 10 |
+
def initialize_session() -> None:
|
| 11 |
""" Initialize session state variables. """
|
| 12 |
if 'messages' not in st.session_state:
|
| 13 |
st.session_state['messages'] = [{
|
|
|
|
| 29 |
return True
|
| 30 |
|
| 31 |
|
| 32 |
+
def generate_response(_api_key: str, _model_name: str, _prompt: str) -> str | None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
""" Generate a response from the model based on the user prompt. """
|
| 34 |
try:
|
| 35 |
+
client = genai.Client(api_key=_api_key)
|
| 36 |
+
resp = client.models.generate_content(
|
| 37 |
+
model=_model_name,
|
| 38 |
+
contents=_prompt
|
| 39 |
+
)
|
| 40 |
return resp.text
|
| 41 |
+
except (APIError) as e:
|
| 42 |
st.error(
|
| 43 |
+
f"Failed to get a response from the model. Details: {e.message}"
|
| 44 |
+
)
|
| 45 |
return None
|
| 46 |
|
| 47 |
|
|
|
|
| 87 |
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
| 88 |
st.chat_message('user').write(prompt)
|
| 89 |
|
| 90 |
+
response = generate_response(api_key, model_name, prompt)
|
| 91 |
+
if response:
|
| 92 |
+
st.session_state.messages.append(
|
| 93 |
+
{'role': 'assistant', 'content': response})
|
| 94 |
+
st.chat_message('assistant').write(response)
|
| 95 |
+
else:
|
| 96 |
+
st.session_state.messages.append(
|
| 97 |
+
{'role': 'assistant', 'content': "Sorry, I couldn't process your request."})
|
| 98 |
+
st.chat_message('assistant').write(
|
| 99 |
+
"Sorry, I couldn't process your request.")
|
|
|
|
|
|