code: from dotenv import load_dotenv
  import cv2
  import numpy as np
  import os
  import streamlit as st
  from llama_index import SimpleDirectoryReader
  from pydantic_llm import (
      pydantic_llm,
      DamagedParts,
      damages_initial_prompt_str,
      ConditionsReport,
      conditions_report_initial_prompt_str,
  )
  import pandas as pd
  from llama_index.multi_modal_llms.openai import OpenAIMultiModal
  from car_colorizer import process_car_parts
  import requests
  from io import BytesIO
  from streamlit_modal import Modal
  import streamlit.components.v1 as components
  
  modal = Modal("Damage Report", key="demo", max_width=1280)
  
  api_url = "https://dmg-decoder.up.railway.app"
  
  
  def create_report(data={"test": "123"}):
      url = f"{api_url}/api/create_report"
      response = requests.post(
          url, json=data, headers={"Content-Type": "application/json"}
      )
      json = response.json()
      print(json)
      return json["id"]
  
  
  load_dotenv()
  
  
  states_names = ["front_image", "back_image", "left_image", "right_image", "report_id"]
  
  openai_mm_llm = OpenAIMultiModal(model="gpt-4-vision-preview")
  
  # Remove form border and padding styles
  css = r"""
      <style>
          [data-testid="stForm"] {border: 0px;padding:0px}
      </style>
  """
  st.markdown(css, unsafe_allow_html=True)
  
  
  for state_name in states_names:
      if state_name not in st.session_state:
          st.session_state[state_name] = None
  
  
  st.title("Damage Decoder")
  
  
  st.subheader("Upload your car crash pictures")
  
  
  def create_drag_and_drop(state_name, label):
      st.session_state[state_name] = st.file_uploader(
          label=label, key=f"{state_name}_image"
      )
  
      if st.session_state[state_name] is not None:
          css = f"""
              <style>
                  [aria-label="{label}"] {{display: none;}}
              </style>
          """
          st.markdown(css, unsafe_allow_html=True)
          file_bytes = np.asarray(
              bytearray(st.session_state[state_name].read()), dtype=np.uint8
          )
          opencv_image = cv2.imdecode(file_bytes, 1)
          st.image(opencv_image, channels="BGR")
  
  
  col1, col2 = st.columns(2)
  
  with col1:
      create_drag_and_drop("front_image", "Front Image")
      create_drag_and_drop("right_image", "Left Image")
  
  with col2:
      create_drag_and_drop("back_image", "Back Image")
      create_drag_and_drop("left_image", "Right Image")
  
  
  def save_image(state_name):
      path = os.path.join(os.getcwd(), "images")
      if not os.path.exists(path):
          os.makedirs(path)
  
      if st.session_state[state_name] is not None:
          with open(os.path.join(path, f"{state_name}.jpg"), "wb") as f:
              f.write(st.session_state[state_name].getbuffer())
  
  
  def delete_image(state_name):
      path = os.path.join(os.getcwd(), "images")
      if st.session_state[state_name] is not None and os.path.exists(
          os.path.join(path, f"{state_name}.jpg")
      ):
          os.remove(os.path.join(path, f"{state_name}.jpg"))
  
  
  with st.form(key="car_form"):
      selected_make = st.selectbox(
          "Select your car make",
          ("Ford", "Subaru", "BMW", "Mercedes", "Volkswagen", "Volvo"),
      )
  
      selected_model = st.selectbox(
          "Select your car model",
          ("Mustang", "Outback", "X3", "C-Class", "Golf", "XC60"),
      )
  
      selected_year = st.selectbox(
          "Select your car year",
          ("2007", "2010", "2011", "2012", "2013", "2014"),
      )
  
      selected_llm_model = st.selectbox(
          "Select LLM model",
          ("Gemini", "OpenAI"),
      )
  
      submit_button = st.form_submit_button(label="Submit")
  
  if submit_button:
      with st.spinner("Processing..."):
          for state_name in states_names:
              save_image(state_name)
          path = os.path.join(os.getcwd(), "images")
  
          image_documents = SimpleDirectoryReader(path).load_data()
  
          conditions_report_response = pydantic_llm(
              output_class=ConditionsReport,
              image_documents=image_documents,
              prompt_template_str=conditions_report_initial_prompt_str.format(
                  make_name=selected_make, model_name=selected_model, year=selected_year
              ),
              selected_llm_model=selected_llm_model,
          )
  
          for state_name in states_names:
              delete_image(state_name)
  
          request_data = []
  
          for part, condition in dict(conditions_report_response).items():
              request_data.append({"part": part, "condition": condition})
  
          id = create_report(
              data={
                  "conditions_report": request_data,
                  "car_name": f"{selected_make} {selected_model} {selected_year}",
              }
          )
  
          st.session_state["report_id"] = id
  
          car_sides = ["front", "back", "left", "right"]
          import boto3
  
          s3 = boto3.resource("s3")
  
          for side in car_sides:
              colored_side = process_car_parts(dict(conditions_report_response), side)
              in_memory_file = BytesIO()
              colored_side.save(in_memory_file, format="PNG")
              in_memory_file.seek(0)
              s3.Bucket("elastic-llm").put_object(
                  Key=f"{id}/colored_car_{side}.png",
                  Body=in_memory_file,
              )
  
          modal.open()
  
  if modal.is_open():
      with modal.container():
          st.markdown(
              f"<a href='{api_url}/report/{st.session_state['report_id']}' target='_blank'>Go to report</a>",
              unsafe_allow_html=True,
          )
  
          st.code(f"{api_url}/report/{st.session_state['report_id']}", language="python")
  
          html_string = f"""
              <div style="max-height:350px;overflow-y:auto;overflow-x:hidden">
                  <iframe style="overflow-x:hidden" src="{api_url}/report/{st.session_state['report_id']}" width="100%" height="960px"></iframe>
              </div>
          """
          components.html(html_string, height=350)
  
          # st.subheader("Summary")
          # st.write(damages_response.summary)
  
          # st.subheader("Damaged Parts")
          # df = pd.DataFrame.from_records(
          #     [part.model_dump() for part in damages_response.damaged_parts]
          # )
          # st.dataframe(df)
  
          # TODO: look for the parts in the vector store
  
          # filters = MetadataFilters(
          #     filters=[
          #         MetadataFilter(key="make", value=selected_make),
          #         MetadataFilter(key="model", value=selected_model),
          #         MetadataFilter(key="year", value=selected_year),
          #     ]
          # )
  
          # retriever = VectorStoreIndex.from_vector_store(vector_store).as_retriever(
          #     filters=filters,
          # )
  
          # query_engine = RetrieverQueryEngine(
          #     retriever=retriever,
          # )
  
