Spaces:
Sleeping
Sleeping
Update page1.py
Browse files
page1.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 3 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
from langchain.chains import LLMChain
|
|
@@ -78,8 +79,8 @@ def text():
|
|
| 78 |
google_api_key=GOOGLE_API_KEY,
|
| 79 |
temperature=0.3,
|
| 80 |
streaming=True,
|
| 81 |
-
timeout=
|
| 82 |
-
max_retries=
|
| 83 |
|
| 84 |
)
|
| 85 |
|
|
@@ -89,7 +90,7 @@ def text():
|
|
| 89 |
# Show initial bot message
|
| 90 |
if len(st.session_state.messages) == 0:
|
| 91 |
animated_text = '<div class="anim-typewriter">Hello π, how may I assist you today?</div>'
|
| 92 |
-
#st.chat_message("assistant").markdown(animated_text, unsafe_allow_html=True)
|
| 93 |
st.session_state.messages.append({"role": "assistant", "content": "Hello π, how may I assist you today?"})
|
| 94 |
|
| 95 |
# Display historical messages
|
|
@@ -97,7 +98,7 @@ def text():
|
|
| 97 |
if message["role"] == "user":
|
| 98 |
if message.get("image"):
|
| 99 |
st.chat_message("user", avatar="π§").markdown(
|
| 100 |
-
f
|
| 101 |
unsafe_allow_html=True
|
| 102 |
)
|
| 103 |
else:
|
|
@@ -106,30 +107,52 @@ def text():
|
|
| 106 |
st.chat_message("assistant", avatar="π€").markdown(message["content"])
|
| 107 |
|
| 108 |
# Chat input with multimodal support
|
| 109 |
-
user_input = st.chat_input("Say something", accept_file=True, file_type=["png", "jpg", "jpeg"])
|
| 110 |
|
| 111 |
if user_input:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
# Process user input
|
| 113 |
-
image_url = ""
|
| 114 |
message_content = [{"type": "text", "text": user_input.text}]
|
|
|
|
| 115 |
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
uploaded_file = user_input["files"][0]
|
| 118 |
image_base64 = convert_to_base64(uploaded_file)
|
| 119 |
image_url = f"data:image/jpeg;base64,{image_base64}"
|
| 120 |
-
message_content.append({"type": "image_url", "image_url": image_url})
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
# Add user message to UI
|
| 123 |
with chat_container:
|
| 124 |
-
if
|
| 125 |
st.chat_message("user", avatar="π§").markdown(
|
| 126 |
-
f
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
unsafe_allow_html=True
|
| 134 |
)
|
| 135 |
|
|
@@ -140,7 +163,9 @@ def text():
|
|
| 140 |
st.session_state.messages.append({
|
| 141 |
"role": "user",
|
| 142 |
"content": user_input.text,
|
| 143 |
-
"image": image_url if user_input["files"] else ""
|
|
|
|
|
|
|
| 144 |
})
|
| 145 |
|
| 146 |
# Create LangChain message
|
|
@@ -242,6 +267,4 @@ def text():
|
|
| 242 |
{"input": user_message.content},
|
| 243 |
{"output": ai_message.content}
|
| 244 |
)
|
| 245 |
-
|
| 246 |
-
#st.sidebar.subheader("Raw Chat History")
|
| 247 |
-
#st.sidebar.write(st.session_state.chat_history.messages)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PyPDF2 import PdfReader
|
| 3 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
from langchain.chains import LLMChain
|
|
|
|
| 79 |
google_api_key=GOOGLE_API_KEY,
|
| 80 |
temperature=0.3,
|
| 81 |
streaming=True,
|
| 82 |
+
timeout=120,
|
| 83 |
+
max_retries=6
|
| 84 |
|
| 85 |
)
|
| 86 |
|
|
|
|
| 90 |
# Show initial bot message
|
| 91 |
if len(st.session_state.messages) == 0:
|
| 92 |
animated_text = '<div class="anim-typewriter">Hello π, how may I assist you today?</div>'
|
| 93 |
+
# st.chat_message("assistant").markdown(animated_text, unsafe_allow_html=True)
|
| 94 |
st.session_state.messages.append({"role": "assistant", "content": "Hello π, how may I assist you today?"})
|
| 95 |
|
| 96 |
# Display historical messages
|
|
|
|
| 98 |
if message["role"] == "user":
|
| 99 |
if message.get("image"):
|
| 100 |
st.chat_message("user", avatar="π§").markdown(
|
| 101 |
+
f"""{message["content"]}<br><br>{'<img src="' + message["image"] + f'" width="50" style="margin-top: 10px; border-radius: 8px;">' if message["file_type"] == "application/pdf" else '<img src="' + message["image"] + f'" width="200" style="margin-top: 10px; border-radius: 8px;">'}<br> {f'<i style="font-size: 12px;">{message["file_name"]}</i>' if message["file_type"] == "application/pdf" else message["file_name"] if message["file_type"] else ''}""",
|
| 102 |
unsafe_allow_html=True
|
| 103 |
)
|
| 104 |
else:
|
|
|
|
| 107 |
st.chat_message("assistant", avatar="π€").markdown(message["content"])
|
| 108 |
|
| 109 |
# Chat input with multimodal support
|
| 110 |
+
user_input = st.chat_input("Say something", accept_file=True, file_type=["png", "jpg", "jpeg", "pdf"])
|
| 111 |
|
| 112 |
if user_input:
|
| 113 |
+
file_type = None
|
| 114 |
+
file_name = ""
|
| 115 |
+
image_base64 = convert_to_base64("pdf_icon.png")
|
| 116 |
+
image_url = f"data:image/jpeg;base64,{image_base64}"
|
| 117 |
# Process user input
|
| 118 |
+
#image_url = ""
|
| 119 |
message_content = [{"type": "text", "text": user_input.text}]
|
| 120 |
+
files = user_input["files"]
|
| 121 |
|
| 122 |
+
|
| 123 |
+
if files:
|
| 124 |
+
file_type = files[0].type
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
if file_type in ["image/png", "image/jpg", "image/jpeg"]:
|
| 128 |
uploaded_file = user_input["files"][0]
|
| 129 |
image_base64 = convert_to_base64(uploaded_file)
|
| 130 |
image_url = f"data:image/jpeg;base64,{image_base64}"
|
|
|
|
| 131 |
|
| 132 |
+
message_content.append({"type": "image_url", "image_url": image_url})
|
| 133 |
+
text = ""
|
| 134 |
+
if file_type == "application/pdf":
|
| 135 |
+
uploaded_file = user_input["files"][0]
|
| 136 |
+
file_name = files[0].name
|
| 137 |
+
pdf_reader = PdfReader(uploaded_file)
|
| 138 |
+
for page in pdf_reader.pages:
|
| 139 |
+
text += page.extract_text()
|
| 140 |
+
#st.sidebar.write(text)
|
| 141 |
+
prompt = "this is pdf data: \n"+text +"this is user asking about pdf:"+user_input.text
|
| 142 |
+
message_content = [{"type": "text", "text": prompt}]
|
| 143 |
+
message_content.append({"type": "text", "text": file_name})
|
| 144 |
+
#message_content.append({"type": "image_url", "image_url": image_url})
|
| 145 |
# Add user message to UI
|
| 146 |
with chat_container:
|
| 147 |
+
if file_type:
|
| 148 |
st.chat_message("user", avatar="π§").markdown(
|
| 149 |
+
f"""
|
| 150 |
+
{user_input.text}
|
| 151 |
+
<br><br>
|
| 152 |
+
{'<img src="' + image_url + f'" width="50" style="margin-top: 10px; border-radius: 8px;">' if file_type == "application/pdf" else '<img src="' + image_url + f'" width="200" style="margin-top: 10px; border-radius: 8px;">' if file_type else ''}
|
| 153 |
+
<br>
|
| 154 |
+
{f'<i style="font-size: 12px;">{file_name}</i>' if file_type == "application/pdf" else file_name if file_type else ''}
|
| 155 |
+
""",
|
| 156 |
unsafe_allow_html=True
|
| 157 |
)
|
| 158 |
|
|
|
|
| 163 |
st.session_state.messages.append({
|
| 164 |
"role": "user",
|
| 165 |
"content": user_input.text,
|
| 166 |
+
"image": image_url if user_input["files"] else "",
|
| 167 |
+
"file_name" : file_name,
|
| 168 |
+
"file_type" : file_type
|
| 169 |
})
|
| 170 |
|
| 171 |
# Create LangChain message
|
|
|
|
| 267 |
{"input": user_message.content},
|
| 268 |
{"output": ai_message.content}
|
| 269 |
)
|
| 270 |
+
#st.sidebar.write(user_message)
|
|
|
|
|
|