PaulNdrei commited on
Commit
73c5116
1 Parent(s): db29093

Inference error handling

Browse files
Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -4,12 +4,20 @@ import gradio as gr
4
  from gradio.components import Textbox, Button, Slider, Checkbox
5
  from AinaTheme import theme
6
  from huggingface_hub import InferenceClient
 
7
 
8
  load_dotenv()
9
 
10
  def generate(prompt, model_parameters):
11
- output = client.text_generation(prompt, **model_parameters, return_full_text=True)
12
- return output
 
 
 
 
 
 
 
13
 
14
 
15
  client = InferenceClient(
@@ -38,13 +46,13 @@ def submit_input(input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sa
38
 
39
  output = generate(input_, model_parameters)
40
 
41
- if output is None:
42
- gr.Warning('Inference endpoint is not available right now. Please try again later.')
43
  return output
44
 
45
  def change_interactive(text):
46
  if len(text.strip()) > MAX_INPUT_CHARACTERS:
47
  return gr.update(interactive = True), gr.update(interactive = False)
 
 
48
  return gr.update(interactive = True), gr.update(interactive = True)
49
 
50
  def clear():
@@ -145,6 +153,7 @@ def gradio_app():
145
  submit_btn = Button(
146
  "Submit",
147
  variant="primary",
 
148
  )
149
 
150
  with gr.Row():
 
4
  from gradio.components import Textbox, Button, Slider, Checkbox
5
  from AinaTheme import theme
6
  from huggingface_hub import InferenceClient
7
+ from urllib.error import HTTPError
8
 
9
  load_dotenv()
10
 
11
  def generate(prompt, model_parameters):
12
+
13
+ try:
14
+ output = client.text_generation(prompt, **model_parameters, return_full_text=True)
15
+ return output
16
+ except HTTPError as err:
17
+ if err.code == 400:
18
+ gr.Warning("The inference endpoint is only available Monday through Friday, from 08:00 to 20:00 CET.")
19
+ except:
20
+ gr.Warning('Inference endpoint is not available right now. Please try again later.')
21
 
22
 
23
  client = InferenceClient(
 
46
 
47
  output = generate(input_, model_parameters)
48
 
 
 
49
  return output
50
 
51
  def change_interactive(text):
52
  if len(text.strip()) > MAX_INPUT_CHARACTERS:
53
  return gr.update(interactive = True), gr.update(interactive = False)
54
+ if (len(text) == 0):
55
+ return gr.update(interactive = True), gr.update(interactive = False)
56
  return gr.update(interactive = True), gr.update(interactive = True)
57
 
58
  def clear():
 
153
  submit_btn = Button(
154
  "Submit",
155
  variant="primary",
156
+ interactive=False
157
  )
158
 
159
  with gr.Row():