nguyenbh commited on
Commit
d459b9e
·
1 Parent(s): 38c463d

update app

Browse files
Files changed (1) hide show
  1. app.py +212 -40
app.py CHANGED
@@ -24,15 +24,17 @@ except Exception as e:
24
  print("Could not get model info:", str(e))
25
 
26
  # Configuration parameters
27
- default_temperature = 0.8
28
- default_max_tokens = 2048
29
  default_top_p = 0.1
30
 
31
  # Example prompts that users can try
32
  example_prompts = [
 
33
  "I have $20,000 in my savings account, where I receive a 4% profit per year and payments twice a year. Can you please tell me how long it will take for me to become a millionaire?",
34
  "I have total $500 create a plan with travel and food",
35
  "I have $1000 and 5 years. Is it better to invest in a stock paying $15 quarterly dividends or in a 5% annual savings account?"
 
36
  ]
37
 
38
  def get_azure_response(message, chat_history, temperature, max_tokens, top_p):
@@ -40,7 +42,7 @@ def get_azure_response(message, chat_history, temperature, max_tokens, top_p):
40
  Function to get a response from the Azure Phi-4 model
41
  """
42
  # Prepare conversation history in the format expected by Azure
43
- messages = [{"role": "system", "content": "You are a helpful AI assistant."}]
44
 
45
  # Add conversation history
46
  for human, assistant in chat_history:
@@ -59,48 +61,158 @@ def get_azure_response(message, chat_history, temperature, max_tokens, top_p):
59
  "top_p": top_p,
60
  "presence_penalty": 0,
61
  "frequency_penalty": 0,
 
62
  }
63
 
64
  # Get response
65
  try:
66
  print("Sending request to Azure...")
67
  response = client.complete(payload)
68
- reply = response.choices[0].message.content
69
-
70
- # Print usage statistics
71
- print(f"Usage - Prompt tokens: {response.usage.prompt_tokens}, "
72
- f"Completion tokens: {response.usage.completion_tokens}, "
73
- f"Total tokens: {response.usage.total_tokens}")
74
-
75
- return reply
76
  except Exception as e:
77
  print(f"Error getting response: {str(e)}")
78
  return f"Error: {str(e)}"
79
 
80
- # Create the Gradio interface
81
- with gr.Blocks(title="Phi-4-mini Chatbot") as demo:
82
- gr.Markdown("Chat with the Phi-4 mini model hosted on Azure AI")
83
-
84
- # Create a chatbot component
85
- chatbot = gr.Chatbot(height=300)
86
- msg = gr.Textbox(label="Type your message here", placeholder="Ask me anything...", lines=1)
87
- clear = gr.Button("Clear Conversation")
88
-
89
- # Add examples section
90
- with gr.Accordion("Try these examples", open=True):
91
- examples = gr.Examples(
92
- examples=example_prompts,
93
- inputs=msg
94
- )
95
-
96
- # Add model parameter controls
97
- with gr.Accordion("Model Parameters", open=False):
98
- temp_slider = gr.Slider(minimum=0.0, maximum=1.0, value=default_temperature, step=0.1,
99
- label="Temperature (higher = more creative, lower = more focused)")
100
- max_tokens_slider = gr.Slider(minimum=100, maximum=4096, value=default_max_tokens, step=100,
101
- label="Max Tokens (maximum length of response)")
102
- top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=default_top_p, step=0.1,
103
- label="Top P (diversity of response)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
  # Simplified chat function that handles both sending and receiving messages
106
  def chat(message, history, temperature, max_tokens, top_p):
@@ -111,17 +223,77 @@ with gr.Blocks(title="Phi-4-mini Chatbot") as demo:
111
  response = get_azure_response(message, history, temperature, max_tokens, top_p)
112
 
113
  # Add the exchange to history
114
- history.append((message, response))
115
-
116
- return "", history # Clear the input field after sending
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  # Function to clear the conversation
119
  def clear_conversation():
120
  return [], default_temperature, default_max_tokens, default_top_p
121
 
122
- # Set up event handlers - simplified approach
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  msg.submit(chat, [msg, chatbot, temp_slider, max_tokens_slider, top_p_slider], [msg, chatbot])
 
124
  clear.click(clear_conversation, None, [chatbot, temp_slider, max_tokens_slider, top_p_slider])
 
125
 
126
  # Launch the app
127
- demo.launch(debug=True) # Set share=True to generate a public URL for testing
 
 
24
  print("Could not get model info:", str(e))
25
 
26
  # Configuration parameters
27
+ default_temperature = 0.7
28
+ default_max_tokens = 4096
29
  default_top_p = 0.1
30
 
31
  # Example prompts that users can try
32
  example_prompts = [
33
+ "Explain internet to a medieval knight.",
34
  "I have $20,000 in my savings account, where I receive a 4% profit per year and payments twice a year. Can you please tell me how long it will take for me to become a millionaire?",
35
  "I have total $500 create a plan with travel and food",
36
  "I have $1000 and 5 years. Is it better to invest in a stock paying $15 quarterly dividends or in a 5% annual savings account?"
37
+
38
  ]
39
 
40
  def get_azure_response(message, chat_history, temperature, max_tokens, top_p):
 
42
  Function to get a response from the Azure Phi-4 model
43
  """
