thanhnt-cf commited on
Commit
ed3afa6
·
1 Parent(s): c3abb2a

show error on UI if it happens

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -5,13 +5,15 @@ from llm_api.exceptions import RefusalError
5
  # from llm_api.langchain_api import extract_info, follow_structure
6
  from llm_api.native_api import extract_info, follow_structure
7
  from openai import BadRequestError
 
8
 
9
  # load_dotenv('.env', override=True)
10
 
11
  def extract_free_fn(gallery, provider, schema):
12
- img_paths = [item[0] for item in gallery]
13
  # write schema to file before calling extract_free_properties
14
  try:
 
 
15
  if schema is not None and isinstance(schema, str) and 'import' not in schema.replace("'import'", '').replace("'important'", ''):
16
  with open('schema_free.py', 'r') as f:
17
  default_schema = f.read()
@@ -26,21 +28,23 @@ def extract_free_fn(gallery, provider, schema):
26
  print('SCHEMA ELSE')
27
  raise Exception('Invalid schema')
28
  except Exception as e:
29
- print(e)
30
- return 'Invalid schema! Please check the schema or Reload this page to restore default settings', {}, []
31
 
32
  try:
33
  free_attributes = extract_info(img_paths, provider, Garment)
34
  free_attributes = free_attributes.model_dump_json()
35
  except RefusalError as e:
36
- return e.message, {}, []
37
  except BadRequestError as e:
38
- return e.message, {}, []
39
-
 
 
40
  return f'OK - provider: {provider}', free_attributes, [1] * 100
41
 
42
  def follow_schema_fn(free_attributes, provider, probs, schema):
43
  try:
 
44
  if schema is not None and isinstance(schema, str) and 'import' not in schema.replace("'import'", '').replace("'important'", ''):
45
  with open('schema_structured.py', 'r') as f:
46
  default_schema = f.read()
@@ -54,17 +58,20 @@ def follow_schema_fn(free_attributes, provider, probs, schema):
54
  else:
55
  print('SCHEMA ELSE')
56
  raise Exception('Invalid schema')
57
- except:
58
- return 'Invalid schema! Please check the schema or Reload this page to restore default settings', {}
59
 
60
  try:
61
  schema_attributes = follow_structure(free_attributes, provider, Garment)
62
  schema_attributes = schema_attributes.model_dump_json()
63
  except RefusalError as e:
64
- return e.message, {}
65
  except BadRequestError as e:
66
- return e.message, {}
 
 
67
 
 
68
  return f'OK - provider: {provider}', schema_attributes
69
 
70
  with gr.Blocks(title='Internal Demo for Attribution') as demo:
 
5
  # from llm_api.langchain_api import extract_info, follow_structure
6
  from llm_api.native_api import extract_info, follow_structure
7
  from openai import BadRequestError
8
+ # from utils import log_input, log_output
9
 
10
  # load_dotenv('.env', override=True)
11
 
12
  def extract_free_fn(gallery, provider, schema):
 
13
  # write schema to file before calling extract_free_properties
14
  try:
15
+ img_paths = [item[0] for item in gallery]
16
+ # request_id = log_input('extract', img_paths, provider, schema)
17
  if schema is not None and isinstance(schema, str) and 'import' not in schema.replace("'import'", '').replace("'important'", ''):
18
  with open('schema_free.py', 'r') as f:
19
  default_schema = f.read()
 
28
  print('SCHEMA ELSE')
29
  raise Exception('Invalid schema')
30
  except Exception as e:
31
+ return f"ERROR: {e}", {}, []
 
32
 
33
  try:
34
  free_attributes = extract_info(img_paths, provider, Garment)
35
  free_attributes = free_attributes.model_dump_json()
36
  except RefusalError as e:
37
+ return f"ERROR: {e}", {}, []
38
  except BadRequestError as e:
39
+ return f"ERROR: {e}", {}, []
40
+ except Exception as e:
41
+ return f"ERROR: {e}", {}, []
42
+ # log_output(request_id, str(free_attributes))
43
  return f'OK - provider: {provider}', free_attributes, [1] * 100
44
 
45
  def follow_schema_fn(free_attributes, provider, probs, schema):
46
  try:
47
+ # request_id = log_input('follow', None, provider, schema)
48
  if schema is not None and isinstance(schema, str) and 'import' not in schema.replace("'import'", '').replace("'important'", ''):
49
  with open('schema_structured.py', 'r') as f:
50
  default_schema = f.read()
 
58
  else:
59
  print('SCHEMA ELSE')
60
  raise Exception('Invalid schema')
61
+ except Exception as e:
62
+ return f"ERROR: {e}", {}
63
 
64
  try:
65
  schema_attributes = follow_structure(free_attributes, provider, Garment)
66
  schema_attributes = schema_attributes.model_dump_json()
67
  except RefusalError as e:
68
+ return f"ERROR: {e}", {}
69
  except BadRequestError as e:
70
+ return f"ERROR: {e}", {}
71
+ except Exception as e:
72
+ return f"ERROR: {e}", {}
73
 
74
+ # log_output(request_id, str(schema_attributes))
75
  return f'OK - provider: {provider}', schema_attributes
76
 
77
  with gr.Blocks(title='Internal Demo for Attribution') as demo: