Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -164,128 +164,84 @@ import gradio as gr
|
|
| 164 |
# ---------------------------
|
| 165 |
# Chat Logic
|
| 166 |
# ---------------------------
|
| 167 |
-
|
| 168 |
-
"""
|
| 169 |
-
Maintains full conversational context and returns updated chat history.
|
| 170 |
-
The assistant speaks as 'OhamLab'.
|
| 171 |
-
"""
|
| 172 |
-
if not user_message:
|
| 173 |
-
return chat_history, ""
|
| 174 |
-
|
| 175 |
-
# Maintain persistent history in OpenAI-style structure
|
| 176 |
-
history = [
|
| 177 |
-
{"role": m["role"], "content": m["content"]}
|
| 178 |
-
for m in chat_history
|
| 179 |
-
if isinstance(m, dict) and "role" in m
|
| 180 |
-
]
|
| 181 |
-
|
| 182 |
-
# Append user query
|
| 183 |
-
history.append({"role": "user", "content": user_message})
|
| 184 |
-
|
| 185 |
-
try:
|
| 186 |
-
bot_text = generate_response(user_message, history)
|
| 187 |
-
except Exception as e:
|
| 188 |
-
tb = traceback.format_exc()
|
| 189 |
-
bot_text = f"β οΈ OhamLab encountered an error:\n\n{e}\n\n{tb}"
|
| 190 |
-
|
| 191 |
-
# Append OhamLab reply to chat history
|
| 192 |
-
chat_history.append({"role": "assistant", "content": f"**OhamLab:** {bot_text}"})
|
| 193 |
-
|
| 194 |
-
return chat_history, ""
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
def reset_chat():
|
| 198 |
-
"""Resets the chat session."""
|
| 199 |
return []
|
| 200 |
|
| 201 |
-
|
| 202 |
-
# ---------------------------
|
| 203 |
-
# Gradio Chat UI
|
| 204 |
-
# ---------------------------
|
| 205 |
def build_ui():
|
| 206 |
with gr.Blocks(
|
| 207 |
-
theme=gr.themes.Soft(
|
| 208 |
css="""
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
button.primary:hover {
|
| 235 |
-
background-color: #4338ca !important;
|
| 236 |
-
}
|
| 237 |
-
button.secondary {
|
| 238 |
-
background-color: #f3f4f6 !important;
|
| 239 |
-
border-radius: 10px !important;
|
| 240 |
-
color: #374151 !important;
|
| 241 |
-
font-weight: 500;
|
| 242 |
-
transition: all 0.2s ease-in-out;
|
| 243 |
-
}
|
| 244 |
-
button.secondary:hover {
|
| 245 |
-
background-color: #e5e7eb !important;
|
| 246 |
-
}
|
| 247 |
-
"""
|
| 248 |
) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
# Chatbot (branded to OhamLab)
|
| 253 |
chatbot = gr.Chatbot(
|
| 254 |
-
label=
|
| 255 |
-
height=
|
| 256 |
elem_id="ohamlab",
|
| 257 |
-
|
| 258 |
-
avatar_images=
|
| 259 |
)
|
| 260 |
|
| 261 |
-
|
| 262 |
-
with gr.Row():
|
| 263 |
msg = gr.Textbox(
|
| 264 |
-
placeholder="
|
| 265 |
lines=3,
|
| 266 |
-
scale=12,
|
| 267 |
show_label=False,
|
| 268 |
-
|
| 269 |
)
|
| 270 |
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
clear = gr.Button("Clear", variant="secondary", elem_classes=["secondary"])
|
| 275 |
|
| 276 |
-
#
|
| 277 |
send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 278 |
msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 279 |
clear.click(reset_chat, outputs=chatbot)
|
| 280 |
|
| 281 |
return demo
|
| 282 |
|
| 283 |
-
|
| 284 |
-
# ---------------------------
|
| 285 |
-
# Entrypoint
|
| 286 |
-
# ---------------------------
|
| 287 |
if __name__ == "__main__":
|
| 288 |
-
print("π Starting OhamLab Assistant...")
|
| 289 |
demo = build_ui()
|
| 290 |
-
demo.launch(server_name="0.0.0.0", server_port=7860
|
| 291 |
|
|
|
|
| 164 |
# ---------------------------
|
| 165 |
# Chat Logic
|
| 166 |
# ---------------------------
|
| 167 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
def chat_with_model(message, history):
|
| 170 |
+
if history is None:
|
| 171 |
+
history = []
|
| 172 |
+
# Simulate bot response
|
| 173 |
+
response = f"OhamLab says: {message[::-1]}" # replace with actual model call
|
| 174 |
+
history.append(("user", message))
|
| 175 |
+
history.append(("bot", response))
|
| 176 |
+
return history, ""
|
| 177 |
|
| 178 |
def reset_chat():
|
|
|
|
| 179 |
return []
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
def build_ui():
|
| 182 |
with gr.Blocks(
|
| 183 |
+
theme=gr.themes.Soft(),
|
| 184 |
css="""
|
| 185 |
+
#ohamlab .message.user {
|
| 186 |
+
background-color: #e6f0ff !important;
|
| 187 |
+
border-radius: 14px !important;
|
| 188 |
+
color: #003366 !important;
|
| 189 |
+
}
|
| 190 |
+
#ohamlab .message.bot {
|
| 191 |
+
background-color: #f8f9fa !important;
|
| 192 |
+
border-radius: 14px !important;
|
| 193 |
+
color: #111 !important;
|
| 194 |
+
}
|
| 195 |
+
#ohamlab .chatbot .wrap.svelte-1lcyrj3 > div > div > button {
|
| 196 |
+
display: none !important; /* hide share/delete icons */
|
| 197 |
+
}
|
| 198 |
+
#ohamlab .bot-icon {
|
| 199 |
+
background: linear-gradient(90deg, #0047AB, #007FFF);
|
| 200 |
+
color: white;
|
| 201 |
+
border-radius: 50%;
|
| 202 |
+
padding: 6px 10px;
|
| 203 |
+
font-weight: 600;
|
| 204 |
+
font-size: 14px;
|
| 205 |
+
}
|
| 206 |
+
.message-box {
|
| 207 |
+
width: 100%;
|
| 208 |
+
}
|
| 209 |
+
""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
) as demo:
|
| 211 |
+
|
| 212 |
+
gr.Markdown(
|
| 213 |
+
"<h2 style='text-align:center; color:#0047AB;'>π¬ OhamLab AI Assistant</h2>"
|
| 214 |
+
"<p style='text-align:center; color:gray;'>Your professional research and engineering companion.</p>"
|
| 215 |
+
)
|
| 216 |
|
|
|
|
|
|
|
|
|
|
| 217 |
chatbot = gr.Chatbot(
|
| 218 |
+
label=None,
|
| 219 |
+
height=540,
|
| 220 |
elem_id="ohamlab",
|
| 221 |
+
bubble_full_width=False,
|
| 222 |
+
avatar_images=[None, None], # disable circular icons
|
| 223 |
)
|
| 224 |
|
| 225 |
+
with gr.Row(equal_height=False):
|
|
|
|
| 226 |
msg = gr.Textbox(
|
| 227 |
+
placeholder="Type your message here...",
|
| 228 |
lines=3,
|
|
|
|
| 229 |
show_label=False,
|
| 230 |
+
elem_classes="message-box",
|
| 231 |
)
|
| 232 |
|
| 233 |
+
with gr.Row():
|
| 234 |
+
send = gr.Button("Send", variant="primary")
|
| 235 |
+
clear = gr.Button("Clear")
|
|
|
|
| 236 |
|
| 237 |
+
# event bindings
|
| 238 |
send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 239 |
msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 240 |
clear.click(reset_chat, outputs=chatbot)
|
| 241 |
|
| 242 |
return demo
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
if __name__ == "__main__":
|
|
|
|
| 245 |
demo = build_ui()
|
| 246 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 247 |
|