Florent Brosse commited on
Commit
e2123b4
1 Parent(s): c460739

use custom model

Browse files
Files changed (1) hide show
  1. app.py +15 -25
app.py CHANGED
@@ -1,39 +1,38 @@
 
1
  import gradio as gr
2
  import requests
3
  import os
4
 
5
 
6
- def respond(message, history, endpoint, token, temp, max_token=0):
 
7
  if len(message.strip()) == 0:
8
  return "ERROR the question should not be empty"
9
 
10
- if len(token.strip()) > 0 and len(endpoint.strip()) > 0:
11
- local_token = token
12
- local_endpoint = endpoint
13
- custom_message = "[*] "
14
- else:
15
- local_token = os.environ['API_TOKEN']
16
- local_endpoint = os.environ['API_ENDPOINT']
17
- custom_message = ""
18
 
19
  # Add your API token to the headers
20
  headers = {
21
  'Content-Type': 'application/json',
22
  'Authorization': f'Bearer {local_token}'
23
  }
24
- q = {"inputs": {"prompt": ["Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n" +
25
- message + "\n\n### Response:\n"], "max_tokens": [int(max_token)], "temperature": [int(temp)]}}
26
- #print(q)
 
27
  try:
28
  response = requests.post(local_endpoint, json=q, headers=headers, timeout=100)
29
  response_data = response.json(
30
- )["predictions"][0]["candidates"][0]["text"]
31
  except:
32
  response_data = "ERROR status_code:" + \
33
  str(response.status_code) + " response:" + response.text
34
 
35
  #print(response.json())
36
- return custom_message + response_data
37
 
38
 
39
  demo = gr.ChatInterface(
@@ -42,22 +41,13 @@ demo = gr.ChatInterface(
42
  textbox=gr.Textbox(placeholder="Ask me a question",
43
  container=False, scale=7),
44
  title="Chat with a Databricks LLM serving endpoint",
45
- description="This a MPT-7b model",
46
  examples=[["Hello"], ["What is MLflow?"], ["What is Apache Spark?"]],
47
  cache_examples=False,
48
  theme="soft",
49
  retry_btn=None,
50
  undo_btn=None,
51
- clear_btn="Clear",
52
- additional_inputs=[
53
- gr.Textbox(label="Custom Endpoint", type="text",
54
- placeholder="https://XXXXXX.cloud.databricks.com/serving-endpoints/XXXXX/invocations"),
55
- gr.Textbox(label="Custom Token", type="password",
56
- placeholder="dapiXXXXXXXXXX"),
57
- gr.Slider(0, 100, label="Temp", value=0),
58
- gr.Slider(1, 300, label="Max token", value=75)
59
- ],
60
- additional_inputs_accordion_name="Settings"
61
  )
62
 
63
  if __name__ == "__main__":
 
1
+ import itertools
2
  import gradio as gr
3
  import requests
4
  import os
5
 
6
 
7
+ def respond(message, history):
8
+
9
  if len(message.strip()) == 0:
10
  return "ERROR the question should not be empty"
11
 
12
+
13
+ local_token = os.environ['API_TOKEN']
14
+ local_endpoint = os.environ['API_ENDPOINT']
15
+
 
 
 
 
16
 
17
  # Add your API token to the headers
18
  headers = {
19
  'Content-Type': 'application/json',
20
  'Authorization': f'Bearer {local_token}'
21
  }
22
+
23
+ prompt = list(itertools.chain.from_iterable(history))
24
+ prompt.append(message)
25
+ q = {"inputs": [prompt]}
26
  try:
27
  response = requests.post(local_endpoint, json=q, headers=headers, timeout=100)
28
  response_data = response.json(
29
+ )["predictions"]
30
  except:
31
  response_data = "ERROR status_code:" + \
32
  str(response.status_code) + " response:" + response.text
33
 
34
  #print(response.json())
35
+ return response_data
36
 
37
 
38
  demo = gr.ChatInterface(
 
41
  textbox=gr.Textbox(placeholder="Ask me a question",
42
  container=False, scale=7),
43
  title="Chat with a Databricks LLM serving endpoint",
44
+ description="This a advanced model hosted on Databricks Serving",
45
  examples=[["Hello"], ["What is MLflow?"], ["What is Apache Spark?"]],
46
  cache_examples=False,
47
  theme="soft",
48
  retry_btn=None,
49
  undo_btn=None,
50
+ clear_btn="Clear"
 
 
 
 
 
 
 
 
 
51
  )
52
 
53
  if __name__ == "__main__":