Spaces:
Running
Running
nguyenbh
commited on
Commit
·
5f2dccf
1
Parent(s):
d00bdd7
Add penalty control
Browse files
app.py
CHANGED
@@ -29,6 +29,8 @@ except Exception as e:
|
|
29 |
default_temperature = 0.7
|
30 |
default_max_tokens = 4096
|
31 |
default_top_p = 0.1
|
|
|
|
|
32 |
|
33 |
# Example prompts that users can try
|
34 |
example_prompts = [
|
@@ -39,7 +41,7 @@ example_prompts = [
|
|
39 |
"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?",
|
40 |
]
|
41 |
|
42 |
-
def get_azure_response(message, chat_history, temperature, max_tokens, top_p):
|
43 |
"""
|
44 |
Function to get a response from the Azure Phi-4 model
|
45 |
"""
|
@@ -61,8 +63,8 @@ def get_azure_response(message, chat_history, temperature, max_tokens, top_p):
|
|
61 |
"max_tokens": max_tokens,
|
62 |
"temperature": temperature,
|
63 |
"top_p": top_p,
|
64 |
-
"presence_penalty":
|
65 |
-
"frequency_penalty":
|
66 |
"stream": True
|
67 |
}
|
68 |
|
@@ -277,6 +279,10 @@ with gr.Blocks(css=custom_css, title="Phi-4-mini Playground") as demo:
|
|
277 |
# Header section
|
278 |
with gr.Column(elem_classes="header"):
|
279 |
gr.Markdown("# Phi-4-mini Playground")
|
|
|
|
|
|
|
|
|
280 |
|
281 |
# Main content with side-by-side layout
|
282 |
with gr.Row():
|
@@ -343,18 +349,36 @@ with gr.Blocks(css=custom_css, title="Phi-4-mini Playground") as demo:
|
|
343 |
label="Max Tokens",
|
344 |
info="Maximum length of response"
|
345 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
346 |
|
347 |
# Footer
|
348 |
with gr.Column(elem_classes="footer"):
|
349 |
gr.Markdown("Powered by Microsoft [Phi-4 mini model](https://aka.ms/phi-4-mini/azure) on Azure AI. © 2025")
|
350 |
|
351 |
# Simplified chat function that handles both sending and receiving messages
|
352 |
-
def chat(message, history, temperature, max_tokens, top_p):
|
353 |
if not message.strip():
|
354 |
return "", history
|
355 |
|
356 |
# Get response from Azure
|
357 |
-
response = get_azure_response(message, history, temperature, max_tokens, top_p)
|
358 |
|
359 |
# Add the exchange to history
|
360 |
history.append((message, ""))
|
@@ -387,10 +411,10 @@ with gr.Blocks(css=custom_css, title="Phi-4-mini Playground") as demo:
|
|
387 |
|
388 |
# Function to clear the conversation
|
389 |
def clear_conversation():
|
390 |
-
return [], default_temperature, default_max_tokens, default_top_p
|
391 |
|
392 |
# Function to regenerate the last response
|
393 |
-
def regenerate_response(history, temperature, max_tokens, top_p):
|
394 |
if not history:
|
395 |
return history
|
396 |
|
@@ -399,7 +423,7 @@ with gr.Blocks(css=custom_css, title="Phi-4-mini Playground") as demo:
|
|
399 |
history = history[:-1]
|
400 |
|
401 |
# Get new response
|
402 |
-
response = get_azure_response(last_user_message, history, temperature, max_tokens, top_p)
|
403 |
|
404 |
# Add the exchange to history
|
405 |
history.append((last_user_message, ""))
|
@@ -423,10 +447,10 @@ with gr.Blocks(css=custom_css, title="Phi-4-mini Playground") as demo:
|
|
423 |
return history
|
424 |
|
425 |
# Set up event handlers
|
426 |
-
msg.submit(chat, [msg, chatbot, temp_slider, max_tokens_slider, top_p_slider], [msg, chatbot])
|
427 |
-
send_btn.click(chat, [msg, chatbot, temp_slider, max_tokens_slider, top_p_slider], [msg, chatbot])
|
428 |
-
clear.click(clear_conversation, None, [chatbot, temp_slider, max_tokens_slider, top_p_slider])
|
429 |
-
regenerate.click(regenerate_response, [chatbot, temp_slider, max_tokens_slider, top_p_slider], [chatbot])
|
430 |
|
431 |
# Launch the app
|
432 |
-
demo.launch(share=True) # Set share=True to generate a public URL for testing
|
|
|
29 |
default_temperature = 0.7
|
30 |
default_max_tokens = 4096
|
31 |
default_top_p = 0.1
|
32 |
+
default_presence_penalty = 0.0
|
33 |
+
default_frequency_penalty = 0.0
|
34 |
|
35 |
# Example prompts that users can try
|
36 |
example_prompts = [
|
|
|
41 |
"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?",
|
42 |
]
|
43 |
|
44 |
+
def get_azure_response(message, chat_history, temperature, max_tokens, top_p, presence_penalty, frequency_penalty):
|
45 |
"""
|
46 |
Function to get a response from the Azure Phi-4 model
|
47 |
"""
|
|
|
63 |
"max_tokens": max_tokens,
|
64 |
"temperature": temperature,
|
65 |
"top_p": top_p,
|
66 |
+
"presence_penalty": presence_penalty,
|
67 |
+
"frequency_penalty": frequency_penalty,
|
68 |
"stream": True
|
69 |
}
|
70 |
|
|
|
279 |
# Header section
|
280 |
with gr.Column(elem_classes="header"):
|
281 |
gr.Markdown("# Phi-4-mini Playground")
|
282 |
+
gr.Markdown("""This demo allows you to interact with the [Phi-4-Mini](https://aka.ms/phi-4-multimodal/techreport).
|
283 |
+
Other demos include [Thoughts Organizer](https://microsoft-thoughtsorganizer.hf.space/),
|
284 |
+
[Stories Come Alive](https://microsoft-storiescomealive.hf.space/),
|
285 |
+
[Phine Speech Translator](https://microsoft-phinespeechtranslator.hf.space/)""")
|
286 |
|
287 |
# Main content with side-by-side layout
|
288 |
with gr.Row():
|
|
|
349 |
label="Max Tokens",
|
350 |
info="Maximum length of response"
|
351 |
)
|
352 |
+
|
353 |
+
# New sliders for presence and frequency penalty
|
354 |
+
presence_penalty_slider = gr.Slider(
|
355 |
+
minimum=-2.0,
|
356 |
+
maximum=2.0,
|
357 |
+
value=default_presence_penalty,
|
358 |
+
step=0.1,
|
359 |
+
label="Presence Penalty",
|
360 |
+
info="Positive values increase likelihood to talk about new topics"
|
361 |
+
)
|
362 |
+
frequency_penalty_slider = gr.Slider(
|
363 |
+
minimum=-2.0,
|
364 |
+
maximum=2.0,
|
365 |
+
value=default_frequency_penalty,
|
366 |
+
step=0.1,
|
367 |
+
label="Frequency Penalty",
|
368 |
+
info="Positive values decrease likelihood to repeat the same text"
|
369 |
+
)
|
370 |
|
371 |
# Footer
|
372 |
with gr.Column(elem_classes="footer"):
|
373 |
gr.Markdown("Powered by Microsoft [Phi-4 mini model](https://aka.ms/phi-4-mini/azure) on Azure AI. © 2025")
|
374 |
|
375 |
# Simplified chat function that handles both sending and receiving messages
|
376 |
+
def chat(message, history, temperature, max_tokens, top_p, presence_penalty, frequency_penalty):
|
377 |
if not message.strip():
|
378 |
return "", history
|
379 |
|
380 |
# Get response from Azure
|
381 |
+
response = get_azure_response(message, history, temperature, max_tokens, top_p, presence_penalty, frequency_penalty)
|
382 |
|
383 |
# Add the exchange to history
|
384 |
history.append((message, ""))
|
|
|
411 |
|
412 |
# Function to clear the conversation
|
413 |
def clear_conversation():
|
414 |
+
return [], default_temperature, default_max_tokens, default_top_p, default_presence_penalty, default_frequency_penalty
|
415 |
|
416 |
# Function to regenerate the last response
|
417 |
+
def regenerate_response(history, temperature, max_tokens, top_p, presence_penalty, frequency_penalty):
|
418 |
if not history:
|
419 |
return history
|
420 |
|
|
|
423 |
history = history[:-1]
|
424 |
|
425 |
# Get new response
|
426 |
+
response = get_azure_response(last_user_message, history, temperature, max_tokens, top_p, presence_penalty, frequency_penalty)
|
427 |
|
428 |
# Add the exchange to history
|
429 |
history.append((last_user_message, ""))
|
|
|
447 |
return history
|
448 |
|
449 |
# Set up event handlers
|
450 |
+
msg.submit(chat, [msg, chatbot, temp_slider, max_tokens_slider, top_p_slider, presence_penalty_slider, frequency_penalty_slider], [msg, chatbot])
|
451 |
+
send_btn.click(chat, [msg, chatbot, temp_slider, max_tokens_slider, top_p_slider, presence_penalty_slider, frequency_penalty_slider], [msg, chatbot])
|
452 |
+
clear.click(clear_conversation, None, [chatbot, temp_slider, max_tokens_slider, top_p_slider, presence_penalty_slider, frequency_penalty_slider])
|
453 |
+
regenerate.click(regenerate_response, [chatbot, temp_slider, max_tokens_slider, top_p_slider, presence_penalty_slider, frequency_penalty_slider], [chatbot])
|
454 |
|
455 |
# Launch the app
|
456 |
+
demo.launch(share=True) # Set share=True to generate a public URL for testing
|