File size: 11,537 Bytes
eb8806e
 
0534f4a
eb8806e
 
0534f4a
eb8806e
464e08f
 
 
0534f4a
eb8806e
 
 
2134b04
464e08f
 
f188268
ea8adcf
0534f4a
0950619
eb8806e
c2a5dbd
eb8806e
0534f4a
f188268
eb8806e
0534f4a
eb8806e
0534f4a
 
370c76a
0534f4a
85afa5c
 
 
 
 
 
 
370c76a
464e08f
 
 
 
85afa5c
464e08f
ef02466
464e08f
 
 
 
85afa5c
370c76a
464e08f
0534f4a
 
 
464e08f
cf7da81
0534f4a
cf7da81
0534f4a
 
 
464e08f
0534f4a
eb8806e
0534f4a
 
 
 
 
 
 
8064cc8
9b6c718
0534f4a
464e08f
0534f4a
464e08f
0534f4a
 
 
 
 
0b1a592
0534f4a
 
2b70838
 
 
 
 
 
 
 
0534f4a
 
ef02466
2b70838
0534f4a
 
370c76a
2b70838
 
0534f4a
 
5365097
0534f4a
 
eb8806e
5365097
 
0534f4a
 
 
 
 
 
ef02466
0534f4a
 
 
 
 
 
 
 
 
 
 
ef02466
0534f4a
 
 
 
 
 
 
 
 
 
 
ef02466
0534f4a
 
eb8806e
cf7da81
0534f4a
 
2b70838
eb8806e
0534f4a
ef02466
0534f4a
 
 
 
464e08f
0534f4a
45e9cef
0534f4a
eb8806e
0534f4a
 
 
 
 
 
 
 
 
eb8806e
0534f4a
 
 
 
4eb8efe
5a1d31c
0534f4a
 
ea8adcf
5a1d31c
0534f4a
a6428e6
 
0534f4a
5a1d31c
0534f4a
 
 
21b75aa
0534f4a
 
d87279c
0534f4a
 
eb8806e
0534f4a
 
 
 
 
 
 
 
5365097
 
 
 
 
 
 
 
 
0534f4a
 
 
 
 
 
 
 
cf7da81
eb8806e
0534f4a
 
 
 
 
eb8806e
 
5365097
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0534f4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf7da81
5365097
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf7da81
eb8806e
0534f4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb8806e
0534f4a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import os
import gradio as gr
from gradio import ChatMessage
from typing import Iterator
import google.generativeai as genai
import time # Import time module for potential debugging/delay

print("import library complete")
print("add API key")

# get Gemini API Key from the environ variable
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=GEMINI_API_KEY)

print("add API key complete ")
print("add model")

used_model = "gemini-2.5-pro-exp-03-25"

# we will be using the Gemini 2.0 Flash model with Thinking capabilities
model = genai.GenerativeModel("gemini-2.0-flash-thinking-exp-01-21")

print(f"add model {used_model} complete\n")

def format_chat_history(messages: list) -> list:
    print("\nstart format history")
    """
    Formats the chat history into a structure Gemini can understand
    """
    formatted_history = []
    for message in messages:
        #print(f"t1 {message}")
        # Skip thinking messages (messages with metadata)
        #if not (message.get("role") == "assistant" and "metadata" in message):
        #    print(f"t2 {message}")
        #    formatted_history.append({
        #        "role": "user" if message.get("role") == "user" else "assistant",
        #        "parts": [message.get("content", "")]
        #    })
            
        #print(f"t2 {message}")

        if message.get("role") == "user" :
            formatted_history.append({
            "role": "user",
            "parts": [message.get("content", "")]
            })
        elif message.get("role") == "assistant" :
            formatted_history.append({
            "role": "model",
            "parts": [message.get("content", "")]
            })
        
    #print(f"t3 {formatted_history}")
    print("return formatted history")
    return formatted_history

