Pavithiran commited on
Commit
c07b25b
·
verified ·
1 Parent(s): e23e468

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -18
app.py CHANGED
@@ -1,5 +1,6 @@
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
@@ -8,12 +9,12 @@ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
 
10
  def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
  ):
18
  messages = [{"role": "system", "content": system_message}]
19
 
@@ -27,17 +28,21 @@ def respond(
27
 
28
  response = ""
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
 
 
 
 
41
 
42
  """
43
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
@@ -60,4 +65,4 @@ demo = gr.ChatInterface(
60
 
61
 
62
  if __name__ == "__main__":
63
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from typing import List, Tuple
4
 
5
  """
6
  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
 
9
 
10
 
11
  def respond(
12
+ message: str,
13
+ history: List[Tuple[str, str]],
14
+ system_message: str,
15
+ max_tokens=1024,
16
+ temperature=0.7,
17
+ top_p=0.95,
18
  ):
19
  messages = [{"role": "system", "content": system_message}]
20
 
 
28
 
29
  response = ""
30
 
31
+ try:
32
+ for message in client.chat_completion(
33
+ messages,
34
+ max_tokens=max_tokens,
35
+ stream=True,
36
+ temperature=temperature,
37
+ top_p=top_p,
38
+ ):
39
+ if message.choices and "delta" in message.choices[0]:
40
+ token = message.choices[0].delta.get("content", "")
41
+ response += token
42
+ yield response
43
+ except Exception as e:
44
+ print(f"Error during API call: {e}")
45
+ yield "An error occurred while processing the request."
46
 
47
  """
48
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
65
 
66
 
67
  if __name__ == "__main__":
68
+ demo.launch()