David commited on
Commit
f7d8d0c
1 Parent(s): fcabc39
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -14,6 +14,8 @@ headers = {
14
  def respond(message, history, system_message, max_tokens, temperature, top_p):
15
  if system_message is not None:
16
  messages = [{"role": "system", "content": system_message}]
 
 
17
 
18
  for human, assistant in history:
19
  messages.append({"role": "user", "content": human})
@@ -29,12 +31,25 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
29
  "top_p": top_p
30
  }
31
 
32
- response = requests.post(API_URL, headers=headers, json=data)
 
 
 
 
 
 
 
 
 
33
 
34
- if response.status_code == 200:
35
- return response.json()['choices'][0]['message']['content']
36
- else:
37
- return f"Error: {response.status_code} - {response.text}"
 
 
 
 
38
 
39
  demo = gr.ChatInterface(
40
  respond,
 
14
  def respond(message, history, system_message, max_tokens, temperature, top_p):
15
  if system_message is not None:
16
  messages = [{"role": "system", "content": system_message}]
17
+ else:
18
+ messages = []
19
 
20
  for human, assistant in history:
21
  messages.append({"role": "user", "content": human})
 
31
  "top_p": top_p
32
  }
33
 
34
+ try:
35
+ response = requests.post(API_URL, headers=headers, json=data)
36
+ response.raise_for_status() # Raises an HTTPError for bad responses
37
+
38
+ response_json = response.json()
39
+
40
+ if 'choices' in response_json and len(response_json['choices']) > 0:
41
+ return response_json['choices'][0]['message']['content']
42
+ else:
43
+ return f"Error: Unexpected response format. Full response: {response_json}"
44
 
45
+ except requests.exceptions.RequestException as e:
46
+ return f"Error: {str(e)}"
47
+ except ValueError as e:
48
+ return f"Error: Invalid JSON response. {str(e)}"
49
+ except KeyError as e:
50
+ return f"Error: Unexpected response structure. Missing key: {str(e)}"
51
+ except Exception as e:
52
+ return f"Unexpected error: {str(e)}"
53
 
54
  demo = gr.ChatInterface(
55
  respond,