zelk12 commited on
Commit
281b54c
·
verified ·
1 Parent(s): 1c4e28d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +315 -84
app.py CHANGED
@@ -1,87 +1,318 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- print(",3521534532")
5
- print(",350164845646531458641654352648621035421534532")
6
- print(",3521534532")
7
- print(",3521534532")
8
- print(",3521534532")
9
- print(",3521534532")
10
- print(",3521534532")
11
- print(",3521534532")
12
- print(",3521534532")
13
- print(",3521534532")
14
- print(",3521534532")
15
- print(",3521534532")
16
- print(",3521534532")
17
- print(",3521534532")
18
- print(",3521534532")
19
- print(",3521534532")
20
- print(",3521534532")
21
- print(",3521534532")
22
-
23
- """
24
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
25
- """
26
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
27
-
28
-
29
- def respond(
30
- message,
31
- history: list[tuple[str, str]],
32
- system_message,
33
- max_tokens,
34
- temperature,
35
- top_p,
36
- ):
37
- messages = [{"role": "system", "content": system_message}]
38
-
39
- for val in history:
40
- if val[0]:
41
- messages.append({"role": "user", "content": val[0]})
42
- if val[1]:
43
- messages.append({"role": "assistant", "content": val[1]})
44
-
45
- messages.append({"role": "user", "content": message})
46
-
47
- response = ""
48
-
49
- for message in client.chat_completion(
50
- messages,
51
- max_tokens=max_tokens,
52
- stream=True,
53
- temperature=temperature,
54
- top_p=top_p,
55
- ):
56
- token = message.choices[0].delta.content
57
-
58
- response += token
59
- yield response
60
-
61
-
62
- """
63
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
64
- """
65
- demo = gr.ChatInterface(
66
- respond,
67
- additional_inputs=[
68
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
69
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
70
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
71
- gr.Slider(
72
- minimum=0.1,
73
- maximum=1.0,
74
- value=0.95,
75
- step=0.05,
76
- label="Top-p (nucleus sampling)",
77
- ),
78
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
79
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
80
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
81
- ],
82
- )
83
-
84
- print("016484560164845646531458641654352648621035446531016484564653145864165435264862103544586416543526486210354")
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  if __name__ == "__main__":
87
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
+ from gradio import ChatMessage
4
+ from typing import Iterator
5
+ import google.generativeai as genai
6
+ import time # Import time module for potential debugging/delay
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ print("import library complete")
9
+ print("add API key")
10
+
11
+ # get Gemini API Key from the environ variable
12
+ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
13
+ genai.configure(api_key=GEMINI_API_KEY)
14
+
15
+ print("add API key complete ")
16
+ print("add model")
17
+
18
+ used_model = "gemini-2.5-pro-exp-03-25"
19
+
20
+ # we will be using the Gemini 2.0 Flash model with Thinking capabilities
21
+ model = genai.GenerativeModel("gemini-2.0-flash-thinking-exp-01-21")
22
+
23
+ print(f"add model {used_model} complete\n")
24
+
25
+ def format_chat_history(messages: list) -> list:
26
+ print("\nstart format history")
27
+ """
28
+ Formats the chat history into a structure Gemini can understand
29
+ """
30
+ formatted_history = []
31
+ for message in messages:
32
+ #print(f"t1 {message}")
33
+ # Skip thinking messages (messages with metadata)
34
+ #if not (message.get("role") == "assistant" and "metadata" in message):
35
+ # print(f"t2 {message}")
36
+ # formatted_history.append({
37
+ # "role": "user" if message.get("role") == "user" else "assistant",
38
+ # "parts": [message.get("content", "")]
39
+ # })
40
+
41
+ #print(f"t2 {message}")
42
+
43
+ if message.get("role") == "user" :
44
+ formatted_history.append({
45
+ "role": "user",
46
+ "parts": [message.get("content", "")]
47
+ })
48
+ elif message.get("role") == "assistant" :
49
+ formatted_history.append({
50
+ "role": "model",
51
+ "parts": [message.get("content", "")]
52
+ })
53
+
54
+ #print(f"t3 {formatted_history}")
55
+ print("return formatted history")
56
+ return formatted_history
57
+
58
+ def stream_gemini_response(user_message: str, messages: list) -> Iterator[list]:
59
+ print("start model response stream")
60
+ """
61
+ Streams thoughts and response with conversation history support for text input only.
62
+ """
63
+ if not user_message.strip(): # Robust check: if text message is empty or whitespace
64
+ messages.append(ChatMessage(role="assistant", content="Please provide a non-empty text message. Empty input is not allowed.")) # More specific message
65
+ yield messages
66
+ print("Empty text message")
67
+ return
68
+
69
+ try:
70
+ print(f"\n=== New Request (Text) ===")
71
+ print(f"User message: {user_message}")
72
+
73
+ # Format chat history for Gemini
74
+ chat_history = format_chat_history(messages)
75
+
76
+ #print(f"hist {chat_history}")
77
+
78
+ # Initialize Gemini chat
79
+ print("Chat parameter")
80
+ chat = model.start_chat(history=chat_history)
81
+ print("Start response")
82
+ response = chat.send_message(user_message, stream=True)
83
+
84
+ # Initialize buffers and flags
85
+ thought_buffer = ""
86
+ response_buffer = ""
87
+ #thinking_complete = False
88
+
89
+ # Add initial thinking message
90
+ #messages.append(
91
+ # ChatMessage(
92
+ # role="assistant",
93
+ # content="",
94
+ # metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
95
+ # )
96
+ #)
97
+
98
+ messages.append(
99
+ ChatMessage(
100
+ role="assistant",
101
+ content=response_buffer
102
+ )
103
+ )
104
+ #print(f"mes {messages} \n\nhis {chat_history}")
105
+
106
+ thinking_complete = True
107
+
108
+ for chunk in response:
109
+ print("chunk start")
110
+ parts = chunk.candidates[0].content.parts
111
+ current_chunk = parts[0].text
112
+
113
+ print(f"\n=========\nparts len: {len(parts)}\n\nparts: {parts}\n\ncurrent chunk: {current_chunk}\n=========\n")
114
+
115
+ if len(parts) == 2 and not thinking_complete:
116
+ # Complete thought and start response
117
+ thought_buffer += current_chunk
118
+ print(f"\n=== Complete Thought ===\n{thought_buffer}")
119
+
120
+ messages[-1] = ChatMessage(
121
+ role="assistant",
122
+ content=thought_buffer,
123
+ metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
124
+ )
125
+ yield messages
126
+
127
+ # Start response
128
+ response_buffer = parts[1].text
129
+ print(f"\n=== Starting Response ===\n{response_buffer}")
130
+
131
+ messages.append(
132
+ ChatMessage(
133
+ role="assistant",
134
+ content=response_buffer
135
+ )
136
+ )
137
+ thinking_complete = True
138
+
139
+ elif thinking_complete:
140
+ # Stream response
141
+ response_buffer += current_chunk
142
+ print(f"\n=== Response Chunk ===\n{current_chunk}")
143
+
144
+ messages[-1] = ChatMessage(
145
+ role="assistant",
146
+ content=response_buffer
147
+ )
148
+
149
+ else:
150
+ # Stream thinking
151
+ thought_buffer += current_chunk
152
+ print(f"\n=== Thinking Chunk ===\n{current_chunk}")
153
+
154
+ messages[-1] = ChatMessage(
155
+ role="assistant",
156
+ content=thought_buffer,
157
+ metadata={"title": "⚙️ Thinking: *The thoughts produced by the model are experimental"}
158
+ )
159
+ #time.sleep(0.05) #Optional: Uncomment this line to add a slight delay for debugging/visualization of streaming. Remove for final version
160
+ print("Response end")
161
+ yield messages
162
+
163
+ print(f"\n=== Final Response ===\n{response_buffer}")
164
+
165
+ except Exception as e:
166
+ print(f"\n=== Error ===\n{str(e)}")
167
+ messages.append(
168
+ ChatMessage(
169
+ role="assistant",
170
+ content=f"I apologize, but I encountered an error: {str(e)}"
171
+ )
172
+ )
173
+ yield messages
174
+
175
+ def user_message(msg: str, history: list) -> tuple[str, list]:
176
+ """Adds user message to chat history"""
177
+ history.append(ChatMessage(role="user", content=msg))
178
+ return "", history
179
+
180
+
181
+ # Create the Gradio interface
182
+ 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
183
+ gr.Markdown("# Chat with " + used_model)
184
+
185
+
186
+ gr.HTML("""<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fzelk12%2FGemini-2">
187
+ <img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fzelk12%2FGemini-2&countColor=%23263759" />
188
+ </a>""")
189
+
190
+
191
+ chatbot = gr.Chatbot(
192
+ type="messages",
193
+ label=used_model + " Chatbot (Streaming Output)", #Label now indicates streaming
194
+ render_markdown=True,
195
+ scale=1,
196
+ editable="all",
197
+ avatar_images=(None,"https://lh3.googleusercontent.com/oxz0sUBF0iYoN4VvhqWTmux-cxfD1rxuYkuFEfm1SFaseXEsjjE4Je_C_V3UQPuJ87sImQK3HfQ3RXiaRnQetjaZbjJJUkiPL5jFJ1WRl5FKJZYibUA=w214-h214-n-nu")
198
+ )
199
+
200
+ with gr.Row(equal_height=True):
201
+ input_box = gr.Textbox(
202
+ lines=1,
203
+ label="Chat Message",
204
+ placeholder="Type your message here...",
205
+ scale=4
206
+ )
207
+
208
+ with gr.Column(scale=1):
209
+ submit_button = gr.Button("Submit", scale=1)
210
+ clear_button = gr.Button("Clear Chat", scale=1)
211
+
212
+ with gr.Row(equal_height=True):
213
+ test_button = gr.Button("test", scale=1)
214
+ test1_button = gr.Button("test1", scale=1)
215
+ test2_button = gr.Button("test2", scale=1)
216
+ test3_button = gr.Button("test3", scale=1)
217
+
218
+ # Add example prompts - removed file upload examples. Kept text focused examples.
219
+ example_prompts = [
220
+ ["Write a short poem about the sunset."],
221
+ ["Explain the theory of relativity in simple terms."],
222
+ ["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?"],
223
+ ["Summarize the plot of Hamlet."],
224
+ ["Write a haiku about a cat."]
225
+ ]
226
+
227
+ gr.Examples(
228
+ examples=example_prompts,
229
+ inputs=input_box,
230
+ label="Examples: Try these prompts to see Gemini's thinking!",
231
+ examples_per_page=5 # Adjust as needed
232
+ )
233
+
234
+ # Created by gemini-2.5-pro-exp-03-25
235
+ #def process_message(msg):
236
+ # """Обрабатывает сообщение пользователя: сохраняет, отображает и генерирует ответ."""
237
+ # msg_store_val, _, _ = lambda msg: (msg, msg, "")(msg) # Store message and clear input (inline lambda)
238
+ # input_box_val, chatbot_val = user_message(msg_store_val, chatbot) # Add user message to chat
239
+ # chatbot_val_final = stream_gemini_response(msg_store_val, chatbot_val) # Generate and stream response
240
+ # return msg_store_val, input_box_val, chatbot_val_final
241
+ #
242
+ #input_box.submit(
243
+ # process_message,
244
+ # inputs=[input_box],
245
+ # outputs=[msg_store, input_box, chatbot], # Исправлены outputs, чтобы включать chatbot
246
+ # queue=False
247
+ #)
248
+
249
+ #submit_button.click(
250
+ # process_message,
251
+ # inputs=[input_box],
252
+ # outputs=[msg_store, input_box, chatbot], # Исправлены outputs, чтобы включать chatbot
253
+ # queue=False
254
+ #)
255
+
256
+ # Set up event handlers
257
+ msg_store = gr.State("") # Store for preserving user message
258
+
259
+ input_box.submit(
260
+ lambda msg: (msg, msg, ""), # Store message and clear input
261
+ inputs=[input_box],
262
+ outputs=[msg_store, input_box, input_box],
263
+ queue=False
264
+ ).then(
265
+ user_message, # Add user message to chat
266
+ inputs=[msg_store, chatbot],
267
+ outputs=[input_box, chatbot],
268
+ queue=False
269
+ ).then(
270
+ stream_gemini_response, # Generate and stream response
271
+ inputs=[msg_store, chatbot],
272
+ outputs=chatbot
273
+ )
274
+
275
+ submit_button.click(
276
+ lambda msg: (msg, msg, ""), # Store message and clear input
277
+ inputs=[input_box],
278
+ outputs=[msg_store, input_box, input_box],
279
+ queue=False
280
+ ).then(
281
+ user_message, # Add user message to chat
282
+ inputs=[msg_store, chatbot],
283
+ outputs=[input_box, chatbot],
284
+ queue=False
285
+ ).then(
286
+ stream_gemini_response, # Generate and stream response
287
+ inputs=[msg_store, chatbot],
288
+ outputs=chatbot
289
+ )
290
+
291
+ clear_button.click(
292
+ lambda: ([], "", ""),
293
+ outputs=[chatbot, input_box, msg_store],
294
+ queue=False
295
+ )
296
+
297
+ gr.Markdown( # Description moved to the bottom - updated for text-only
298
+ """
299
+ <br><br><br> <!-- Add some vertical space -->
300
+ ---
301
+ ### About this Chatbot
302
+ **Try out the example prompts below to see Gemini in action!**
303
+ **Key Features:**
304
+ * Powered by Google's **Gemini 2.0 Flash** model.
305
+ * Supports **conversation history** for multi-turn chats.
306
+ * Uses **streaming** for a more interactive experience.
307
+ **Instructions:**
308
+ 1. Type your message in the input box below or select an example.
309
+ 2. Press Enter or click Submit to send.
310
+ 3. Observe the chatbot's "Thinking" process followed by the final response.
311
+ 4. Use the "Clear Chat" button to start a new conversation.
312
+ """
313
+ )
314
+
315
+
316
+ # Launch the interface
317
  if __name__ == "__main__":
318
+ demo.launch(debug=True)