def stream_gemini_response(user_message: str, messages: list) -> Iterator[list]:
    print("start model response stream")
    """
    Streams thoughts and response with conversation history support for text input only.
    """
    if not user_message.strip(): # Robust check: if text message is empty or whitespace
        messages.append(ChatMessage(role="assistant", content="Please provide a non-empty text message. Empty input is not allowed.")) # More specific message
        yield messages
        print("Empty text message")
        return

    try:
        print(f"\n=== New Request (Text) ===")
        print(f"User message: {user_message}")

        # Format chat history for Gemini
        chat_history = format_chat_history(messages)

        #print(f"hist {chat_history}")
        
        # Initialize Gemini chat
        print("Chat parameter")
        chat = model.start_chat(history=chat_history)
        print("Start response")
        response = chat.send_message(user_message, stream=True)

        # Initialize buffers and flags
        thought_buffer = ""
        response_buffer = ""
        #thinking_complete = False

        # Add initial thinking message
        #messages.append(
        #    ChatMessage(
        #        role="assistant",
        #        content="",
        #        metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
        #    )
        #)
        
        messages.append(
            ChatMessage(
                role="assistant",
                content=response_buffer
            )
        )
        #print(f"mes {messages} \n\nhis {chat_history}")
        
        thinking_complete = True

        for chunk in response:
            print("chunk start")
            parts = chunk.candidates[0].content.parts
            current_chunk = parts[0].text

            print(f"\n=========\nparts len: {len(parts)}\n\nparts: {parts}\n\ncurrent chunk: {current_chunk}\n=========\n")
            
            if len(parts) == 2 and not thinking_complete:
                # Complete thought and start response
                thought_buffer += current_chunk
                print(f"\n=== Complete Thought ===\n{thought_buffer}")

                messages[-1] = ChatMessage(
                    role="assistant",
                    content=thought_buffer,
                    metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
                )
                yield messages

                # Start response
                response_buffer = parts[1].text
                print(f"\n=== Starting Response ===\n{response_buffer}")

                messages.append(
                    ChatMessage(
                        role="assistant",
                        content=response_buffer
                    )
                )
                thinking_complete = True

            elif thinking_complete:
                # Stream response
                response_buffer += current_chunk
                print(f"\n=== Response Chunk ===\n{current_chunk}")

                messages[-1] = ChatMessage(
                    role="assistant",
                    content=response_buffer
                )

            else:
                # Stream thinking
                thought_buffer += current_chunk
                print(f"\n=== Thinking Chunk ===\n{current_chunk}")

                messages[-1] = ChatMessage(
                    role="assistant",
                    content=thought_buffer,
                    metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
                )
            #time.sleep(0.05) #Optional: Uncomment this line to add a slight delay for debugging/visualization of streaming. Remove for final version
            print("Response end")
            yield messages

        print(f"\n=== Final Response ===\n{response_buffer}")

    except Exception as e:
        print(f"\n=== Error ===\n{str(e)}")
        messages.append(
            ChatMessage(
                role="assistant",
                content=f"I apologize, but I encountered an error: {str(e)}"
            )
        )
        yield messages

def user_message(msg: str, history: list) -> tuple[str, list]:
    """Adds user message to chat history"""
    history.append(ChatMessage(role="user", content=msg))
    return "", history


