Spaces:
Sleeping
Sleeping
Vatsal Gupta git Copilot commited on
Fix: Making a single client (#31)
Browse files* Fix: Making a single client
* Updated
* Fixed the code
* Update src/app.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update src/app.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
---------
Co-authored-by: git <github@example.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- src/app.py +33 -12
src/app.py
CHANGED
|
@@ -7,6 +7,22 @@ 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:
|
|
@@ -29,11 +45,10 @@ def validate_inputs(_api_key: str, _model_name: str) -> bool:
|
|
| 29 |
return True
|
| 30 |
|
| 31 |
|
| 32 |
-
def generate_response(
|
| 33 |
""" Generate a response from the model based on the user prompt. """
|
| 34 |
try:
|
| 35 |
-
|
| 36 |
-
resp = client.models.generate_content(
|
| 37 |
model=_model_name,
|
| 38 |
contents=_prompt
|
| 39 |
)
|
|
@@ -45,6 +60,14 @@ def generate_response(_api_key: str, _model_name: str, _prompt: str) -> str | No
|
|
| 45 |
return None
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
with st.sidebar:
|
| 49 |
api_key = st.text_input(
|
| 50 |
label='Google API Key',
|
|
@@ -55,13 +78,7 @@ with st.sidebar:
|
|
| 55 |
)
|
| 56 |
model_name = st.selectbox(
|
| 57 |
label='Select Model',
|
| 58 |
-
options=
|
| 59 |
-
'gemini-2.5-pro',
|
| 60 |
-
'gemini-2.5-flash',
|
| 61 |
-
'gemini-2.5-flash-lite',
|
| 62 |
-
'gemini-2.0-flash',
|
| 63 |
-
'gemini-2.0-flash-lite',
|
| 64 |
-
],
|
| 65 |
index=None,
|
| 66 |
help='Choose a Gemini model'
|
| 67 |
)
|
|
@@ -83,11 +100,15 @@ for msg in st.session_state.messages:
|
|
| 83 |
INPUT_DISABLED = not validate_inputs(api_key, model_name)
|
| 84 |
prompt = st.chat_input(disabled=INPUT_DISABLED)
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
| 88 |
st.chat_message('user').write(prompt)
|
| 89 |
|
| 90 |
-
response = generate_response(
|
| 91 |
if response:
|
| 92 |
st.session_state.messages.append(
|
| 93 |
{'role': 'assistant', 'content': response})
|
|
|
|
| 7 |
import streamlit as st
|
| 8 |
|
| 9 |
|
| 10 |
+
class Config:
|
| 11 |
+
""" Configuration settings for the chatbot application. """
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def __init__(self, _api_key: str) -> None:
|
| 15 |
+
self.__api_key = _api_key
|
| 16 |
+
self.__client: genai.Client | None = None
|
| 17 |
+
|
| 18 |
+
def get_client(self) -> genai.Client:
|
| 19 |
+
""" Create and return a GenAI client instance. """
|
| 20 |
+
if self.__client is None:
|
| 21 |
+
self.__client = genai.Client(api_key=self.__api_key)
|
| 22 |
+
|
| 23 |
+
return self.__client
|
| 24 |
+
|
| 25 |
+
|
| 26 |
def initialize_session() -> None:
|
| 27 |
""" Initialize session state variables. """
|
| 28 |
if 'messages' not in st.session_state:
|
|
|
|
| 45 |
return True
|
| 46 |
|
| 47 |
|
| 48 |
+
def generate_response(_client: genai.Client, _model_name: str, _prompt: str) -> str | None:
|
| 49 |
""" Generate a response from the model based on the user prompt. """
|
| 50 |
try:
|
| 51 |
+
resp = _client.models.generate_content(
|
|
|
|
| 52 |
model=_model_name,
|
| 53 |
contents=_prompt
|
| 54 |
)
|
|
|
|
| 60 |
return None
|
| 61 |
|
| 62 |
|
| 63 |
+
AVAILABLE_MODELS = [
|
| 64 |
+
'gemini-2.5-pro',
|
| 65 |
+
'gemini-2.5-flash',
|
| 66 |
+
'gemini-2.5-flash-lite',
|
| 67 |
+
'gemini-2.0-flash',
|
| 68 |
+
'gemini-2.0-flash-lite',
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
with st.sidebar:
|
| 72 |
api_key = st.text_input(
|
| 73 |
label='Google API Key',
|
|
|
|
| 78 |
)
|
| 79 |
model_name = st.selectbox(
|
| 80 |
label='Select Model',
|
| 81 |
+
options=AVAILABLE_MODELS,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
index=None,
|
| 83 |
help='Choose a Gemini model'
|
| 84 |
)
|
|
|
|
| 100 |
INPUT_DISABLED = not validate_inputs(api_key, model_name)
|
| 101 |
prompt = st.chat_input(disabled=INPUT_DISABLED)
|
| 102 |
|
| 103 |
+
GENAI_CLIENT = None
|
| 104 |
+
|
| 105 |
+
if not INPUT_DISABLED:
|
| 106 |
+
GENAI_CLIENT = Config(api_key).get_client()
|
| 107 |
+
if prompt and GENAI_CLIENT is not None:
|
| 108 |
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
| 109 |
st.chat_message('user').write(prompt)
|
| 110 |
|
| 111 |
+
response = generate_response(GENAI_CLIENT, model_name, prompt)
|
| 112 |
if response:
|
| 113 |
st.session_state.messages.append(
|
| 114 |
{'role': 'assistant', 'content': response})
|