subhannadeem1 commited on
Commit
10b21a7
·
1 Parent(s): 839dc93

Create page2.py

Browse files
Files changed (1) hide show
  1. page2.py +188 -0
page2.py ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain_core.messages import HumanMessage
3
+ from langchain_google_genai import ChatGoogleGenerativeAI
4
+ from streamlit_chat import message
5
+ from PIL import Image
6
+ import base64
7
+ import io
8
+ from langchain.chains import LLMChain
9
+ from langchain.prompts import PromptTemplate
10
+ from langchain.memory import ConversationBufferMemory
11
+ from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
12
+
13
+ # Streamlit app
14
+ def image():
15
+ st.markdown("""
16
+ <style>
17
+ .anim-typewriter {
18
+ animation: typewriter 3s steps(40) 1s 1 normal both, blinkTextCursor 800ms steps(40) infinite normal;
19
+ overflow: hidden;
20
+ white-space: nowrap;
21
+ border-right: 3px solid;
22
+ font-family: serif;
23
+ font-size: 0.8em;
24
+ }
25
+ @keyframes typewriter {
26
+ from {
27
+ width: 0;
28
+ }
29
+ to {
30
+ width: 100%;
31
+ height: 100%
32
+ }
33
+ }
34
+ @keyframes blinkTextCursor {
35
+ from {
36
+ border-right-color: rgba(255, 255, 255, 0.75);
37
+ }
38
+ to {
39
+ border-right-color: transparent;
40
+ }
41
+ }
42
+ </style>
43
+ """, unsafe_allow_html=True)
44
+ text1 = "Hello 👋, upload an image and ask questions related to it!"
45
+ animated = f'<div class="line-1 anim-typewriter">{text1}</div>'
46
+ with st.chat_message("assistant").markdown(animated, unsafe_allow_html=True):
47
+ st.markdown(animated, unsafe_allow_html=True)
48
+ def process_image(uploaded_file):
49
+ # Display the uploaded image
50
+ image = Image.open(uploaded_file)
51
+ st.image(image, caption='Uploaded Image', use_column_width=True)
52
+
53
+ # Process the image and return the URL or other information
54
+ # For demonstration purposes, convert the image to base64 and return a data URL
55
+ buffered = io.BytesIO()
56
+ image.save(buffered, format="JPEG")
57
+ image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
58
+ image_url = f"data:image/jpeg;base64,{image_base64}"
59
+
60
+ return image_url
61
+ apiKey = "AIzaSyAXkkcrrUBjPEgj93tZ9azy7zcS1wI1jUA"
62
+
63
+ llm = ChatGoogleGenerativeAI(model="gemini-pro-vision", google_api_key=apiKey)
64
+
65
+ image_url = None # Initialize image_url outside the if statement
66
+ with st.sidebar:
67
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
68
+ if uploaded_file is not None:
69
+ image_url = process_image(uploaded_file)
70
+
71
+
72
+ if 'messages' not in st.session_state:
73
+ st.session_state['messages'] = []
74
+
75
+ for message in st.session_state.messages:
76
+ with st.chat_message(message["role"]):
77
+ st.markdown(message["content"])
78
+ prompt = st.chat_input("Say something")
79
+ message = HumanMessage(
80
+ content=[
81
+ {
82
+ "type": "text",
83
+ "text": prompt,
84
+ }, # You can optionally provide text parts
85
+ {"type": "image_url", "image_url": image_url},
86
+ ]
87
+ )
88
+
89
+ if prompt:
90
+ with st.chat_message("user").markdown(prompt):
91
+ st.session_state.messages.append(
92
+ {
93
+ "role": "user",
94
+ "content": prompt
95
+ }
96
+ )
97
+ spinner_html = """
98
+ <div class="col-3">
99
+ <div class="snippet" data-title="dot-pulse">
100
+ <div class="stage">
101
+ <div class="dot-pulse"></div>
102
+ </div>
103
+ </div>
104
+ </div>
105
+ """
106
+
107
+ spinner_css = """
108
+ .dot-pulse {
109
+ position: relative;
110
+ left: -9999px;
111
+ width: 10px;
112
+ height: 10px;
113
+ border-radius: 5px;
114
+ background-color: #9880ff;
115
+ color: #9880ff;
116
+ box-shadow: 9999px 0 0 -5px;
117
+ animation: dot-pulse 1.5s infinite linear;
118
+ animation-delay: 0.25s;
119
+ }
120
+ .dot-pulse::before, .dot-pulse::after {
121
+ content: "";
122
+ display: inline-block;
123
+ position: absolute;
124
+ top: 0;
125
+ width: 10px;
126
+ height: 10px;
127
+ border-radius: 5px;
128
+ background-color: #9880ff;
129
+ color: #9880ff;
130
+ }
131
+ .dot-pulse::before {
132
+ box-shadow: 9984px 0 0 -5px;
133
+ animation: dot-pulse-before 1.5s infinite linear;
134
+ animation-delay: 0s;
135
+ }
136
+ .dot-pulse::after {
137
+ box-shadow: 10014px 0 0 -5px;
138
+ animation: dot-pulse-after 1.5s infinite linear;
139
+ animation-delay: 0.5s;
140
+ }
141
+ @keyframes dot-pulse-before {
142
+ 0% {
143
+ box-shadow: 9984px 0 0 -5px;
144
+ }
145
+ 30% {
146
+ box-shadow: 9984px 0 0 2px;
147
+ }
148
+ 60%, 100% {
149
+ box-shadow: 9984px 0 0 -5px;
150
+ }
151
+ }
152
+ @keyframes dot-pulse {
153
+ 0% {
154
+ box-shadow: 9999px 0 0 -5px;
155
+ }
156
+ 30% {
157
+ box-shadow: 9999px 0 0 2px;
158
+ }
159
+ 60%, 100% {
160
+ box-shadow: 9999px 0 0 -5px;
161
+ }
162
+ }
163
+ @keyframes dot-pulse-after {
164
+ 0% {
165
+ box-shadow: 10014px 0 0 -5px;
166
+ }
167
+ 30% {
168
+ box-shadow: 10014px 0 0 2px;
169
+ }
170
+ 60%, 100% {
171
+ box-shadow: 10014px 0 0 -5px;
172
+ }
173
+ }
174
+ """
175
+
176
+ st.markdown(f'<style>{spinner_css}</style>', unsafe_allow_html=True)
177
+ st.markdown(spinner_html, unsafe_allow_html=True)
178
+ response = llm.invoke([message])
179
+ text_output = response.content
180
+ st.markdown('<style>.dot-pulse { visibility: hidden; }</style>', unsafe_allow_html=True)
181
+
182
+ with st.chat_message("assistant").markdown(text_output):
183
+ st.session_state.messages.append(
184
+ {
185
+ "role": "assistant",
186
+ "content": text_output
187
+ }
188
+ )