Update app.py
Browse files
app.py
CHANGED
@@ -92,37 +92,121 @@ def chat_interface(user_input):
|
|
92 |
|
93 |
# ---- Gradio App Setup ----
|
94 |
import gradio as gr
|
95 |
-
|
96 |
-
auth = ("Tej", "186281mps", "ACC", "HIPE")
|
97 |
|
98 |
with gr.Blocks() as app:
|
99 |
-
gr.Markdown("# **
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
# Adding custom styling for the UI
|
109 |
gr.HTML("""
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
""")
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
# Setting the button click event
|
125 |
-
|
126 |
|
127 |
# Launch the Gradio app
|
128 |
app.launch()
|
|
|
92 |
|
93 |
# ---- Gradio App Setup ----
|
94 |
import gradio as gr
|
95 |
+
import datetime
|
|
|
96 |
|
97 |
with gr.Blocks() as app:
|
98 |
+
gr.Markdown("# **Gertrude**")
|
99 |
+
|
100 |
+
# Chat history container
|
101 |
+
chatbot = gr.HTML(value="<div id='chat-box'></div>", elem_id="chat-output", interactive=False)
|
102 |
+
user_input = gr.Textbox(label="", placeholder="Type a message...", lines=1)
|
103 |
+
submit_button = gr.Button("Send")
|
104 |
+
|
105 |
+
# Custom CSS for text message bubbles and layout
|
|
|
|
|
106 |
gr.HTML("""
|
107 |
+
<style>
|
108 |
+
#chat-output {
|
109 |
+
background-color: #f0f0f0;
|
110 |
+
border: 1px solid #ccc;
|
111 |
+
border-radius: 10px;
|
112 |
+
height: 500px;
|
113 |
+
overflow-y: auto;
|
114 |
+
padding: 15px;
|
115 |
+
font-family: Arial, sans-serif;
|
116 |
+
}
|
117 |
+
.chat-bubble {
|
118 |
+
display: inline-block;
|
119 |
+
padding: 10px 15px;
|
120 |
+
border-radius: 20px;
|
121 |
+
margin: 5px 0;
|
122 |
+
max-width: 70%;
|
123 |
+
word-wrap: break-word;
|
124 |
+
}
|
125 |
+
.user-bubble {
|
126 |
+
background-color: #DCF8C6; /* WhatsApp light green */
|
127 |
+
float: right;
|
128 |
+
text-align: left;
|
129 |
+
margin-right: 10px;
|
130 |
+
clear: both;
|
131 |
+
}
|
132 |
+
.bot-bubble {
|
133 |
+
background-color: #FFFFFF; /* White for bot responses */
|
134 |
+
border: 1px solid #E1E1E1;
|
135 |
+
float: left;
|
136 |
+
text-align: left;
|
137 |
+
margin-left: 10px;
|
138 |
+
clear: both;
|
139 |
+
}
|
140 |
+
.timestamp {
|
141 |
+
font-size: 10px;
|
142 |
+
color: #888;
|
143 |
+
margin: 2px 0 10px;
|
144 |
+
}
|
145 |
+
.typing {
|
146 |
+
font-style: italic;
|
147 |
+
color: #888;
|
148 |
+
margin: 5px;
|
149 |
+
animation: blink 1.5s step-start 0s infinite;
|
150 |
+
}
|
151 |
+
@keyframes blink {
|
152 |
+
50% { opacity: 0; }
|
153 |
+
}
|
154 |
+
</style>
|
155 |
""")
|
156 |
|
157 |
+
# Chat logic with message timestamps
|
158 |
+
chat_history = []
|
159 |
+
|
160 |
+
def update_chat(user_message):
|
161 |
+
# Generate timestamp
|
162 |
+
timestamp = datetime.datetime.now().strftime("%I:%M %p")
|
163 |
+
|
164 |
+
# Add user message to the chat history
|
165 |
+
user_bubble = f"""
|
166 |
+
<div class='chat-bubble user-bubble'>
|
167 |
+
{user_message}
|
168 |
+
<div class='timestamp'>{timestamp}</div>
|
169 |
+
</div>
|
170 |
+
"""
|
171 |
+
chat_history.append(user_bubble)
|
172 |
+
|
173 |
+
# Add bot "typing..." indicator
|
174 |
+
typing_indicator = """
|
175 |
+
<div class='chat-bubble bot-bubble typing'>Gertrude is typing...</div>
|
176 |
+
"""
|
177 |
+
chat_history.append(typing_indicator)
|
178 |
+
|
179 |
+
# Display chat history with typing indicator
|
180 |
+
chat_html = "".join(chat_history)
|
181 |
+
yield gr.HTML.update(value=f"<div id='chat-box'>{chat_html}</div>")
|
182 |
+
|
183 |
+
# Generate bot response and update the chat
|
184 |
+
bot_response = advanced_agi_chat(user_message)
|
185 |
+
chat_history.pop() # Remove typing indicator
|
186 |
+
bot_bubble = f"""
|
187 |
+
<div class='chat-bubble bot-bubble'>
|
188 |
+
{bot_response}
|
189 |
+
<div class='timestamp'>{timestamp}</div>
|
190 |
+
</div>
|
191 |
+
"""
|
192 |
+
chat_history.append(bot_bubble)
|
193 |
+
|
194 |
+
# Update chat history with final bot response
|
195 |
+
chat_html = "".join(chat_history)
|
196 |
+
yield gr.HTML.update(value=f"<div id='chat-box'>{chat_html}")
|
197 |
+
|
198 |
+
# Connect button click with the chat logic
|
199 |
+
submit_button.click(
|
200 |
+
update_chat,
|
201 |
+
inputs=user_input,
|
202 |
+
outputs=chatbot,
|
203 |
+
queue=True
|
204 |
+
)
|
205 |
+
|
206 |
+
|
207 |
+
|
208 |
# Setting the button click event
|
209 |
+
|
210 |
|
211 |
# Launch the Gradio app
|
212 |
app.launch()
|