import json import gradio as gr from dotenv import load_dotenv from openai import BadRequestError 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 load_dotenv(".env", override=True) garment_categories = [ "Upper Garment", "Lower Garment", "Hat", "Luggage", "Handbag", "Makeup", "Chair", "Carpet", "Watch", "Cookware", "Tableware", "Sleepware", "BathroomItem", ] schema_categories = [ "UpperGarmentProperties", "LowerGarmentProperties", "HatProperties", "LuggageProperties", "HandbagProperties", "MakeupProperties", "ChairProperties", "CarpetProperties", "WatchProperties", "CookwareProperties", "TablewareProperties", "SleepwareProperties", "BathroomItemProperties", ] def extract_free_fn(gallery, category, provider, schema, known_data): img_paths = [item[0] for item in gallery] category_schema = schema_categories[garment_categories.index(category)] # 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 # dynamically change the last line of the schema to include the category schema if category_schema in schema_categories: print("Dynamic schema activated") schema_lines = full_schema.split("\n") schema_lines[-1] = f" properties: {category_schema}" full_schema = "\n".join(schema_lines) with open("read_schema_free.py", "w") as f: f.write(full_schema) exec(full_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: status_code, free_attributes = extract_info(img_paths, provider, Garment, known_data=known_data) free_attributes = free_attributes.model_dump_json() except RefusalError as e: return e.message, {}, [] except BadRequestError as e: return e.message, {}, [] return f"status: {status_code} - provider: {provider}", free_attributes, [1] * 100 def follow_schema_fn(free_attributes, provider, probs, schema, known_data): 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: status_code, schema_attributes = follow_structure( free_attributes, provider, Garment, known_data=known_data ) schema_attributes = schema_attributes.model_dump_json() except RefusalError as e: return e.message, {} except BadRequestError as e: return e.message, {} return f"status {status_code} - provider: {provider}", schema_attributes with gr.Blocks(title="Internal Demo for Attribution") as demo: with gr.Row(): with gr.Column(scale=12): gr.Markdown( """