apis: llama_index.multi_modal_llms.openai.OpenAIMultiModal llama_index.SimpleDirectoryReader
extract_api: [((557, 607), 'streamlit_modal.Modal', 'Modal', (['"""Damage Report"""'], {'key': '"""demo"""', 'max_width': '(1280)'}), "('Damage Report', key='demo', max_width=1280)\n", (562, 607), False, 'from streamlit_modal import Modal\n'), ((912, 925), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (923, 925), False, 'from dotenv import load_dotenv\n'), ((1032, 1078), 'llama_index.multi_modal_llms.openai.OpenAIMultiModal', 'OpenAIMultiModal', ([], {'model': '"""gpt-4-vision-preview"""'}), "(model='gpt-4-vision-preview')\n", (1048, 1078), False, 'from llama_index.multi_modal_llms.openai import OpenAIMultiModal\n'), ((1217, 1257), 'streamlit.markdown', 'st.markdown', (['css'], {'unsafe_allow_html': '(True)'}), '(css, unsafe_allow_html=True)\n', (1228, 1257), True, 'import streamlit as st\n'), ((1381, 1407), 'streamlit.title', 'st.title', (['"""Damage Decoder"""'], {}), "('Damage Decoder')\n", (1389, 1407), True, 'import streamlit as st\n'), ((1410, 1456), 'streamlit.subheader', 'st.subheader', (['"""Upload your car crash pictures"""'], {}), "('Upload your car crash pictures')\n", (1422, 1456), True, 'import streamlit as st\n'), ((2070, 2083), 'streamlit.columns', 'st.columns', (['(2)'], {}), '(2)\n', (2080, 2083), True, 'import streamlit as st\n'), ((755, 830), 'requests.post', 'requests.post', (['url'], {'json': 'data', 'headers': "{'Content-Type': 'application/json'}"}), "(url, json=data, headers={'Content-Type': 'application/json'})\n", (768, 830), False, 'import requests\n'), ((1539, 1595), 'streamlit.file_uploader', 'st.file_uploader', ([], {'label': 'label', 'key': 'f"""{state_name}_image"""'}), "(label=label, key=f'{state_name}_image')\n", (1555, 1595), True, 'import streamlit as st\n'), ((2911, 2934), 'streamlit.form', 'st.form', ([], {'key': '"""car_form"""'}), "(key='car_form')\n", (2918, 2934), True, 'import streamlit as st\n'), ((2956, 3058), 'streamlit.selectbox', 'st.selectbox', (['"""Select your car make"""', "('Ford', 'Subaru', 'BMW', 'Mercedes', 'Volkswagen', 'Volvo')"], {}), "('Select your car make', ('Ford', 'Subaru', 'BMW', 'Mercedes',\n    'Volkswagen', 'Volvo'))\n", (2968, 3058), True, 'import streamlit as st\n'), ((3100, 3198), 'streamlit.selectbox', 'st.selectbox', (['"""Select your car model"""', "('Mustang', 'Outback', 'X3', 'C-Class', 'Golf', 'XC60')"], {}), "('Select your car model', ('Mustang', 'Outback', 'X3',\n    'C-Class', 'Golf', 'XC60'))\n", (3112, 3198), True, 'import streamlit as st\n'), ((3239, 3329), 'streamlit.selectbox', 'st.selectbox', (['"""Select your car year"""', "('2007', '2010', '2011', '2012', '2013', '2014')"], {}), "('Select your car year', ('2007', '2010', '2011', '2012',\n    '2013', '2014'))\n", (3251, 3329), True, 'import streamlit as st\n'), ((3375, 3429), 'streamlit.selectbox', 'st.selectbox', (['"""Select LLM model"""', "('Gemini', 'OpenAI')"], {}), "('Select LLM model', ('Gemini', 'OpenAI'))\n", (3387, 3429), True, 'import streamlit as st\n'), ((3474, 3511), 'streamlit.form_submit_button', 'st.form_submit_button', ([], {'label': '"""Submit"""'}), "(label='Submit')\n", (3495, 3511), True, 'import streamlit as st\n'), ((1798, 1838), 'streamlit.markdown', 'st.markdown', (['css'], {'unsafe_allow_html': '(True)'}), '(css, unsafe_allow_html=True)\n', (1809, 1838), True, 'import streamlit as st\n'), ((1980, 2007), 'cv2.imdecode', 'cv2.imdecode', (['file_bytes', '(1)'], {}), '(file_bytes, 1)\n', (1992, 2007), False, 'import cv2\n'), ((2016, 2054), 'streamlit.image', 'st.image', (['opencv_image'], {'channels': '"""BGR"""'}), "(opencv_image, channels='BGR')\n", (2024, 2054), True, 'import streamlit as st\n'), ((2378, 2389), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (2387, 2389), False, 'import os\n'), ((2412, 2432), 'os.path.exists', 'os.path.exists', (['path'], {}), '(path)\n', (2426, 2432), False, 'import os\n'), ((2442, 2459), 'os.makedirs', 'os.makedirs', (['path'], {}), '(path)\n', (2453, 2459), False, 'import os\n'), ((2699, 2710), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (2708, 2710), False, 'import os\n'), ((3540, 3567), 'streamlit.spinner', 'st.spinner', (['"""Processing..."""'], {}), "('Processing...')\n", (3550, 3567), True, 'import streamlit as st\n'), ((4732, 4752), 'boto3.resource', 'boto3.resource', (['"""s3"""'], {}), "('s3')\n", (4746, 4752), False, 'import boto3\n'), ((5235, 5376), 'streamlit.markdown', 'st.markdown', (['f"""<a href=\'{api_url}/report/{st.session_state[\'report_id\']}\' target=\'_blank\'>Go to report</a>"""'], {'unsafe_allow_html': '(True)'}), '(\n    f"<a href=\'{api_url}/report/{st.session_state[\'report_id\']}\' target=\'_blank\'>Go to report</a>"\n    , unsafe_allow_html=True)\n', (5246, 5376), True, 'import streamlit as st\n'), ((5411, 5490), 'streamlit.code', 'st.code', (['f"""{api_url}/report/{st.session_state[\'report_id\']}"""'], {'language': '"""python"""'}), '(f"{api_url}/report/{st.session_state[\'report_id\']}", language=\'python\')\n', (5418, 5490), True, 'import streamlit as st\n'), ((5778, 5818), 'streamlit.components.v1.html', 'components.html', (['html_string'], {'height': '(350)'}), '(html_string, height=350)\n', (5793, 5818), True, 'import streamlit.components.v1 as components\n'), ((2798, 2837), 'os.path.join', 'os.path.join', (['path', 'f"""{state_name}.jpg"""'], {}), "(path, f'{state_name}.jpg')\n", (2810, 2837), False, 'import os\n'), ((2863, 2902), 'os.path.join', 'os.path.join', (['path', 'f"""{state_name}.jpg"""'], {}), "(path, f'{state_name}.jpg')\n", (2875, 2902), False, 'import os\n'), ((3672, 3683), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (3681, 3683), False, 'import os\n'), ((4899, 4908), 'io.BytesIO', 'BytesIO', ([], {}), '()\n', (4906, 4908), False, 'from io import BytesIO\n'), ((2528, 2567), 'os.path.join', 'os.path.join', (['path', 'f"""{state_name}.jpg"""'], {}), "(path, f'{state_name}.jpg')\n", (2540, 2567), False, 'import os\n'), ((3722, 3749), 'llama_index.SimpleDirectoryReader', 'SimpleDirectoryReader', (['path'], {}), '(path)\n', (3743, 3749), False, 'from llama_index import SimpleDirectoryReader\n'), ((3934, 4053), 'pydantic_llm.conditions_report_initial_prompt_str.format', 'conditions_report_initial_prompt_str.format', ([], {'make_name': 'selected_make', 'model_name': 'selected_model', 'year': 'selected_year'}), '(make_name=selected_make,\n    model_name=selected_model, year=selected_year)\n', (3977, 4053), False, 'from pydantic_llm import pydantic_llm, DamagedParts, damages_initial_prompt_str, ConditionsReport, conditions_report_initial_prompt_str\n')]