# Create the Gradio interface
with gr.Blocks(theme=gr.themes.Soft(primary_hue="teal", secondary_hue="slate", neutral_hue="neutral")) as demo: # Using Soft theme with adjusted hues for a refined look
    gr.Markdown("# Chat with " + used_model)

    
    gr.HTML("""<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fzelk12%2FGemini-2">
               <img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fzelk12%2FGemini-2&countColor=%23263759" />
               </a>""")

    
    chatbot = gr.Chatbot(
        type="messages",
        label=used_model + " Chatbot (Streaming Output)", #Label now indicates streaming
        render_markdown=True,
        scale=1,
        editable="all",
        avatar_images=(None,"https://lh3.googleusercontent.com/oxz0sUBF0iYoN4VvhqWTmux-cxfD1rxuYkuFEfm1SFaseXEsjjE4Je_C_V3UQPuJ87sImQK3HfQ3RXiaRnQetjaZbjJJUkiPL5jFJ1WRl5FKJZYibUA=w214-h214-n-nu")
    )

    with gr.Row(equal_height=True):
        input_box = gr.Textbox(
            lines=1,
            label="Chat Message",
            placeholder="Type your message here...",
            scale=4
        )

        with gr.Column(scale=1):
            submit_button = gr.Button("Submit", scale=1)
            clear_button = gr.Button("Clear Chat", scale=1)

    with gr.Row(equal_height=True):
        test_button = gr.Button("test", scale=1)
        test1_button = gr.Button("test1", scale=1)
        test2_button = gr.Button("test2", scale=1)
        test3_button = gr.Button("test3", scale=1)

    # Add example prompts - removed file upload examples. Kept text focused examples.
    example_prompts = [
        ["Write a short poem about the sunset."],
        ["Explain the theory of relativity in simple terms."],
        ["If a train leaves Chicago at 6am traveling at 60mph, and another train leaves New York at 8am traveling at 80mph, at what time will they meet?"],
        ["Summarize the plot of Hamlet."],
        ["Write a haiku about a cat."]
    ]

    gr.Examples(
        examples=example_prompts,
        inputs=input_box,
        label="Examples: Try these prompts to see Gemini's thinking!",
        examples_per_page=5 # Adjust as needed
    )

# Created by gemini-2.5-pro-exp-03-25
#def process_message(msg):
#    """Обрабатывает сообщение пользователя: сохраняет, отображает и генерирует ответ."""
#    msg_store_val, _, _ = lambda msg: (msg, msg, "")(msg) # Store message and clear input (inline lambda)
#    input_box_val, chatbot_val = user_message(msg_store_val, chatbot) # Add user message to chat
#    chatbot_val_final = stream_gemini_response(msg_store_val, chatbot_val) # Generate and stream response
#    return msg_store_val, input_box_val, chatbot_val_final
#
#input_box.submit(
#    process_message,
#    inputs=[input_box],
#    outputs=[msg_store, input_box, chatbot],  # Исправлены outputs, чтобы включать chatbot
#    queue=False
#)

#submit_button.click(
#    process_message,
#    inputs=[input_box],
#    outputs=[msg_store, input_box, chatbot],  # Исправлены outputs, чтобы включать chatbot
#    queue=False
#)
    
    # Set up event handlers
    msg_store = gr.State("")  # Store for preserving user message

    input_box.submit(
        lambda msg: (msg, msg, ""),  # Store message and clear input
        inputs=[input_box],
        outputs=[msg_store, input_box, input_box],
        queue=False
    ).then(
        user_message,  # Add user message to chat
        inputs=[msg_store, chatbot],
        outputs=[input_box, chatbot],
        queue=False
    ).then(
        stream_gemini_response,  # Generate and stream response
        inputs=[msg_store, chatbot],
        outputs=chatbot
    )

    submit_button.click(
        lambda msg: (msg, msg, ""),  # Store message and clear input
        inputs=[input_box],
        outputs=[msg_store, input_box, input_box],
        queue=False
    ).then(
        user_message,  # Add user message to chat
        inputs=[msg_store, chatbot],
        outputs=[input_box, chatbot],
        queue=False
    ).then(
        stream_gemini_response,  # Generate and stream response
        inputs=[msg_store, chatbot],
        outputs=chatbot
    )

    clear_button.click(
        lambda: ([], "", ""),
        outputs=[chatbot, input_box, msg_store],
        queue=False
    )

    gr.Markdown(  # Description moved to the bottom - updated for text-only
        """
        <br><br><br>  <!-- Add some vertical space -->
        ---
        ### About this Chatbot
        **Try out the example prompts below to see Gemini in action!**
        **Key Features:**
        *   Powered by Google's **Gemini 2.0 Flash** model.
        *   Supports **conversation history** for multi-turn chats.
        *   Uses **streaming** for a more interactive experience.
        **Instructions:**
        1.  Type your message in the input box below or select an example.
        2.  Press Enter or click Submit to send.
        3.  Observe the chatbot's "Thinking" process followed by the final response.
        4.  Use the "Clear Chat" button to start a new conversation.
        """
    )


# Launch the interface
if __name__ == "__main__":
    demo.launch(debug=True)