fishytorts commited on
Commit
7ff2d8b
β€’
1 Parent(s): cdd1cf3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -101
app.py CHANGED
@@ -40,15 +40,11 @@ def encode_image(image_files):
40
 
41
  st.set_page_config(page_title="Document/Image AI Analyst", layout="centered", initial_sidebar_state="collapsed")
42
  # Streamlit page setup
43
- st.title("Document/Image AI Analyst: `GPT-4 with Vision` πŸ‘€")
44
  st.write("")
45
  st.write("")
46
  st.write("")
47
 
48
- # Retrieve the OpenAI API Key from secrets
49
- #load_dotenv()
50
- # api_key = os.getenv("OPENAI_API_KEY")t
51
-
52
  # Guide
53
  st.subheader('What can it do?')
54
  with st.expander('Read more details', expanded=False):
@@ -81,98 +77,39 @@ st.subheader('2. Upload Images: ')
81
  uploaded_files = st.file_uploader("", help='Up to five images only.', type=["jpg", "png", "jpeg"], accept_multiple_files=True)
82
 
83
 
84
- #############
85
-
86
- # loader = TextLoader("./kb_document_requirements_v1.txt", encoding="utf-8")
87
- # documents = loader.load()
88
-
89
- # text_splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=0) #you reduced chunk_size from 1000
90
- # texts = text_splitter.split_documents(documents)
91
- # for i, text in enumerate(texts):
92
- # text.metadata["source"] = f"{i+1}-page"
93
- # embeddings = OpenAIEmbeddings(api_key=api_key)
94
- # docsearch = Chroma.from_documents(texts, embeddings)
95
-
96
- # llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613", api_key=api_key)
97
- # qa_chain = create_qa_with_sources_chain(llm)
98
-
99
- # doc_prompt = PromptTemplate(
100
- # template="Content: {page_content}\nSource: {source}",
101
- # input_variables=["page_content", "source"],
102
- # )
103
-
104
- # final_qa_chain = StuffDocumentsChain(
105
- # llm_chain=qa_chain,
106
- # document_variable_name="context",
107
- # document_prompt=doc_prompt,
108
- # )
109
-
110
- # retrieval_qa = RetrievalQA(
111
- # retriever=docsearch.as_retriever(), combine_documents_chain=final_qa_chain
112
- # )
113
- #############
114
-
115
- # Retrieve the OpenAI API Key from secrets
116
- # load_dotenv()
117
- # api_key = os.getenv("OPENAI_API_KEY")
118
-
119
- # docs = [
120
- # Document(
121
- # page_content="Rules and requirements of IELTS",
122
- # metadata={"type": "english proficiency certification", "name": "ielts", "filename": "./docs/ielts_kb_doc.txt"},
123
- # ),
124
- # Document(
125
- # page_content="Rules and requirements of IELTS UKVI Academic",
126
- # metadata={"type": "english proficiency certification", "name": "ielts ukvi", "filename": "./docs/pearson_pte_kb_doc.txt"},
127
- # ),
128
- # Document(
129
- # page_content="Rules and requirements of TOEFL iBT (Test of English as a Foreign Language internet-based test)",
130
- # metadata={"type": "english proficiency certification", "name": "toefl", "filename": "./docs/toefl_kb_doc.txt"},
131
- # ),
132
- # Document(
133
- # page_content="Rules and requirements of Pearson PTE Academic",
134
- # metadata={"type": "english proficiency certification", "name": "pearson pte academic", "filename": "./docs/ielts_kb_doc.txt"},
135
- # ),
136
- # Document(
137
- # page_content="Requirements of international passport",
138
- # metadata={"type": "passport", "name": "international passport", "filename": "./docs/passport_kb_doc.txt"},
139
- # ),
140
- # ]
141
-
142
- # docsearch = Chroma.from_documents(docs, OpenAIEmbeddings(), persist_directory="./knowledge_base")
143
-
144
- # load from disk
145
- docsearch = Chroma(persist_directory="./knowledge_base", embedding_function=OpenAIEmbeddings())
146
-
147
- metadata_field_info = [
148
- AttributeInfo(
149
- name="type",
150
- description="The type of document",
151
- type="string",
152
- ),
153
- AttributeInfo(
154
- name="name",
155
- description="The name of the document",
156
- type="string",
157
- ),
158
- AttributeInfo(
159
- name="filename",
160
- description="The file name of the document",
161
- type="string",
162
- ),
163
- ]
164
-
165
- document_content_description = "Type of a document"
166
- llm = ChatOpenAI(temperature=0)
167
-
168
- retriever = SelfQueryRetriever.from_llm(
169
- llm,
170
- docsearch,
171
- document_content_description,
172
- metadata_field_info,
173
- enable_limit=True,
174
- search_kwargs={"k": 1}
175
- )
176
 
