macadeliccc commited on
Commit
87956ea
·
1 Parent(s): 4ea1c4e
Files changed (1) hide show
  1. app.py +16 -3
app.py CHANGED
@@ -8,7 +8,7 @@ import asyncio
8
 
9
  # Function to start the ochat server
10
  @spaces.GPU
11
- async def start_ochat_server():
12
  print(f"Is CUDA available: {torch.cuda.is_available()}")
13
  print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
14
 
@@ -28,17 +28,30 @@ start_ochat_server()
28
 
29
 
30
 
 
 
 
 
 
 
 
 
31
  # Function to send a message to the ochat server and get a response
32
  async def chat_with_ochat(message):
33
- url = "http://localhost:18888/v1/chat/completions"
 
34
  headers = {"Content-Type": "application/json"}
35
  data = {
36
  "model": "openchat_3.5",
37
  "messages": [{"role": "user", "content": message}]
38
  }
39
 
 
 
 
 
40
  try:
41
- response = requests.post(url, json=data, headers=headers)
42
  if response.status_code == 200:
43
  return response.json()['choices'][0]['message']['content']
44
  else:
 
8
 
9
  # Function to start the ochat server
10
  @spaces.GPU
11
+ def start_ochat_server():
12
  print(f"Is CUDA available: {torch.cuda.is_available()}")
13
  print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
14
 
 
28
 
29
 
30
 
31
+ # Function to check if the server is up
32
+ def is_server_up(url):
33
+ try:
34
+ response = requests.get(url)
35
+ return response.status_code == 200
36
+ except requests.RequestException:
37
+ return False
38
+
39
  # Function to send a message to the ochat server and get a response
40
  async def chat_with_ochat(message):
41
+ base_url = "http://localhost:18888"
42
+ chat_url = f"{base_url}/v1/chat/completions"
43
  headers = {"Content-Type": "application/json"}
44
  data = {
45
  "model": "openchat_3.5",
46
  "messages": [{"role": "user", "content": message}]
47
  }
48
 
49
+ # Check if server is up
50
+ if not is_server_up(base_url):
51
+ return "Error: oChat server is not running."
52
+
53
  try:
54
+ response = requests.post(chat_url, json=data, headers=headers)
55
  if response.status_code == 200:
56
  return response.json()['choices'][0]['message']['content']
57
  else: