Seymour-Visions commited on
Commit
6b838d0
·
verified ·
1 Parent(s): 000973f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -36
app.py CHANGED
@@ -1,49 +1,47 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
 
8
  # Read the system message from the file
9
  with open("system_message.txt", "r") as file:
10
  system_message = file.read().strip()
11
 
12
- #client = InferenceClient("meta-llama/Meta-Llama-3.1-8B-Instruct")
13
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
14
 
15
-
16
- def respond(
17
- message,
18
- history: list[tuple[str, str]],
19
- #system_message,
20
- max_tokens,
21
- temperature,
22
- top_p,
23
- ):
24
  messages = [{"role": "system", "content": system_message}]
25
-
26
  for val in history:
27
  if val[0]:
28
  messages.append({"role": "user", "content": val[0]})
29
  if val[1]:
30
  messages.append({"role": "assistant", "content": val[1]})
31
-
32
  messages.append({"role": "user", "content": message})
33
-
34
  response = ""
35
-
36
- for message in client.chat_completion(
37
- messages,
38
- max_tokens=max_tokens,
39
- stream=True,
40
- temperature=temperature,
41
- top_p=top_p,
42
- ):
43
- token = message.choices[0].delta.content
44
-
45
- response += token
46
- yield response
 
 
 
 
 
 
 
47
 
48
  """
49
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
@@ -51,19 +49,13 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
51
  demo = gr.ChatInterface(
52
  respond,
53
  additional_inputs=[
54
- #gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
55
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
56
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
57
  gr.Slider(
58
- minimum=0.1,
59
- maximum=1.0,
60
- value=0.95,
61
- step=0.05,
62
- label="Top-p (nucleus sampling)",
63
  ),
64
  ],
65
  )
66
 
67
-
68
  if __name__ == "__main__":
69
  demo.launch()
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient #For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
3
+ import json
4
+ import logging
5
 
6
+ # Set up logging
7
+ logging.basicConfig(level=logging.DEBUG)
8
+ logger = logging.getLogger(__name__)
9
 
10
  # Read the system message from the file
11
  with open("system_message.txt", "r") as file:
12
  system_message = file.read().strip()
13
 
 
14
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
15
 
16
+ def respond(message, history: list[tuple[str, str]], max_tokens, temperature, top_p):
 
 
 
 
 
 
 
 
17
  messages = [{"role": "system", "content": system_message}]
 
18
  for val in history:
19
  if val[0]:
20
  messages.append({"role": "user", "content": val[0]})
21
  if val[1]:
22
  messages.append({"role": "assistant", "content": val[1]})
 
23
  messages.append({"role": "user", "content": message})
24
+
25
  response = ""
26
+ try:
27
+ for message in client.chat_completion(
28
+ messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p,
29
+ ):
30
+ try:
31
+ token = message.choices[0].delta.content
32
+ response += token
33
+ yield response
34
+ except json.JSONDecodeError as e:
35
+ logger.error(f"JSON Decode Error: {e}")
36
+ logger.debug(f"Problematic message: {message}")
37
+ # Handle the error gracefully, maybe skip this token or return an error message
38
+ continue
39
+ except Exception as e:
40
+ logger.error(f"Unexpected error processing token: {e}")
41
+ continue
42
+ except Exception as e:
43
+ logger.error(f"Error in chat completion: {e}")
44
+ yield "I'm sorry, but I encountered an error while processing your request."
45
 
46
  """
47
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
49
  demo = gr.ChatInterface(
50
  respond,
51
  additional_inputs=[
 
52
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
53
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
54
  gr.Slider(
55
+ minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)",
 
 
 
 
56
  ),
57
  ],
58
  )
59
 
 
60
  if __name__ == "__main__":
61
  demo.launch()