Spaces:
Sleeping
Sleeping
Initial commit
Browse files
app.py
CHANGED
|
@@ -72,7 +72,7 @@ if "show_thinking" not in st.session_state:
|
|
| 72 |
|
| 73 |
# Заголовок приложения
|
| 74 |
st.title("Hello, miracle!")
|
| 75 |
-
st.caption("✦ Official Gemini API is used | v1.0.2-alpha.3")
|
| 76 |
|
| 77 |
# Инициализация истории сообщений, если её нет в состоянии сессии
|
| 78 |
if "messages" not in st.session_state:
|
|
@@ -95,7 +95,7 @@ if uploaded_file is not None:
|
|
| 95 |
# Обработка ввода пользователя
|
| 96 |
if prompt := st.chat_input("What is up?"):
|
| 97 |
# --- Изменено: Добавлена обработка изображения ---
|
| 98 |
-
if uploaded_image and selected_model_key
|
| 99 |
# Кодируем изображение в base64
|
| 100 |
buffered = io.BytesIO()
|
| 101 |
uploaded_image.save(buffered, format="JPEG")
|
|
@@ -118,7 +118,7 @@ if prompt := st.chat_input("What is up?"):
|
|
| 118 |
message_placeholder = st.empty()
|
| 119 |
full_response = ""
|
| 120 |
thinking_process = ""
|
| 121 |
-
|
| 122 |
with st.spinner("Generating response..."):
|
| 123 |
try:
|
| 124 |
response = st.session_state.chat_session.send_message(
|
|
@@ -133,6 +133,7 @@ if prompt := st.chat_input("What is up?"):
|
|
| 133 |
|
| 134 |
# Обработка размышлений для gemini-2.0-flash-thinking-exp-1219
|
| 135 |
if selected_model_key == "gemini-2.0-flash-thinking-exp-1219":
|
|
|
|
| 136 |
if hasattr(response, 'candidates'):
|
| 137 |
for candidate in response.candidates:
|
| 138 |
if candidate.content and candidate.content.parts:
|
|
@@ -145,22 +146,18 @@ if prompt := st.chat_input("What is up?"):
|
|
| 145 |
sleep(0.5)
|
| 146 |
|
| 147 |
# Получение финального ответа для gemini-2.0-flash-thinking-exp-1219
|
| 148 |
-
response = st.session_state.chat_session.send_message("**
|
| 149 |
-
# Поддержка LaTeX в ответах
|
| 150 |
full_response = response.text
|
| 151 |
message_placeholder.markdown(full_response, unsafe_allow_html=True)
|
|
|
|
| 152 |
else:
|
| 153 |
# Для остальных моделей - стандартный стриминг
|
| 154 |
for chunk in response:
|
| 155 |
for char in chunk.text:
|
| 156 |
full_response += char
|
| 157 |
-
# Поддержка LaTeX в ответах с анимацией
|
| 158 |
message_placeholder.markdown(f'<span class="token">{html.escape(full_response)}</span>' + " ⬤", unsafe_allow_html=True)
|
| 159 |
sleep(0.01)
|
| 160 |
|
| 161 |
-
# Сбрасываем состояние кнопки "Show Thinking Process"
|
| 162 |
-
st.session_state.show_thinking = False
|
| 163 |
-
|
| 164 |
# Добавляем кнопку для показа размышлений, если модель "gemini-2.0-flash-thinking-exp-1219"
|
| 165 |
if selected_model_key == "gemini-2.0-flash-thinking-exp-1219":
|
| 166 |
if st.button("🤔 Show Thinking Process", key="show_thinking_button"):
|
|
@@ -168,7 +165,6 @@ if prompt := st.chat_input("What is up?"):
|
|
| 168 |
|
| 169 |
# Отображаем размышления, если кнопка нажата
|
| 170 |
if selected_model_key == "gemini-2.0-flash-thinking-exp-1219" and st.session_state.show_thinking:
|
| 171 |
-
# Поддержка LaTeX в размышлениях
|
| 172 |
st.markdown("**Thinking Process:**", unsafe_allow_html=True)
|
| 173 |
st.markdown(thinking_process, unsafe_allow_html=True)
|
| 174 |
|
|
@@ -176,4 +172,6 @@ if prompt := st.chat_input("What is up?"):
|
|
| 176 |
st.error(f"An error occurred: {e}")
|
| 177 |
full_response = "Error occurred while generating response."
|
| 178 |
|
|
|
|
|
|
|
| 179 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
|
|
|
| 72 |
|
| 73 |
# Заголовок приложения
|
| 74 |
st.title("Hello, miracle!")
|
| 75 |
+
st.caption("✦ Official Gemini API is used | v1.0.2-alpha.3"0)
|
| 76 |
|
| 77 |
# Инициализация истории сообщений, если её нет в состоянии сессии
|
| 78 |
if "messages" not in st.session_state:
|
|
|
|
| 95 |
# Обработка ввода пользователя
|
| 96 |
if prompt := st.chat_input("What is up?"):
|
| 97 |
# --- Изменено: Добавлена обработка изображения ---
|
| 98 |
+
if uploaded_image and selected_model_key == "gemini-exp-1206":
|
| 99 |
# Кодируем изображение в base64
|
| 100 |
buffered = io.BytesIO()
|
| 101 |
uploaded_image.save(buffered, format="JPEG")
|
|
|
|
| 118 |
message_placeholder = st.empty()
|
| 119 |
full_response = ""
|
| 120 |
thinking_process = ""
|
| 121 |
+
|
| 122 |
with st.spinner("Generating response..."):
|
| 123 |
try:
|
| 124 |
response = st.session_state.chat_session.send_message(
|
|
|
|
| 133 |
|
| 134 |
# Обработка размышлений для gemini-2.0-flash-thinking-exp-1219
|
| 135 |
if selected_model_key == "gemini-2.0-flash-thinking-exp-1219":
|
| 136 |
+
# Обновление логики извлечения текста
|
| 137 |
if hasattr(response, 'candidates'):
|
| 138 |
for candidate in response.candidates:
|
| 139 |
if candidate.content and candidate.content.parts:
|
|
|
|
| 146 |
sleep(0.5)
|
| 147 |
|
| 148 |
# Получение финального ответа для gemini-2.0-flash-thinking-exp-1219
|
| 149 |
+
response = st.session_state.chat_session.send_message("**Finally:**", stream=False)
|
|
|
|
| 150 |
full_response = response.text
|
| 151 |
message_placeholder.markdown(full_response, unsafe_allow_html=True)
|
| 152 |
+
|
| 153 |
else:
|
| 154 |
# Для остальных моделей - стандартный стриминг
|
| 155 |
for chunk in response:
|
| 156 |
for char in chunk.text:
|
| 157 |
full_response += char
|
|
|
|
| 158 |
message_placeholder.markdown(f'<span class="token">{html.escape(full_response)}</span>' + " ⬤", unsafe_allow_html=True)
|
| 159 |
sleep(0.01)
|
| 160 |
|
|
|
|
|
|
|
|
|
|
| 161 |
# Добавляем кнопку для показа размышлений, если модель "gemini-2.0-flash-thinking-exp-1219"
|
| 162 |
if selected_model_key == "gemini-2.0-flash-thinking-exp-1219":
|
| 163 |
if st.button("🤔 Show Thinking Process", key="show_thinking_button"):
|
|
|
|
| 165 |
|
| 166 |
# Отображаем размышления, если кнопка нажата
|
| 167 |
if selected_model_key == "gemini-2.0-flash-thinking-exp-1219" and st.session_state.show_thinking:
|
|
|
|
| 168 |
st.markdown("**Thinking Process:**", unsafe_allow_html=True)
|
| 169 |
st.markdown(thinking_process, unsafe_allow_html=True)
|
| 170 |
|
|
|
|
| 172 |
st.error(f"An error occurred: {e}")
|
| 173 |
full_response = "Error occurred while generating response."
|
| 174 |
|
| 175 |
+
# Сбрасываем состояние кнопки "Show Thinking Process"
|
| 176 |
+
st.session_state.show_thinking = False
|
| 177 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|