import json import gradio as gr from dotenv import load_dotenv from llm_api.exceptions import RefusalError # from llm_api.langchain_api import extract_info, follow_structure from llm_api.native_api import extract_info, follow_structure from openai import BadRequestError load_dotenv('.env', override=True) def extract_free_fn(gallery, provider, schema): img_paths = [item[0] for item in gallery] # write schema to file before calling extract_free_properties try: if schema is not None and isinstance(schema, str) and 'import' not in schema.replace("'import'", '').replace("'important'", ''): with open('schema_free.py', 'r') as f: default_schema = f.read() import_part = default_schema.split('#####')[0] print('Executing freestyle schema...') full_schema = import_part + schema with open('read_schema_free.py', 'w') as f: f.write(full_schema) exec(import_part + schema, globals()) print('Executed freestyle schema') else: print('SCHEMA ELSE') raise Exception('Invalid schema') except Exception as e: print(e) return 'Invalid schema! Please check the schema or Reload this page to restore default settings', {}, [] try: free_attributes = extract_info(img_paths, provider, Garment) free_attributes = free_attributes.model_dump_json() except RefusalError as e: return e.message, {}, [] except BadRequestError as e: return e.message, {}, [] return f'OK - provider: {provider}', free_attributes, [1] * 100 def follow_schema_fn(free_attributes, provider, probs, schema): try: if schema is not None and isinstance(schema, str) and 'import' not in schema.replace("'import'", '').replace("'important'", ''): with open('schema_structured.py', 'r') as f: default_schema = f.read() import_part = default_schema.split('#####')[0] print('executing structured schema\n') full_schema = import_part + schema with open('read_schema_structured.py', 'w') as f: f.write(full_schema) exec(full_schema, globals()) print('executed structured schema') else: print('SCHEMA ELSE') raise Exception('Invalid schema') except: return 'Invalid schema! Please check the schema or Reload this page to restore default settings', {} try: schema_attributes = follow_structure(free_attributes, provider, Garment) schema_attributes = schema_attributes.model_dump_json() except RefusalError as e: return e.message, {} except BadRequestError as e: return e.message, {} return f'OK - provider: {provider}', schema_attributes with gr.Blocks(title='Internal Demo for Attribution') as demo: with gr.Row(): with gr.Column(scale=12): gr.Markdown('''