44
  # Prepare conversation history in the format expected by Azure
45
+ messages = [{"role": "system", "content": "You are a helpful AI assistant specialized in financial advice and planning."}]
46
 
47
  # Add conversation history
48
  for human, assistant in chat_history:
 
61
  "top_p": top_p,
62
  "presence_penalty": 0,
63
  "frequency_penalty": 0,
64
+ "stream": True
65
  }
66
 
67
  # Get response
68
  try:
69
  print("Sending request to Azure...")
70
  response = client.complete(payload)
71
+ return response
 
 
 
 
 
 
 
72
  except Exception as e:
73
  print(f"Error getting response: {str(e)}")
74
  return f"Error: {str(e)}"
75
 
76
+ # CSS for custom styling
77
+ custom_css = """
78
+ .container {
79
+ max-width: 1000px !important;
80
+ margin-left: auto !important;
81
+ margin-right: auto !important;
82
+ padding-top: 0rem !important;
83
+ }
84
+ .header {
85
+ text-align: center;
86
+ margin-bottom: 0rem;
87
+ }
88
+ .header h1 {
89
+ font-size: 2.5rem !important;
90
+ font-weight: 700 !important;
91
+ color: #1a5276 !important;
92
+ margin-bottom: 0.5rem !important;
93
+ }
94
+ .header p {
95
+ font-size: 1.2rem !important;
96
+ color: #34495e !important;
97
+ }
98
+ .chatbot-container {
99
+ border-radius: 10px !important;
100
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) !important;
101
+ overflow: hidden !important;
102
+ }
103
+ .emoji-button {
104
+ background: none !important;
105
+ border: none !important;
106
+ padding: 0.2rem 0.5rem !important;
107
+ font-size: 1.5rem !important;
108
+ cursor: pointer !important;
109
+ transition: transform 0.2s !important;
110
+ }
111
+
112
+ .emoji-button:hover {
113
+ transform: scale(1.2) !important;
114
+ }
115
+
116
+ .message-input {
117
+ margin-top: 1rem !important;
118
+ display: flex !important;
119
+ align-items: center !important;
120
+ }
121
+ .footer {
122
+ margin-top: 2rem;
123
+ text-align: center;
124
+ font-size: 0.9rem;
125
+ color: #7f8c8d;
126
+ }
127
+ .parameters-section {
128
+ background-color: #f8f9fa !important;
129
+ padding: 1rem !important;
130
+ border-radius: 8px !important;
131
+ margin-top: 1rem !important;
132
+ }
133
+ .examples-section {
134
+ background-color: #e8f4f8 !important;
135
+ padding: 1rem !important;
136
+ border-radius: 8px !important;
137
+ margin-top: 1rem !important;
138
+ }
139
+ """
140
+
141
+ # Create the Gradio interface with a modern, professional design
142
+ with gr.Blocks(css=custom_css, title="Phi-4-mini Playground") as demo:
143
+ with gr.Column(elem_classes="container"):
144
+ # Header section
145
+ with gr.Column(elem_classes="header"):
146
+ gr.Markdown("# Phi-4-mini Playground")
147
+
148
+ # Main chat interface
149
+ with gr.Column(elem_classes="chatbot-container"):
150
+ chatbot = gr.Chatbot(
151
+ height=500,
152
+ bubble_full_width=False,
153
+ show_label=False,
154
+ avatar_images=(None, "https://upload.wikimedia.org/wikipedia/commons/d/d3/Phi-integrated-information-symbol.png")
155
+ )
156
+
157
+ with gr.Row(elem_classes="message-input"):
158
+ msg = gr.Textbox(
159
+ label="Your message",
160
+ placeholder="Ask about financial planning, investments, or budgeting...",
161
+ lines=2,
162
+ show_label=False,
163
+ container=False,
164
+ scale=8
165
+ )
166
+ send_btn = gr.Button("📤Send", size="sm", variant="primary", scale=1)
167
+ clear = gr.Button("🗑️ Clear", size="sm", variant="secondary", scale=1)
168
+ regenerate = gr.Button("🔄 Regenerate", size="sm", variant="secondary", scale=1)
169
+
170
+ # Examples section
171
+ with gr.Accordion("Try these examples", open=True, elem_classes="examples-section"):
172
+ examples = gr.Examples(
173
+ examples=example_prompts,
174
+ inputs=msg,
175
+ # label="Financial Questions"
176
+ )
177
+
178
+ # Model parameters section
179
+ with gr.Accordion("Advanced Settings", open=False, elem_classes="parameters-section"):
180
+ with gr.Row():
181
+ with gr.Column(scale=1):
182
+ gr.Markdown("### Response Parameters")
183
+
184
+ with gr.Row():
185
+ with gr.Column(scale=1):
186
+ temp_slider = gr.Slider(
187
+ minimum=0.0,
188
+ maximum=1.0,
189
+ value=default_temperature,
190
+ step=0.1,
191
+ label="Temperature",
192
+ info="Higher = more creative, lower = more focused"
193
+ )
194
+ with gr.Column(scale=1):
195
+ top_p_slider = gr.Slider(
196
+ minimum=0.1,
197
+ maximum=1.0,
198
+ value=default_top_p,
199
+ step=0.1,
200
+ label="Top P",
201
+ info="Controls diversity of responses"
202
+ )
203
+ with gr.Column(scale=1):
204
+ max_tokens_slider = gr.Slider(
205
+ minimum=100,
206
+ maximum=32000,
207
+ value=default_max_tokens,
208
+ step=100,
209
+ label="Max Tokens",
210
+ info="Maximum length of response"
211
+ )
212
+
213
+ # Footer
214
+ with gr.Column(elem_classes="footer"):
215
+ gr.Markdown("Powered by Microsoft Phi-4 mini model on Azure AI. © 2025")
216
 
