aiqcamp commited on
Commit
6eb5405
·
verified ·
1 Parent(s): 517e915

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -20
app.py CHANGED
@@ -33,19 +33,19 @@ def stream_gemini_response(user_message: str, messages: list) -> Iterator[list]:
33
  try:
34
  print(f"\n=== New Request ===")
35
  print(f"User message: {user_message}")
36
-
37
  # Format chat history for Gemini
38
  chat_history = format_chat_history(messages)
39
-
40
  # Initialize Gemini chat
41
  chat = model.start_chat(history=chat_history)
42
  response = chat.send_message(user_message, stream=True)
43
-
44
  # Initialize buffers and flags
45
  thought_buffer = ""
46
  response_buffer = ""
47
  thinking_complete = False
48
-
49
  # Add initial thinking message
50
  messages.append(
51
  ChatMessage(
@@ -54,27 +54,27 @@ def stream_gemini_response(user_message: str, messages: list) -> Iterator[list]:
54
  metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
55
  )
56
  )
57
-
58
  for chunk in response:
59
  parts = chunk.candidates[0].content.parts
60
  current_chunk = parts[0].text
61
-
62
  if len(parts) == 2 and not thinking_complete:
63
  # Complete thought and start response
64
  thought_buffer += current_chunk
65
  print(f"\n=== Complete Thought ===\n{thought_buffer}")
66
-
67
  messages[-1] = ChatMessage(
68
  role="assistant",
69
  content=thought_buffer,
70
  metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
71
  )
72
  yield messages
73
-
74
  # Start response
75
  response_buffer = parts[1].text
76
  print(f"\n=== Starting Response ===\n{response_buffer}")
77
-
78
  messages.append(
79
  ChatMessage(
80
  role="assistant",
@@ -82,32 +82,32 @@ def stream_gemini_response(user_message: str, messages: list) -> Iterator[list]:
82
  )
83
  )
84
  thinking_complete = True
85
-
86
  elif thinking_complete:
87
  # Stream response
88
  response_buffer += current_chunk
89
  print(f"\n=== Response Chunk ===\n{current_chunk}")
90
-
91
  messages[-1] = ChatMessage(
92
  role="assistant",
93
  content=response_buffer
94
  )
95
-
96
  else:
97
  # Stream thinking
98
  thought_buffer += current_chunk
99
  print(f"\n=== Thinking Chunk ===\n{current_chunk}")
100
-
101
  messages[-1] = ChatMessage(
102
  role="assistant",
103
  content=thought_buffer,
104
  metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
105
  )
106
-
107
  yield messages
108
-
109
  print(f"\n=== Final Response ===\n{response_buffer}")
110
-
111
  except Exception as e:
112
  print(f"\n=== Error ===\n{str(e)}")
113
  messages.append(
@@ -122,12 +122,34 @@ def user_message(msg: str, history: list) -> tuple[str, list]:
122
  """Adds user message to chat history"""
123
  history.append(ChatMessage(role="user", content=msg))
124
  return "", history
125
-
126
 
127
  # Create the Gradio interface
128
  with gr.Blocks(theme=gr.themes.Citrus(), fill_height=True) as demo:
129
- #with gr.Column():
130
- gr.Markdown("# Chat with Gemini 2.0 Flash and See its Thoughts 💭")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
  chatbot = gr.Chatbot(
133
  type="messages",
@@ -149,7 +171,7 @@ with gr.Blocks(theme=gr.themes.Citrus(), fill_height=True) as demo:
149
 
150
  # Set up event handlers
151
  msg_store = gr.State("") # Store for preserving user message
152
-
153
  input_box.submit(
154
  lambda msg: (msg, msg, ""), # Store message and clear input
155
  inputs=[input_box],
 
33
  try:
34
  print(f"\n=== New Request ===")
35
  print(f"User message: {user_message}")
36
+
37
  # Format chat history for Gemini
38
  chat_history = format_chat_history(messages)
39
+
40
  # Initialize Gemini chat
41
  chat = model.start_chat(history=chat_history)
42
  response = chat.send_message(user_message, stream=True)
43
+
44
  # Initialize buffers and flags
45
  thought_buffer = ""
46
  response_buffer = ""
47
  thinking_complete = False
48
+
49
  # Add initial thinking message
50
  messages.append(
51
  ChatMessage(
 
54
  metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
55
  )
56
  )
57
+
58
  for chunk in response:
59
  parts = chunk.candidates[0].content.parts
60
  current_chunk = parts[0].text
61
+
62
  if len(parts) == 2 and not thinking_complete:
63
  # Complete thought and start response
64
  thought_buffer += current_chunk
65
  print(f"\n=== Complete Thought ===\n{thought_buffer}")
66
+
67
  messages[-1] = ChatMessage(
68
  role="assistant",
69
  content=thought_buffer,
70
  metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
71
  )
72
  yield messages
73
+
74
  # Start response
75
  response_buffer = parts[1].text
76
  print(f"\n=== Starting Response ===\n{response_buffer}")
77
+
78
  messages.append(
79
  ChatMessage(
80
  role="assistant",
 
82
  )
83
  )
84
  thinking_complete = True
85
+
86
  elif thinking_complete:
87
  # Stream response
88
  response_buffer += current_chunk
89
  print(f"\n=== Response Chunk ===\n{current_chunk}")
90
+
91
  messages[-1] = ChatMessage(
92
  role="assistant",
93
  content=response_buffer
94
  )
95
+
96
  else:
97
  # Stream thinking
98
  thought_buffer += current_chunk
99
  print(f"\n=== Thinking Chunk ===\n{current_chunk}")
100
+
101
  messages[-1] = ChatMessage(
102
  role="assistant",
103
  content=thought_buffer,
104
  metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
105
  )
106
+
107
  yield messages
108
+
109
  print(f"\n=== Final Response ===\n{response_buffer}")
110
+
111
  except Exception as e:
112
  print(f"\n=== Error ===\n{str(e)}")
113
  messages.append(
 
122
  """Adds user message to chat history"""
123
  history.append(ChatMessage(role="user", content=msg))
124
  return "", history
125
+
126
 
127
  # Create the Gradio interface
128
  with gr.Blocks(theme=gr.themes.Citrus(), fill_height=True) as demo:
129
+ gr.Markdown(
130
+ """
131
+ # Gemini 2.0 Flash 'Thinking' Chatbot 💭
132
+
133
+ This chatbot demonstrates the experimental 'thinking' capability of the **Gemini 2.0 Flash** model.
134
+ You can observe the model's thought process as it generates responses, displayed with the "⚙️ Thinking" prefix.
135
+
136
+ **Key Features:**
137
+
138
+ * Powered by Google's **Gemini 2.0 Flash** model.
139
+ * Shows the model's **thoughts** before the final answer (experimental feature).
140
+ * Supports **conversation history** for multi-turn chats.
141
+ * Uses **streaming** for a more interactive experience.
142
+
143
+ **Instructions:**
144
+
145
+ 1. Type your message in the input box below.
146
+ 2. Press Enter or click Submit to send.
147
+ 3. Observe the chatbot's "Thinking" process followed by the final response.
148
+ 4. Use the "Clear Chat" button to start a new conversation.
149
+
150
+ *Please note*: The 'thinking' feature is experimental and the quality of thoughts may vary.
151
+ """
152
+ )
153
 
154
  chatbot = gr.Chatbot(
155
  type="messages",
 
171
 
172
  # Set up event handlers
173
  msg_store = gr.State("") # Store for preserving user message
174
+
175
  input_box.submit(
176
  lambda msg: (msg, msg, ""), # Store message and clear input
177
  inputs=[input_box],