ambrosfitz commited on
Commit
51ad36a
·
verified ·
1 Parent(s): 2b8a170

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -5
app.py CHANGED
@@ -4,14 +4,14 @@ import os
4
  import time
5
 
6
  # Initialize the OpenAI Client with your API key and endpoint
7
- api_key = os.environ.get("RUNPOD_API_KEY") # Ensure your API key is correctly loaded from environment variables
8
  client = OpenAI(
9
  api_key=api_key,
10
  base_url="https://api.runpod.ai/v2/vllm-k0g4c60zor9xuu/openai/v1",
11
  )
12
 
13
  def get_response(user_message, history):
14
- # Format the history for OpenAI
15
  history_openai_format = []
16
  for human, assistant in history:
17
  if human:
@@ -28,9 +28,9 @@ def get_response(user_message, history):
28
  max_tokens=150
29
  )
30
 
31
- # Properly access the text response
32
  if response.choices:
33
- bot_message = response.choices[0].text.strip() if response.choices[0].text.strip() else "No response generated."
34
  else:
35
  bot_message = "No response generated."
36
  return bot_message
@@ -41,7 +41,7 @@ with gr.Blocks() as demo:
41
  clear = gr.Button("Clear")
42
 
43
  def user(user_message, history):
44
- if not user_message.strip(): # Handle empty input gracefully
45
  return "", history
46
  bot_response = get_response(user_message, history)
47
  return "", history + [[user_message, bot_response]]
 
4
  import time
5
 
6
  # Initialize the OpenAI Client with your API key and endpoint
7
+ api_key = os.environ.get("RUNPOD_API_KEY") # Make sure your API key is correctly configured
8
  client = OpenAI(
9
  api_key=api_key,
10
  base_url="https://api.runpod.ai/v2/vllm-k0g4c60zor9xuu/openai/v1",
11
  )
12
 
13
  def get_response(user_message, history):
14
+ # Format the history for the OpenAI call
15
  history_openai_format = []
16
  for human, assistant in history:
17
  if human:
 
28
  max_tokens=150
29
  )
30
 
31
+ # Access the text response
32
  if response.choices:
33
+ bot_message = response.choices[0].message['content'].strip() if response.choices[0].message['content'].strip() else "No response generated."
34
  else:
35
  bot_message = "No response generated."
36
  return bot_message
 
41
  clear = gr.Button("Clear")
42
 
43
  def user(user_message, history):
44
+ if not user_message.strip():
45
  return "", history
46
  bot_response = get_response(user_message, history)
47
  return "", history + [[user_message, bot_response]]