217
  # Simplified chat function that handles both sending and receiving messages
218
  def chat(message, history, temperature, max_tokens, top_p):
 
223
  response = get_azure_response(message, history, temperature, max_tokens, top_p)
224
 
225
  # Add the exchange to history
226
+ history.append((message, ""))
227
+
228
+ response_index = len(history) - 1 # Create a blank index for the newest response
229
+ full_response = "" # Stream the response
230
+ try:
231
+ print("Streaming response from Azure...")
232
+ for chunk in response:
233
+ if chunk.choices:
234
+ content = chunk.choices[0].delta.content
235
+ if content:
236
+ full_response += content
237
+ # Update the response in place
238
+ history[response_index] = (message, full_response)
239
+ # Yield the updated history
240
+ yield "", history
241
+
242
+ # Print usage statistics at the end
243
+ print("Streaming completed")
244
+
245
+ # Return the final state
246
+ return "", history
247
+ except Exception as e:
248
+ error_message = f"Error: {str(e)}"
249
+ print(error_message)
250
+ # Update history with error message
251
+ history[response_index] = (message, error_message)
252
+ return "", history
253
 
254
  # Function to clear the conversation
255
  def clear_conversation():
256
  return [], default_temperature, default_max_tokens, default_top_p
257
 
258
+ # Function to regenerate the last response
259
+ def regenerate_response(history, temperature, max_tokens, top_p):
260
+ if not history:
261
+ return history
262
+
263
+ last_user_message = history[-1][0]
264
+ # Remove the last exchange
265
+ history = history[:-1]
266
+
267
+ # Get new response
268
+ response = get_azure_response(last_user_message, history, temperature, max_tokens, top_p)
269
+
270
+ # Add the exchange to history
271
+ history.append((last_user_message, ""))
272
+
273
+ response_index = len(history) - 1
274
+ full_response = ""
275
+
276
+ try:
277
+ for chunk in response:
278
+ if chunk.choices:
279
+ content = chunk.choices[0].delta.content
280
+ if content:
281
+ full_response += content
282
+ history[response_index] = (last_user_message, full_response)
283
+ yield history
284
+
285
+ return history
286
+ except Exception as e:
287
+ error_message = f"Error: {str(e)}"
288
+ history[response_index] = (last_user_message, error_message)
289
+ return history
290
+
291
+ # Set up event handlers
292
  msg.submit(chat, [msg, chatbot, temp_slider, max_tokens_slider, top_p_slider], [msg, chatbot])
293
+ send_btn.click(chat, [msg, chatbot, temp_slider, max_tokens_slider, top_p_slider], [msg, chatbot])
294
  clear.click(clear_conversation, None, [chatbot, temp_slider, max_tokens_slider, top_p_slider])
295
+ regenerate.click(regenerate_response, [chatbot, temp_slider, max_tokens_slider, top_p_slider], [chatbot])
296
 
297
  # Launch the app
298
+
299
+ demo.launch(share=True) # Set share=True to generate a public URL for testing