177
  # This sample for querying
178
  # results = retriever.invoke("requirements in passport")
@@ -400,11 +337,10 @@ if uploaded_files is not None and api_key and analyze_button:
400
  message_placeholder.markdown("**Identified Documents:**\n\n" + markdown_string)
401
 
402
  ### TO DO : The ABOVE code can be in a function or object.
403
- # Initialize an empty array to store the results
404
  retrieved_knowledge = []
405
 
406
  # Iterate over each item in the full_response dictionary
407
- #for filename, details in full_response_json.items():
408
  st.subheader("5. Analysis Results")
409
  for i, (filename, details) in enumerate(full_response_json.items()):
410
 
@@ -472,10 +408,12 @@ if uploaded_files is not None and api_key and analyze_button:
472
  # Now you can work with file_content
473
  #print(file_content)
474
  # add data to array
475
- #retrieved_knowledge.append(validation_response)
476
 
477
  validation_placeholder.markdown(validation_response)
478
-
 
 
479
 
480
  # Display the response in the app
481
  # st.write(response.choices[0].message.content)
 
40
 
41
  st.set_page_config(page_title="Document/Image AI Analyst", layout="centered", initial_sidebar_state="collapsed")
42
  # Streamlit page setup
43
+ st.title("Document/Image AI Analyst: `GPT-4 with Vision v2` πŸ‘€")
44
  st.write("")
45
  st.write("")
46
  st.write("")
47
 
 
 
 
 
48
  # Guide
49
  st.subheader('What can it do?')
50
  with st.expander('Read more details', expanded=False):
 
77
  uploaded_files = st.file_uploader("", help='Up to five images only.', type=["jpg", "png", "jpeg"], accept_multiple_files=True)
78
 
79
 
80
+ if api_key:
81
+ # load from disk
82
+ docsearch = Chroma(persist_directory="./knowledge_base", embedding_function=OpenAIEmbeddings())
83
+
84
+ metadata_field_info = [
85
+ AttributeInfo(
86
+ name="type",
87
+ description="The type of document",
88
+ type="string",
89
+ ),
90
+ AttributeInfo(
91
+ name="name",
92
+ description="The name of the document",
93
+ type="string",
94
+ ),
95
+ AttributeInfo(
96
+ name="filename",
97
+ description="The file name of the document",
98
+ type="string",
99
+ ),
100
+ ]
101
+
102
+ document_content_description = "Type of a document"
103
+ llm = ChatOpenAI(temperature=0)
104
+
105
+ retriever = SelfQueryRetriever.from_llm(
106
+ llm,
107
+ docsearch,
108
+ document_content_description,
109
+ metadata_field_info,
110
+ enable_limit=True,
111
+ search_kwargs={"k": 1}
112
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
  # This sample for querying
115
  # results = retriever.invoke("requirements in passport")
 
337
  message_placeholder.markdown("**Identified Documents:**\n\n" + markdown_string)
338
 
339
  ### TO DO : The ABOVE code can be in a function or object.
340
+ # Initialize an empty array to store the full prompt as log
341
  retrieved_knowledge = []
342
 
343
  # Iterate over each item in the full_response dictionary
 
344
  st.subheader("5. Analysis Results")
345
  for i, (filename, details) in enumerate(full_response_json.items()):
346
 
 
408
  # Now you can work with file_content
409
  #print(file_content)
410
  # add data to array
411
+ retrieved_knowledge.extend([prompt_text_validation, messages])
412
 
413
  validation_placeholder.markdown(validation_response)
414
+
415
+ with st.expander("Process Logs: "):
416
+ st.json(retrieved_knowledge)
417
 
418
  # Display the response in the app
419
  # st.write(response.choices[0].message.content)