Update app.py
Browse files
app.py
CHANGED
@@ -188,19 +188,16 @@ history = [
|
|
188 |
"output: 1. directing his knowingly armed supporters to the capitol",
|
189 |
]
|
190 |
|
191 |
-
# Create the Streamlit interface
|
192 |
st.title("Pattern Completion")
|
193 |
|
194 |
with st.sidebar:
|
195 |
st.header("Model Configuration")
|
196 |
|
197 |
-
# Model selection dropdown
|
198 |
model_name = st.selectbox(
|
199 |
"Select Model",
|
200 |
["gemini-2.0-pro-exp-02-05", "gemini-exp-1206", "gemini-2.0-flash-lite-preview-02-05", "gemini-1.5-pro", "gemini-2.0-flash-exp", "gemini-2.0-flash-thinking-exp-1219"]
|
201 |
)
|
202 |
|
203 |
-
# Temperature slider
|
204 |
temperature = st.slider(
|
205 |
"Temperature",
|
206 |
min_value=0.0,
|
@@ -209,7 +206,6 @@ with st.sidebar:
|
|
209 |
step=0.1
|
210 |
)
|
211 |
|
212 |
-
# Top_p slider
|
213 |
top_p = st.slider(
|
214 |
"Top P",
|
215 |
min_value=0.0,
|
@@ -226,13 +222,11 @@ with st.sidebar:
|
|
226 |
step=1
|
227 |
)
|
228 |
|
229 |
-
# API key input
|
230 |
api_key = "AIzaSyB93eoXaU48hrJ2hojk-2gQPj28dGVSIbQ"
|
|
|
231 |
|
232 |
-
# Configure Gemini
|
233 |
genai.configure(api_key=api_key)
|
234 |
|
235 |
-
# Create the model
|
236 |
generation_config = {
|
237 |
"temperature": temperature,
|
238 |
"top_p": top_p,
|
@@ -246,25 +240,31 @@ with st.sidebar:
|
|
246 |
generation_config=generation_config,
|
247 |
)
|
248 |
|
249 |
-
# Input text box (now using text_area instead of text_input)
|
250 |
user_input = st.text_area("Enter your input:", height=150)
|
251 |
if st.button("Generate"):
|
252 |
if user_input:
|
253 |
-
# Create prompt with history and new input
|
254 |
prompt = history.copy()
|
255 |
prompt.extend([f"input: {user_input}", "output: "])
|
256 |
print(prompt)
|
257 |
-
|
258 |
try:
|
259 |
response = model.generate_content(prompt)
|
260 |
output = response.text.strip()
|
261 |
-
# Display result
|
262 |
st.success(f"Output: {output}")
|
263 |
except Exception as e:
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
"output: 1. directing his knowingly armed supporters to the capitol",
|
189 |
]
|
190 |
|
|
|
191 |
st.title("Pattern Completion")
|
192 |
|
193 |
with st.sidebar:
|
194 |
st.header("Model Configuration")
|
195 |
|
|
|
196 |
model_name = st.selectbox(
|
197 |
"Select Model",
|
198 |
["gemini-2.0-pro-exp-02-05", "gemini-exp-1206", "gemini-2.0-flash-lite-preview-02-05", "gemini-1.5-pro", "gemini-2.0-flash-exp", "gemini-2.0-flash-thinking-exp-1219"]
|
199 |
)
|
200 |
|
|
|
201 |
temperature = st.slider(
|
202 |
"Temperature",
|
203 |
min_value=0.0,
|
|
|
206 |
step=0.1
|
207 |
)
|
208 |
|
|
|
209 |
top_p = st.slider(
|
210 |
"Top P",
|
211 |
min_value=0.0,
|
|
|
222 |
step=1
|
223 |
)
|
224 |
|
|
|
225 |
api_key = "AIzaSyB93eoXaU48hrJ2hojk-2gQPj28dGVSIbQ"
|
226 |
+
backup_api_key = "AIzaSyDNWizNQAA0UaflR5f_zTF6FcMSIA5D8RU"
|
227 |
|
|
|
228 |
genai.configure(api_key=api_key)
|
229 |
|
|
|
230 |
generation_config = {
|
231 |
"temperature": temperature,
|
232 |
"top_p": top_p,
|
|
|
240 |
generation_config=generation_config,
|
241 |
)
|
242 |
|
|
|
243 |
user_input = st.text_area("Enter your input:", height=150)
|
244 |
if st.button("Generate"):
|
245 |
if user_input:
|
|
|
246 |
prompt = history.copy()
|
247 |
prompt.extend([f"input: {user_input}", "output: "])
|
248 |
print(prompt)
|
249 |
+
|
250 |
try:
|
251 |
response = model.generate_content(prompt)
|
252 |
output = response.text.strip()
|
|
|
253 |
st.success(f"Output: {output}")
|
254 |
except Exception as e:
|
255 |
+
if "429" in str(e):
|
256 |
+
try:
|
257 |
+
# Switch to backup API key
|
258 |
+
genai.configure(api_key=backup_api_key)
|
259 |
+
model = genai.GenerativeModel(
|
260 |
+
model_name=model_name,
|
261 |
+
generation_config=generation_config,
|
262 |
+
)
|
263 |
+
# Retry with new API key
|
264 |
+
response = model.generate_content(prompt)
|
265 |
+
output = response.text.strip()
|
266 |
+
st.success(f"Output: {output}")
|
267 |
+
except Exception as backup_error:
|
268 |
+
st.error(f"Error with backup API key: {str(backup_error)}")
|
269 |
+
else:
|
270 |
+
st.error(f"An error occurred: {str(e)}")
|