Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ import datetime
|
|
| 7 |
import re
|
| 8 |
from typing import List, Dict, Any, Optional
|
| 9 |
|
| 10 |
-
# --- App Description ---
|
| 11 |
DESCRIPTION = """
|
| 12 |
# GemiWine
|
| 13 |
**Powered by Gemini 2.5 Flash + Google Search Grounding**
|
|
@@ -114,12 +113,10 @@ Your expertise covers the full lifecycle of wine production and the surrounding
|
|
| 114 |
"""
|
| 115 |
|
| 116 |
|
| 117 |
-
# --- Logger ---
|
| 118 |
def log(msg: str):
|
| 119 |
now = datetime.datetime.now().strftime("%H:%M:%S")
|
| 120 |
print(f"[{now}] {msg}", flush=True)
|
| 121 |
|
| 122 |
-
# --- Persistent chat sessions for MTC ---
|
| 123 |
CHAT_SESSIONS: Dict[str, Dict[str, Any]] = {}
|
| 124 |
|
| 125 |
def get_or_create_chat(api_key: str):
|
|
@@ -137,7 +134,6 @@ def get_or_create_chat(api_key: str):
|
|
| 137 |
log(f"❌ Error creating Gemini client/chat: {e}")
|
| 138 |
return None, None
|
| 139 |
|
| 140 |
-
# --- Helper Functions ---
|
| 141 |
def try_open_image(path: str) -> Optional[Image.Image]:
|
| 142 |
try:
|
| 143 |
return Image.open(path)
|
|
@@ -174,7 +170,6 @@ def insert_citations_from_grounding(candidates):
|
|
| 174 |
log(f"⚠️ Citation insert failed: {e}")
|
| 175 |
return None
|
| 176 |
|
| 177 |
-
# --- Reasoning Config ---
|
| 178 |
def reasoning_budget(level: str) -> int:
|
| 179 |
level = (level or "Dynamic").lower()
|
| 180 |
if level == "none":
|
|
@@ -187,7 +182,6 @@ def reasoning_budget(level: str) -> int:
|
|
| 187 |
return -1
|
| 188 |
return -1
|
| 189 |
|
| 190 |
-
# --- Main Chat Function (Streaming + MTC + Thinking) ---
|
| 191 |
def chat_with_gemini(api_key, chat_history_msgs, multimodal_input, show_thoughts, reasoning_level):
|
| 192 |
log("=== chat_with_gemini CALLED ===")
|
| 193 |
|
|
@@ -262,7 +256,7 @@ def chat_with_gemini(api_key, chat_history_msgs, multimodal_input, show_thoughts
|
|
| 262 |
thoughts += part.text
|
| 263 |
if show_thoughts:
|
| 264 |
chat_history_msgs[thought_index]["content"] = (
|
| 265 |
-
f"<details open>"
|
| 266 |
f"<summary><strong>💭 GemiWine’s Thinking</strong></summary>"
|
| 267 |
f"<div style='white-space:pre-wrap;background:inherit;color:inherit;"
|
| 268 |
f"padding:8px;border-radius:8px;border:1px solid var(--border-color);'>"
|
|
@@ -293,7 +287,6 @@ def chat_with_gemini(api_key, chat_history_msgs, multimodal_input, show_thoughts
|
|
| 293 |
yield chat_history_msgs
|
| 294 |
return
|
| 295 |
|
| 296 |
-
# --- UI Layout (Corrected Version) ---
|
| 297 |
with gr.Blocks(
|
| 298 |
theme=gr.themes.Soft(primary_hue="purple", secondary_hue="blue"),
|
| 299 |
title="GemiWine",
|
|
@@ -317,7 +310,6 @@ with gr.Blocks(
|
|
| 317 |
""")
|
| 318 |
|
| 319 |
with gr.Row():
|
| 320 |
-
# --- Sidebar ---
|
| 321 |
with gr.Column(scale=1, min_width=320):
|
| 322 |
gr.Markdown("## ⚙️ Settings & Controls")
|
| 323 |
api_key = gr.Textbox(
|
|
@@ -337,15 +329,13 @@ with gr.Blocks(
|
|
| 337 |
info="Display reasoning process before answers.",
|
| 338 |
)
|
| 339 |
|
| 340 |
-
# --- Main Content ---
|
| 341 |
with gr.Column(scale=4):
|
| 342 |
-
# gr.Markdown(DESCRIPTION) # Uncomment if you have a DESCRIPTION variable
|
| 343 |
with gr.Group(elem_classes="chat-panel"):
|
| 344 |
chatbot = gr.Chatbot(
|
| 345 |
label="🍇 Chat with GemiWine",
|
| 346 |
height=650,
|
| 347 |
show_copy_button=True,
|
| 348 |
-
type="messages",
|
| 349 |
avatar_images=(None, "https://i.imgur.com/Q2EMk2N.png"),
|
| 350 |
)
|
| 351 |
multimodal_msg = gr.MultimodalTextbox(
|
|
|
|
| 7 |
import re
|
| 8 |
from typing import List, Dict, Any, Optional
|
| 9 |
|
|
|
|
| 10 |
DESCRIPTION = """
|
| 11 |
# GemiWine
|
| 12 |
**Powered by Gemini 2.5 Flash + Google Search Grounding**
|
|
|
|
| 113 |
"""
|
| 114 |
|
| 115 |
|
|
|
|
| 116 |
def log(msg: str):
|
| 117 |
now = datetime.datetime.now().strftime("%H:%M:%S")
|
| 118 |
print(f"[{now}] {msg}", flush=True)
|
| 119 |
|
|
|
|
| 120 |
CHAT_SESSIONS: Dict[str, Dict[str, Any]] = {}
|
| 121 |
|
| 122 |
def get_or_create_chat(api_key: str):
|
|
|
|
| 134 |
log(f"❌ Error creating Gemini client/chat: {e}")
|
| 135 |
return None, None
|
| 136 |
|
|
|
|
| 137 |
def try_open_image(path: str) -> Optional[Image.Image]:
|
| 138 |
try:
|
| 139 |
return Image.open(path)
|
|
|
|
| 170 |
log(f"⚠️ Citation insert failed: {e}")
|
| 171 |
return None
|
| 172 |
|
|
|
|
| 173 |
def reasoning_budget(level: str) -> int:
|
| 174 |
level = (level or "Dynamic").lower()
|
| 175 |
if level == "none":
|
|
|
|
| 182 |
return -1
|
| 183 |
return -1
|
| 184 |
|
|
|
|
| 185 |
def chat_with_gemini(api_key, chat_history_msgs, multimodal_input, show_thoughts, reasoning_level):
|
| 186 |
log("=== chat_with_gemini CALLED ===")
|
| 187 |
|
|
|
|
| 256 |
thoughts += part.text
|
| 257 |
if show_thoughts:
|
| 258 |
chat_history_msgs[thought_index]["content"] = (
|
| 259 |
+
f"<details open>"
|
| 260 |
f"<summary><strong>💭 GemiWine’s Thinking</strong></summary>"
|
| 261 |
f"<div style='white-space:pre-wrap;background:inherit;color:inherit;"
|
| 262 |
f"padding:8px;border-radius:8px;border:1px solid var(--border-color);'>"
|
|
|
|
| 287 |
yield chat_history_msgs
|
| 288 |
return
|
| 289 |
|
|
|
|
| 290 |
with gr.Blocks(
|
| 291 |
theme=gr.themes.Soft(primary_hue="purple", secondary_hue="blue"),
|
| 292 |
title="GemiWine",
|
|
|
|
| 310 |
""")
|
| 311 |
|
| 312 |
with gr.Row():
|
|
|
|
| 313 |
with gr.Column(scale=1, min_width=320):
|
| 314 |
gr.Markdown("## ⚙️ Settings & Controls")
|
| 315 |
api_key = gr.Textbox(
|
|
|
|
| 329 |
info="Display reasoning process before answers.",
|
| 330 |
)
|
| 331 |
|
|
|
|
| 332 |
with gr.Column(scale=4):
|
|
|
|
| 333 |
with gr.Group(elem_classes="chat-panel"):
|
| 334 |
chatbot = gr.Chatbot(
|
| 335 |
label="🍇 Chat with GemiWine",
|
| 336 |
height=650,
|
| 337 |
show_copy_button=True,
|
| 338 |
+
type="messages",
|
| 339 |
avatar_images=(None, "https://i.imgur.com/Q2EMk2N.png"),
|
| 340 |
)
|
| 341 |
multimodal_msg = gr.MultimodalTextbox(
|