nguyenbh commited on
Commit
6d105e8
·
1 Parent(s): cbde992

update params

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -21,10 +21,17 @@ url = os.getenv("AZURE_ENDPOINT")
21
  api_key = os.getenv("AZURE_API_KEY")
22
 
23
 
 
 
 
 
 
 
 
24
  # Initialize MIME types
25
  mimetypes.init()
26
 
27
- def call_aml_endpoint(payload, url, api_key):
28
  """Call Azure ML endpoint with the given payload."""
29
  # Allow self-signed HTTPS certificates
30
  def allow_self_signed_https(allowed):
@@ -33,17 +40,24 @@ def call_aml_endpoint(payload, url, api_key):
33
 
34
  allow_self_signed_https(True)
35
 
36
- # Set parameters (can be adjusted based on your needs)
37
- parameters = {"temperature": 0.7}
 
 
 
 
 
 
 
38
 
39
- # parameters = {
40
- # "max_tokens": 4096,
41
- # "temperature": 0.7,
42
- # "top_p": 0.1,
43
- # "presence_penalty": 0.0,
44
- # "frequency_penalty": 0.0,
45
- # "stream": True
46
- # }
47
 
48
  if "parameters" not in payload["input_data"]:
49
  payload["input_data"]["parameters"] = parameters
@@ -62,6 +76,7 @@ def call_aml_endpoint(payload, url, api_key):
62
 
63
  try:
64
  logger.info(f"Sending request to {url}")
 
65
  response = urllib.request.urlopen(req)
66
  result = response.read().decode('utf-8')
67
  logger.info("Received response successfully")
@@ -72,7 +87,6 @@ def call_aml_endpoint(payload, url, api_key):
72
  error_message = error.read().decode("utf8", 'ignore')
73
  logger.error(f"Error message: {error_message}")
74
  return {"error": error_message}
75
-
76
  def improved_fetch_audio_from_url(url):
77
  """Improved function to fetch audio data from URL and convert to base64
78
  Args:
@@ -578,8 +592,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
578
  description = gr.Markdown("""
579
  This demo allows you to interact with the [Phi-4-Multimodal AI model](https://aka.ms/phi-4-multimodal/techreport).
580
  You can type messages, upload images, or record audio to communicate with the AI.
581
- Other demos include [Thoughts Organizer](https://microsoft-thoughtsorganizer.hf.space/),
582
- [Stories Come Alive](https://microsoft-storiescomealive.hf.space/), [Phine Speech Translator](https://microsoft-phinespeechtranslator.hf.space/), and [Phi-4-Mini Playground](https://microsoft-phi-4-mini.hf.space/)
583
  """)
584
 
585
  # Store the conversation state with base64 data
 
21
  api_key = os.getenv("AZURE_API_KEY")
22
 
23
 
24
+ # Default parameter values
25
+ default_max_tokens = 4096
26
+ default_temperature = 0.0
27
+ default_top_p = 1.0
28
+ default_presence_penalty = 0.0
29
+ default_frequency_penalty = 0.0
30
+
31
  # Initialize MIME types
32
  mimetypes.init()
33
 
34
+ def call_aml_endpoint(payload, url, api_key, params=None):
35
  """Call Azure ML endpoint with the given payload."""
36
  # Allow self-signed HTTPS certificates
37
  def allow_self_signed_https(allowed):
 
40
 
41
  allow_self_signed_https(True)
42
 
43
+ # Set parameters from the UI inputs or use defaults
44
+ if params is None:
45
+ params = {
46
+ "max_tokens": default_max_tokens,
47
+ "temperature": default_temperature,
48
+ "top_p": default_top_p,
49
+ "presence_penalty": default_presence_penalty,
50
+ "frequency_penalty": default_frequency_penalty
51
+ }
52
 
53
+ parameters = {
54
+ "max_tokens": int(params["max_tokens"]),
55
+ "temperature": float(params["temperature"]),
56
+ "top_p": float(params["top_p"]),
57
+ "presence_penalty": float(params["presence_penalty"]),
58
+ "frequency_penalty": float(params["frequency_penalty"]),
59
+ "stream": True
60
+ }
61
 
62
  if "parameters" not in payload["input_data"]:
63
  payload["input_data"]["parameters"] = parameters
 
76
 
77
  try:
78
  logger.info(f"Sending request to {url}")
79
+ logger.info(f"Using parameters: {parameters}")
80
  response = urllib.request.urlopen(req)
81
  result = response.read().decode('utf-8')
82
  logger.info("Received response successfully")
 
87
  error_message = error.read().decode("utf8", 'ignore')
88
  logger.error(f"Error message: {error_message}")
89
  return {"error": error_message}
 
90
  def improved_fetch_audio_from_url(url):
91
  """Improved function to fetch audio data from URL and convert to base64
92
  Args:
 
592
  description = gr.Markdown("""
593
  This demo allows you to interact with the [Phi-4-Multimodal AI model](https://aka.ms/phi-4-multimodal/techreport).
594
  You can type messages, upload images, or record audio to communicate with the AI.
595
+ Other demos include [Phi-4-Mini playground](https://huggingface.co/spaces/microsoft/phi-4-mini), [Thoughts Organizer](https://huggingface.co/spaces/microsoft/ThoughtsOrganizer),
596
+ [Stories Come Alive](https://huggingface.co/spaces/microsoft/StoriesComeAlive), [Phine Speech Translator](https://huggingface.co/spaces/microsoft/PhineSpeechTranslator)
597
  """)
598
 
599
  # Store the conversation state with base64 data