Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -158,65 +158,91 @@ def generate_response(user_input, history, mode="chat"):
|
|
| 158 |
# ---------------------------
|
| 159 |
# 7. Gradio Chat UI
|
| 160 |
# ---------------------------
|
| 161 |
-
import traceback
|
| 162 |
-
import gradio as gr
|
| 163 |
-
|
| 164 |
-
# ---------------------------
|
| 165 |
-
# Chat Logic
|
| 166 |
-
# ---------------------------
|
| 167 |
import gradio as gr
|
|
|
|
| 168 |
|
| 169 |
-
|
|
|
|
| 170 |
if history is None:
|
| 171 |
history = []
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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: #
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
| 189 |
}
|
|
|
|
| 190 |
#ohamlab .message.bot {
|
| 191 |
-
background-color: #
|
| 192 |
-
border-radius: 14px !important;
|
| 193 |
color: #111 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
}
|
| 195 |
-
|
| 196 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
| 215 |
label=None,
|
| 216 |
height=540,
|
| 217 |
-
elem_id="ohamlab",
|
| 218 |
bubble_full_width=False,
|
| 219 |
-
|
|
|
|
| 220 |
)
|
| 221 |
|
| 222 |
with gr.Row(equal_height=False):
|
|
@@ -231,14 +257,15 @@ def build_ui():
|
|
| 231 |
send = gr.Button("Send", variant="primary")
|
| 232 |
clear = gr.Button("Clear")
|
| 233 |
|
| 234 |
-
# event bindings
|
| 235 |
send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 236 |
msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 237 |
clear.click(reset_chat, outputs=chatbot)
|
| 238 |
|
| 239 |
return demo
|
| 240 |
|
|
|
|
| 241 |
if __name__ == "__main__":
|
| 242 |
demo = build_ui()
|
| 243 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 244 |
|
|
|
|
|
|
| 158 |
# ---------------------------
|
| 159 |
# 7. Gradio Chat UI
|
| 160 |
# ---------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
import gradio as gr
|
| 162 |
+
from typing import List, Tuple
|
| 163 |
|
| 164 |
+
# --- Core Chat Logic ---
|
| 165 |
+
def chat_with_model(message: str, history: List[Tuple[str, str]]):
|
| 166 |
if history is None:
|
| 167 |
history = []
|
| 168 |
+
|
| 169 |
+
system_message = "You are OhamLab AI, a professional assistant for research and engineering."
|
| 170 |
+
|
| 171 |
+
# Build structured message history
|
| 172 |
+
messages = [{"role": "system", "content": system_message}]
|
| 173 |
+
for user_msg, bot_msg in history:
|
| 174 |
+
if user_msg:
|
| 175 |
+
messages.append({"role": "user", "content": user_msg})
|
| 176 |
+
if bot_msg:
|
| 177 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 178 |
+
|
| 179 |
+
# Add current user input
|
| 180 |
+
messages.append({"role": "user", "content": message})
|
| 181 |
+
|
| 182 |
+
# Placeholder: Replace with your model inference (RAG, etc.)
|
| 183 |
+
bot_reply = f"OhamLab: {message[::-1]}" # simulate intelligent response
|
| 184 |
+
|
| 185 |
+
# Append to history (user msg, bot reply)
|
| 186 |
+
history.append((message, bot_reply))
|
| 187 |
return history, ""
|
| 188 |
|
| 189 |
+
|
| 190 |
def reset_chat():
|
| 191 |
return []
|
| 192 |
|
| 193 |
+
|
| 194 |
+
# --- Professional OhamLab Chat UI ---
|
| 195 |
def build_ui():
|
| 196 |
with gr.Blocks(
|
| 197 |
theme=gr.themes.Soft(),
|
| 198 |
css="""
|
| 199 |
+
/* Hide share/delete icons */
|
| 200 |
+
#ohamlab .wrap.svelte-1lcyrj3 > div > div > button {
|
| 201 |
+
display: none !important;
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
/* Chat alignment and bubble styling */
|
| 205 |
#ohamlab .message.user {
|
| 206 |
+
background-color: #0047AB !important;
|
| 207 |
+
color: white !important;
|
| 208 |
+
border-radius: 16px !important;
|
| 209 |
+
align-self: flex-end !important;
|
| 210 |
+
text-align: right !important;
|
| 211 |
+
margin-left: 20%;
|
| 212 |
}
|
| 213 |
+
|
| 214 |
#ohamlab .message.bot {
|
| 215 |
+
background-color: #f2f2f2 !important;
|
|
|
|
| 216 |
color: #111 !important;
|
| 217 |
+
border-radius: 16px !important;
|
| 218 |
+
align-self: flex-start !important;
|
| 219 |
+
text-align: left !important;
|
| 220 |
+
margin-right: 20%;
|
| 221 |
}
|
| 222 |
+
|
| 223 |
+
/* General layout tweaks */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
.message-box {
|
| 225 |
width: 100%;
|
| 226 |
}
|
| 227 |
""",
|
| 228 |
) as demo:
|
| 229 |
+
gr.Markdown(
|
| 230 |
+
"""
|
| 231 |
+
<div style='text-align:center;'>
|
| 232 |
+
<h2 style='color:#0047AB;'>🤖 OhamLab AI Assistant</h2>
|
| 233 |
+
<p style='color:gray;'>Your trusted partner in advanced R&D and AI innovation.</p>
|
| 234 |
+
</div>
|
| 235 |
+
"""
|
| 236 |
+
)
|
| 237 |
|
| 238 |
chatbot = gr.Chatbot(
|
| 239 |
+
label="💠 OhamLab Conversation",
|
| 240 |
+
elem_id="ohamlab",
|
| 241 |
label=None,
|
| 242 |
height=540,
|
|
|
|
| 243 |
bubble_full_width=False,
|
| 244 |
+
show_copy_button=False,
|
| 245 |
+
avatar_images=[None, None],
|
| 246 |
)
|
| 247 |
|
| 248 |
with gr.Row(equal_height=False):
|
|
|
|
| 257 |
send = gr.Button("Send", variant="primary")
|
| 258 |
clear = gr.Button("Clear")
|
| 259 |
|
|
|
|
| 260 |
send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 261 |
msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 262 |
clear.click(reset_chat, outputs=chatbot)
|
| 263 |
|
| 264 |
return demo
|
| 265 |
|
| 266 |
+
|
| 267 |
if __name__ == "__main__":
|
| 268 |
demo = build_ui()
|
| 269 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 270 |
|
| 271 |
+
|