Spaces:
Runtime error
Runtime error
jhonparra18
commited on
Commit
β’
9a299ce
1
Parent(s):
78d87d7
updated app logic
Browse files- app.py +20 -11
- app_utils.py +6 -0
- image_processor.py +7 -2
- text_summarizer.py +14 -14
app.py
CHANGED
@@ -6,10 +6,15 @@ from app_utils import TEMP_DIR_NAME,save_uploaded_file,reset_chat
|
|
6 |
import os
|
7 |
import sys
|
8 |
from text_summarizer import agent
|
|
|
9 |
|
10 |
BOT_DEFAULT_MSG="Hello π I'm a test AI assistant to help you with your questions about an input file, or feel free to ask me anything"
|
11 |
-
st.set_page_config(page_title="Invoice|Receipt LLM Summarizer",layout='wide',page_icon=":shark:")
|
|
|
|
|
12 |
IMAGE_TMP_PATH=None
|
|
|
|
|
13 |
|
14 |
with st.sidebar:
|
15 |
|
@@ -24,16 +29,23 @@ with st.sidebar:
|
|
24 |
IMAGE_TMP_PATH=os.path.join(TEMP_DIR_NAME,input_image.name)
|
25 |
st.markdown(f"<h1 style='text-align: center;'> Image Uploaded and saved<br>",unsafe_allow_html=True)
|
26 |
st.image(Image.open(IMAGE_TMP_PATH))
|
27 |
-
|
|
|
|
|
|
|
28 |
|
|
|
|
|
29 |
st.button("Reset Chat History", type="secondary", on_click=reset_chat,use_container_width=True)
|
30 |
-
st.
|
|
|
|
|
31 |
|
32 |
|
33 |
# Initialize chat history based on streamlit doc for chat applications https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps
|
34 |
if "messages" not in st.session_state:
|
35 |
st.session_state.messages = []
|
36 |
-
|
37 |
|
38 |
|
39 |
# Display chat messages from history on app rerun
|
@@ -41,21 +53,18 @@ for message in st.session_state.messages:
|
|
41 |
with st.chat_message(message["role"]):
|
42 |
st.markdown(message["content"])
|
43 |
|
44 |
-
# Set default message on chat
|
45 |
-
with st.chat_message("assistant"):
|
46 |
-
st.write(BOT_DEFAULT_MSG)
|
47 |
-
|
48 |
|
49 |
if prompt := st.chat_input("Write a message to the AI assistant | Escribe un mensaje para el asistente de IA"):
|
50 |
-
|
51 |
st.chat_message("user").markdown(prompt)
|
52 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
53 |
-
|
|
|
54 |
##streamlit callback https://python.langchain.com/docs/integrations/callbacks/streamlit
|
55 |
st_callback = StreamlitCallbackHandler(st.container())
|
56 |
#hotfix to errors
|
57 |
try:
|
58 |
-
response =
|
59 |
except ValueError as e:
|
60 |
response = "Sorry i could't understand your last question."
|
61 |
with st.chat_message("assistant"):
|
|
|
6 |
import os
|
7 |
import sys
|
8 |
from text_summarizer import agent
|
9 |
+
from image_processor import ImageProcessor
|
10 |
|
11 |
BOT_DEFAULT_MSG="Hello π I'm a test AI assistant to help you with your questions about an input file, or feel free to ask me anything"
|
12 |
+
st.set_page_config(page_title="Invoice | Receipt LLM Summarizer",layout='wide',page_icon=":shark:")
|
13 |
+
|
14 |
+
#placeholders for temporal image path and an image processor in case we want to read img text separately
|
15 |
IMAGE_TMP_PATH=None
|
16 |
+
PROCESSOR=ImageProcessor()
|
17 |
+
img_text=""
|
18 |
|
19 |
with st.sidebar:
|
20 |
|
|
|
29 |
IMAGE_TMP_PATH=os.path.join(TEMP_DIR_NAME,input_image.name)
|
30 |
st.markdown(f"<h1 style='text-align: center;'> Image Uploaded and saved<br>",unsafe_allow_html=True)
|
31 |
st.image(Image.open(IMAGE_TMP_PATH))
|
32 |
+
st.markdown("***")
|
33 |
+
inject_text=st.checkbox(label="Inject OCR output",value=False,help="Injects text found in the image without using the agent Action (Speeds response)")
|
34 |
+
if inject_text:
|
35 |
+
img_text=PROCESSOR.run(IMAGE_TMP_PATH)
|
36 |
|
37 |
+
|
38 |
+
st.markdown("***")
|
39 |
st.button("Reset Chat History", type="secondary", on_click=reset_chat,use_container_width=True)
|
40 |
+
_,col_c,_=st.columns(3)
|
41 |
+
with col_c:
|
42 |
+
st.markdown("[![Foo](https://img.icons8.com/material-outlined/96/000000/github.png)](https://github.com/statscol/invoice-llm-summarizer)")
|
43 |
|
44 |
|
45 |
# Initialize chat history based on streamlit doc for chat applications https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps
|
46 |
if "messages" not in st.session_state:
|
47 |
st.session_state.messages = []
|
48 |
+
st.session_state.messages.append({"role": "assistant", "content": BOT_DEFAULT_MSG})
|
49 |
|
50 |
|
51 |
# Display chat messages from history on app rerun
|
|
|
53 |
with st.chat_message(message["role"]):
|
54 |
st.markdown(message["content"])
|
55 |
|
|
|
|
|
|
|
|
|
56 |
|
57 |
if prompt := st.chat_input("Write a message to the AI assistant | Escribe un mensaje para el asistente de IA"):
|
58 |
+
|
59 |
st.chat_message("user").markdown(prompt)
|
60 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
61 |
+
|
62 |
+
prompt_ad=f'{prompt}, img path: {IMAGE_TMP_PATH}' if (input_image is not None and not inject_text) else f'{prompt} text: {img_text}'
|
63 |
##streamlit callback https://python.langchain.com/docs/integrations/callbacks/streamlit
|
64 |
st_callback = StreamlitCallbackHandler(st.container())
|
65 |
#hotfix to errors
|
66 |
try:
|
67 |
+
response = agent.run(prompt_ad,callbacks=[st_callback])
|
68 |
except ValueError as e:
|
69 |
response = "Sorry i could't understand your last question."
|
70 |
with st.chat_message("assistant"):
|
app_utils.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
|
|
|
2 |
import os
|
3 |
import streamlit as st
|
4 |
|
@@ -12,3 +13,8 @@ def save_uploaded_file(uploadedfile):
|
|
12 |
|
13 |
def reset_chat():
|
14 |
st.session_state.messages = []
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
|
3 |
import os
|
4 |
import streamlit as st
|
5 |
|
|
|
13 |
|
14 |
def reset_chat():
|
15 |
st.session_state.messages = []
|
16 |
+
|
17 |
+
def read_txt_file(path_txt:str):
|
18 |
+
with open(path_txt,'r') as f:
|
19 |
+
text=" ".join(f.readlines()).strip()
|
20 |
+
return text
|
image_processor.py
CHANGED
@@ -73,13 +73,18 @@ class ImageProcessor(BaseTool):
|
|
73 |
text=pytesseract.image_to_string(img,lang=lang,config=PYTESSERACT_DEFAULT_CONFIG)
|
74 |
return text
|
75 |
|
76 |
-
def _run(self,img_path):
|
77 |
img=self.process_image(str(img_path))
|
78 |
text=self.img_to_text(img)
|
|
|
|
|
|
|
79 |
return text
|
80 |
|
81 |
# as used in langchain documentation https://python.langchain.com/docs/modules/agents/tools/custom_tools
|
82 |
-
async def _arun(self,
|
83 |
) -> str:
|
84 |
"""Use the tool asynchronously."""
|
85 |
raise NotImplementedError("custom_search does not support async")
|
|
|
|
|
|
73 |
text=pytesseract.image_to_string(img,lang=lang,config=PYTESSERACT_DEFAULT_CONFIG)
|
74 |
return text
|
75 |
|
76 |
+
def _run(self,img_path,save_to_disk=False):
|
77 |
img=self.process_image(str(img_path))
|
78 |
text=self.img_to_text(img)
|
79 |
+
if save_to_disk:
|
80 |
+
with open(f"/tmp/{str(img_pth).split('/')[-1].replace('.jpg','.txt')}",'w') as f:
|
81 |
+
f.write(text)
|
82 |
return text
|
83 |
|
84 |
# as used in langchain documentation https://python.langchain.com/docs/modules/agents/tools/custom_tools
|
85 |
+
async def _arun(self, img_path: str,save_to_disk=False, run_manager: Optional[AsyncCallbackManagerForToolRun] = None
|
86 |
) -> str:
|
87 |
"""Use the tool asynchronously."""
|
88 |
raise NotImplementedError("custom_search does not support async")
|
89 |
+
|
90 |
+
|
text_summarizer.py
CHANGED
@@ -3,7 +3,9 @@ from langchain.schema import SystemMessage
|
|
3 |
from langchain.agents import OpenAIFunctionsAgent,initialize_agent
|
4 |
from langchain.agents import AgentType
|
5 |
from langchain.chat_models import ChatOpenAI
|
6 |
-
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
|
|
|
|
7 |
from dotenv import load_dotenv
|
8 |
from config import OPEN_AI_MODEL_NAME,DEBUG_MODE_LLM
|
9 |
from image_processor import ImageProcessor
|
@@ -18,6 +20,7 @@ system_message = SystemMessage(content="""You are an expert invoice, receipt sum
|
|
18 |
|
19 |
#initial system prompt
|
20 |
prompt = OpenAIFunctionsAgent.create_prompt(system_message=system_message)
|
|
|
21 |
#define LLM to use
|
22 |
llm = ChatOpenAI(temperature=0.1, model=OPEN_AI_MODEL_NAME,)
|
23 |
|
@@ -26,28 +29,25 @@ tools = [
|
|
26 |
ImageProcessor()
|
27 |
]
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
return_messages=True
|
34 |
-
)
|
35 |
|
36 |
llm = ChatOpenAI(
|
37 |
temperature=0,
|
38 |
-
model_name=OPEN_AI_MODEL_NAME
|
39 |
-
max_tokens=2048
|
40 |
)
|
41 |
|
42 |
-
|
43 |
agent = initialize_agent(
|
44 |
-
agent=AgentType.OPENAI_FUNCTIONS,
|
45 |
tools=tools,
|
46 |
llm=llm,
|
47 |
-
max_iterations=
|
48 |
verbose=False,
|
49 |
memory=conversational_memory,
|
50 |
-
|
51 |
prompt=prompt
|
52 |
)
|
53 |
-
|
|
|
|
3 |
from langchain.agents import OpenAIFunctionsAgent,initialize_agent
|
4 |
from langchain.agents import AgentType
|
5 |
from langchain.chat_models import ChatOpenAI
|
6 |
+
#from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
7 |
+
from langchain.memory import ConversationBufferMemory
|
8 |
+
from langchain.prompts import MessagesPlaceholder
|
9 |
from dotenv import load_dotenv
|
10 |
from config import OPEN_AI_MODEL_NAME,DEBUG_MODE_LLM
|
11 |
from image_processor import ImageProcessor
|
|
|
20 |
|
21 |
#initial system prompt
|
22 |
prompt = OpenAIFunctionsAgent.create_prompt(system_message=system_message)
|
23 |
+
|
24 |
#define LLM to use
|
25 |
llm = ChatOpenAI(temperature=0.1, model=OPEN_AI_MODEL_NAME,)
|
26 |
|
|
|
29 |
ImageProcessor()
|
30 |
]
|
31 |
|
32 |
+
agent_kwargs = {
|
33 |
+
"extra_prompt_messages": [MessagesPlaceholder(variable_name="memory")],
|
34 |
+
}
|
35 |
+
conversational_memory = ConversationBufferMemory(memory_key="memory", return_messages=True)
|
|
|
|
|
36 |
|
37 |
llm = ChatOpenAI(
|
38 |
temperature=0,
|
39 |
+
model_name=OPEN_AI_MODEL_NAME
|
|
|
40 |
)
|
41 |
|
|
|
42 |
agent = initialize_agent(
|
43 |
+
agent=AgentType.OPENAI_FUNCTIONS,
|
44 |
tools=tools,
|
45 |
llm=llm,
|
46 |
+
max_iterations=10,
|
47 |
verbose=False,
|
48 |
memory=conversational_memory,
|
49 |
+
agent_kwargs=agent_kwargs,
|
50 |
prompt=prompt
|
51 |
)
|
52 |
+
|
53 |
+
##TO DO, Remove agent and test sequential chain
|