Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ and provides retrieval-augmented chat through Hugging Face router.
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
-
import re
|
| 9 |
import json
|
| 10 |
import time
|
| 11 |
import textwrap
|
|
@@ -31,7 +30,6 @@ EMBED_MODEL = "text-embedding-3-small" # Embedding model
|
|
| 31 |
HF_REPO = "rahul7star/OhamLab-LLM" # Knowledge repo
|
| 32 |
CACHE_PATH = "/tmp/ohamlab_emb_cache.json" # Cache file
|
| 33 |
|
| 34 |
-
# Client
|
| 35 |
client = OpenAI(base_url="https://router.huggingface.co/v1", api_key=HF_TOKEN)
|
| 36 |
api = HfApi(token=HF_TOKEN)
|
| 37 |
|
|
@@ -39,7 +37,7 @@ api = HfApi(token=HF_TOKEN)
|
|
| 39 |
# 2. Load and Chunk Markdown Files
|
| 40 |
# ---------------------------
|
| 41 |
def load_ohamlab_knowledge():
|
| 42 |
-
"""
|
| 43 |
files = list_repo_files(HF_REPO, repo_type="model", token=HF_TOKEN)
|
| 44 |
md_files = [f for f in files if f.endswith(".md")]
|
| 45 |
chunks = []
|
|
@@ -64,7 +62,7 @@ def load_ohamlab_knowledge():
|
|
| 64 |
# 3. Generate or Load Embeddings (with Cache)
|
| 65 |
# ---------------------------
|
| 66 |
def get_embeddings_with_cache():
|
| 67 |
-
"""Generate or load cached embeddings for OhamLab
|
| 68 |
if os.path.exists(CACHE_PATH):
|
| 69 |
try:
|
| 70 |
with open(CACHE_PATH, "r") as f:
|
|
@@ -103,7 +101,7 @@ OHAMLAB_TEXTS, OHAMLAB_EMBS = get_embeddings_with_cache()
|
|
| 103 |
# 4. Semantic Retrieval
|
| 104 |
# ---------------------------
|
| 105 |
def retrieve_knowledge(query, top_k=3):
|
| 106 |
-
"""Retrieve top-k most relevant text snippets."""
|
| 107 |
try:
|
| 108 |
q_emb = client.embeddings.create(model=EMBED_MODEL, input=[query]).data[0].embedding
|
| 109 |
sims = np.dot(OHAMLAB_EMBS, q_emb) / (
|
|
@@ -120,13 +118,13 @@ def retrieve_knowledge(query, top_k=3):
|
|
| 120 |
# ---------------------------
|
| 121 |
def build_system_prompt(context: str, mode: str = "chat") -> str:
|
| 122 |
return textwrap.dedent(f"""
|
| 123 |
-
You are OhamLab — AI Intelligence Software
|
| 124 |
|
| 125 |
Guidelines:
|
|
|
|
|
|
|
| 126 |
- Always answer with clarity, scientific accuracy, and concise insight.
|
| 127 |
-
- Incorporate OhamLab research knowledge when relevant.
|
| 128 |
- Avoid code unless explicitly requested.
|
| 129 |
-
- Be confident but label speculation clearly.
|
| 130 |
- Mode: {mode.upper()}
|
| 131 |
|
| 132 |
--- OhamLab Context (Retrieved Snippets) ---
|
|
@@ -147,45 +145,31 @@ def generate_response(user_input, history, mode="chat"):
|
|
| 147 |
resp = client.chat.completions.create(
|
| 148 |
model=MODEL_ID,
|
| 149 |
messages=messages,
|
| 150 |
-
temperature=0.
|
| 151 |
max_tokens=1200,
|
| 152 |
)
|
| 153 |
return resp.choices[0].message.content.strip()
|
| 154 |
except Exception as e:
|
| 155 |
print(f"⚠️ Model call failed: {e}")
|
| 156 |
-
return "⚠️
|
| 157 |
|
| 158 |
# ---------------------------
|
| 159 |
-
# 7.
|
| 160 |
# ---------------------------
|
| 161 |
-
import traceback
|
| 162 |
-
import gradio as gr
|
| 163 |
-
|
| 164 |
-
# ---------------------------
|
| 165 |
-
# Chat Logic
|
| 166 |
-
# ---------------------------
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
def chat_with_model(user_message, chat_history):
|
| 171 |
-
"""
|
| 172 |
-
Maintains full conversational context and returns updated chat history.
|
| 173 |
-
The assistant speaks as 'OhamLab'.
|
| 174 |
-
"""
|
| 175 |
if not user_message:
|
| 176 |
return chat_history, ""
|
| 177 |
|
| 178 |
if chat_history is None:
|
| 179 |
chat_history = []
|
| 180 |
|
| 181 |
-
# Convert Gradio message list (dict-based) to usable context
|
| 182 |
history = [
|
| 183 |
{"role": m["role"], "content": m["content"]}
|
| 184 |
for m in chat_history
|
| 185 |
if isinstance(m, dict) and "role" in m
|
| 186 |
]
|
| 187 |
|
| 188 |
-
# Append current user message
|
| 189 |
history.append({"role": "user", "content": user_message})
|
| 190 |
|
| 191 |
try:
|
|
@@ -194,99 +178,34 @@ def chat_with_model(user_message, chat_history):
|
|
| 194 |
tb = traceback.format_exc()
|
| 195 |
bot_reply = f"⚠️ OhamLab encountered an error:\n\n{e}\n\n{tb}"
|
| 196 |
|
| 197 |
-
# Add OhamLab's response as assistant role
|
| 198 |
history.append({"role": "assistant", "content": bot_reply})
|
| 199 |
-
|
| 200 |
return history, ""
|
| 201 |
|
| 202 |
-
|
| 203 |
def reset_chat():
|
| 204 |
-
"""Resets the chat session."""
|
| 205 |
return []
|
| 206 |
|
| 207 |
-
|
| 208 |
# ---------------------------
|
| 209 |
-
# Gradio Chat UI
|
| 210 |
# ---------------------------
|
| 211 |
-
|
| 212 |
def build_ui():
|
| 213 |
with gr.Blocks(
|
| 214 |
theme=gr.themes.Soft(primary_hue="indigo"),
|
| 215 |
css="""
|
| 216 |
-
/* --- Hide share/delete icons --- */
|
| 217 |
-
#ohamlab .wrap.svelte-1lcyrj3 > div > div > button {
|
| 218 |
-
display: none !important;
|
| 219 |
-
}
|
| 220 |
[data-testid="share-btn"],
|
| 221 |
[data-testid="delete-btn"],
|
| 222 |
.message-controls,
|
| 223 |
-
.message-actions {
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
}
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
#
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
align-self: flex-end !important;
|
| 234 |
-
text-align: right !important;
|
| 235 |
-
margin-left: 25%;
|
| 236 |
-
}
|
| 237 |
-
|
| 238 |
-
/* --- OhamLab (Left) Message Bubble --- */
|
| 239 |
-
#ohamlab .message.assistant {
|
| 240 |
-
background-color: #f8f9fa !important;
|
| 241 |
-
color: #111 !important;
|
| 242 |
-
border-radius: 14px !important;
|
| 243 |
-
align-self: flex-start !important;
|
| 244 |
-
text-align: left !important;
|
| 245 |
-
margin-right: 25%;
|
| 246 |
-
}
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
#ohamlab .chatbot .wrap.svelte-1lcyrj3 > div > div > button {
|
| 250 |
-
display: none !important; /* hide share/delete icons */
|
| 251 |
-
}
|
| 252 |
-
|
| 253 |
-
/* --- Overall Container --- */
|
| 254 |
-
.gradio-container {
|
| 255 |
-
max-width: 900px !important;
|
| 256 |
-
margin: auto;
|
| 257 |
-
padding-top: .5rem;
|
| 258 |
-
}
|
| 259 |
-
textarea {
|
| 260 |
-
resize: none !important;
|
| 261 |
-
border-radius: 12px !important;
|
| 262 |
-
border: 1px solid #d1d5db !important;
|
| 263 |
-
box-shadow: 0 1px 3px rgba(0,0,0,0.08);
|
| 264 |
-
}
|
| 265 |
-
button.primary {
|
| 266 |
-
background-color: #4f46e5 !important;
|
| 267 |
-
color: white !important;
|
| 268 |
-
border-radius: 10px !important;
|
| 269 |
-
padding: 0.6rem 1.4rem !important;
|
| 270 |
-
font-weight: 600;
|
| 271 |
-
transition: all 0.2s ease-in-out;
|
| 272 |
-
}
|
| 273 |
-
button.primary:hover {
|
| 274 |
-
background-color: #4338ca !important;
|
| 275 |
-
}
|
| 276 |
-
button.secondary {
|
| 277 |
-
background-color: #f3f4f6 !important;
|
| 278 |
-
border-radius: 10px !important;
|
| 279 |
-
color: #374151 !important;
|
| 280 |
-
font-weight: 500;
|
| 281 |
-
transition: all 0.2s ease-in-out;
|
| 282 |
-
}
|
| 283 |
-
button.secondary:hover {
|
| 284 |
-
background-color: #e5e7eb !important;
|
| 285 |
-
}
|
| 286 |
-
""",
|
| 287 |
) as demo:
|
| 288 |
-
|
| 289 |
-
# Chatbot area
|
| 290 |
chatbot = gr.Chatbot(
|
| 291 |
label="💠 OhamLab Conversation",
|
| 292 |
height=520,
|
|
@@ -295,7 +214,6 @@ def build_ui():
|
|
| 295 |
avatar_images=[None, None],
|
| 296 |
)
|
| 297 |
|
| 298 |
-
# Input box (full width)
|
| 299 |
with gr.Row():
|
| 300 |
msg = gr.Textbox(
|
| 301 |
placeholder="Ask OhamLab anything ..",
|
|
@@ -305,19 +223,16 @@ def build_ui():
|
|
| 305 |
container=False,
|
| 306 |
)
|
| 307 |
|
| 308 |
-
# Buttons (Send + Clear)
|
| 309 |
with gr.Row(equal_height=True, variant="compact"):
|
| 310 |
-
send = gr.Button("Send", variant="primary"
|
| 311 |
-
clear = gr.Button("Clear", variant="secondary"
|
| 312 |
|
| 313 |
-
# Wiring
|
| 314 |
send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 315 |
msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 316 |
clear.click(reset_chat, outputs=chatbot)
|
| 317 |
|
| 318 |
return demo
|
| 319 |
|
| 320 |
-
|
| 321 |
# ---------------------------
|
| 322 |
# Entrypoint
|
| 323 |
# ---------------------------
|
|
@@ -325,5 +240,3 @@ if __name__ == "__main__":
|
|
| 325 |
print("🚀 Starting OhamLab Assistant...")
|
| 326 |
demo = build_ui()
|
| 327 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 328 |
-
|
| 329 |
-
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
|
|
|
| 8 |
import json
|
| 9 |
import time
|
| 10 |
import textwrap
|
|
|
|
| 30 |
HF_REPO = "rahul7star/OhamLab-LLM" # Knowledge repo
|
| 31 |
CACHE_PATH = "/tmp/ohamlab_emb_cache.json" # Cache file
|
| 32 |
|
|
|
|
| 33 |
client = OpenAI(base_url="https://router.huggingface.co/v1", api_key=HF_TOKEN)
|
| 34 |
api = HfApi(token=HF_TOKEN)
|
| 35 |
|
|
|
|
| 37 |
# 2. Load and Chunk Markdown Files
|
| 38 |
# ---------------------------
|
| 39 |
def load_ohamlab_knowledge():
|
| 40 |
+
"""Load all .md files from Hugging Face repo and split into ~500-char chunks."""
|
| 41 |
files = list_repo_files(HF_REPO, repo_type="model", token=HF_TOKEN)
|
| 42 |
md_files = [f for f in files if f.endswith(".md")]
|
| 43 |
chunks = []
|
|
|
|
| 62 |
# 3. Generate or Load Embeddings (with Cache)
|
| 63 |
# ---------------------------
|
| 64 |
def get_embeddings_with_cache():
|
| 65 |
+
"""Generate or load cached embeddings for OhamLab knowledge."""
|
| 66 |
if os.path.exists(CACHE_PATH):
|
| 67 |
try:
|
| 68 |
with open(CACHE_PATH, "r") as f:
|
|
|
|
| 101 |
# 4. Semantic Retrieval
|
| 102 |
# ---------------------------
|
| 103 |
def retrieve_knowledge(query, top_k=3):
|
| 104 |
+
"""Retrieve top-k most relevant text snippets from markdown knowledge bank."""
|
| 105 |
try:
|
| 106 |
q_emb = client.embeddings.create(model=EMBED_MODEL, input=[query]).data[0].embedding
|
| 107 |
sims = np.dot(OHAMLAB_EMBS, q_emb) / (
|
|
|
|
| 118 |
# ---------------------------
|
| 119 |
def build_system_prompt(context: str, mode: str = "chat") -> str:
|
| 120 |
return textwrap.dedent(f"""
|
| 121 |
+
You are OhamLab — AI Intelligence Software.
|
| 122 |
|
| 123 |
Guidelines:
|
| 124 |
+
- Only answer using information retrieved from the OhamLab knowledge bank (.md files).
|
| 125 |
+
- Do not answer anything outside of this knowledge; if unknown, respond: "I could not find an answer in my knowledge base."
|
| 126 |
- Always answer with clarity, scientific accuracy, and concise insight.
|
|
|
|
| 127 |
- Avoid code unless explicitly requested.
|
|
|
|
| 128 |
- Mode: {mode.upper()}
|
| 129 |
|
| 130 |
--- OhamLab Context (Retrieved Snippets) ---
|
|
|
|
| 145 |
resp = client.chat.completions.create(
|
| 146 |
model=MODEL_ID,
|
| 147 |
messages=messages,
|
| 148 |
+
temperature=0.0, # deterministic for knowledge-only responses
|
| 149 |
max_tokens=1200,
|
| 150 |
)
|
| 151 |
return resp.choices[0].message.content.strip()
|
| 152 |
except Exception as e:
|
| 153 |
print(f"⚠️ Model call failed: {e}")
|
| 154 |
+
return "⚠️ OhamLab encountered a temporary issue generating your response."
|
| 155 |
|
| 156 |
# ---------------------------
|
| 157 |
+
# 7. Chat Logic
|
| 158 |
# ---------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
def chat_with_model(user_message, chat_history):
|
| 160 |
+
"""Maintains full conversational context and returns updated chat history."""
|
|
|
|
|
|
|
|
|
|
| 161 |
if not user_message:
|
| 162 |
return chat_history, ""
|
| 163 |
|
| 164 |
if chat_history is None:
|
| 165 |
chat_history = []
|
| 166 |
|
|
|
|
| 167 |
history = [
|
| 168 |
{"role": m["role"], "content": m["content"]}
|
| 169 |
for m in chat_history
|
| 170 |
if isinstance(m, dict) and "role" in m
|
| 171 |
]
|
| 172 |
|
|
|
|
| 173 |
history.append({"role": "user", "content": user_message})
|
| 174 |
|
| 175 |
try:
|
|
|
|
| 178 |
tb = traceback.format_exc()
|
| 179 |
bot_reply = f"⚠️ OhamLab encountered an error:\n\n{e}\n\n{tb}"
|
| 180 |
|
|
|
|
| 181 |
history.append({"role": "assistant", "content": bot_reply})
|
|
|
|
| 182 |
return history, ""
|
| 183 |
|
|
|
|
| 184 |
def reset_chat():
|
|
|
|
| 185 |
return []
|
| 186 |
|
|
|
|
| 187 |
# ---------------------------
|
| 188 |
+
# 8. Gradio Chat UI
|
| 189 |
# ---------------------------
|
|
|
|
| 190 |
def build_ui():
|
| 191 |
with gr.Blocks(
|
| 192 |
theme=gr.themes.Soft(primary_hue="indigo"),
|
| 193 |
css="""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
[data-testid="share-btn"],
|
| 195 |
[data-testid="delete-btn"],
|
| 196 |
.message-controls,
|
| 197 |
+
.message-actions { display: none !important; visibility: hidden !important; }
|
| 198 |
+
#ohamlab .message.user { background-color: #4f46e5 !important; color: white !important; border-radius: 14px !important; align-self: flex-end !important; text-align: right !important; margin-left: 25%; }
|
| 199 |
+
#ohamlab .message.assistant { background-color: #f8f9fa !important; color: #111 !important; border-radius: 14px !important; align-self: flex-start !important; text-align: left !important; margin-right: 25%; }
|
| 200 |
+
.gradio-container { max-width: 900px !important; margin: auto; padding-top: .5rem; }
|
| 201 |
+
textarea { resize: none !important; border-radius: 12px !important; border: 1px solid #d1d5db !important; box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
|
| 202 |
+
button.primary { background-color: #4f46e5 !important; color: white !important; border-radius: 10px !important; padding: 0.6rem 1.4rem !important; font-weight: 600; transition: all 0.2s ease-in-out; }
|
| 203 |
+
button.primary:hover { background-color: #4338ca !important; }
|
| 204 |
+
button.secondary { background-color: #f3f4f6 !important; border-radius: 10px !important; color: #374151 !important; font-weight: 500; transition: all 0.2s ease-in-out; }
|
| 205 |
+
button.secondary:hover { background-color: #e5e7eb !important; }
|
| 206 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
) as demo:
|
| 208 |
+
|
|
|
|
| 209 |
chatbot = gr.Chatbot(
|
| 210 |
label="💠 OhamLab Conversation",
|
| 211 |
height=520,
|
|
|
|
| 214 |
avatar_images=[None, None],
|
| 215 |
)
|
| 216 |
|
|
|
|
| 217 |
with gr.Row():
|
| 218 |
msg = gr.Textbox(
|
| 219 |
placeholder="Ask OhamLab anything ..",
|
|
|
|
| 223 |
container=False,
|
| 224 |
)
|
| 225 |
|
|
|
|
| 226 |
with gr.Row(equal_height=True, variant="compact"):
|
| 227 |
+
send = gr.Button("Send", variant="primary")
|
| 228 |
+
clear = gr.Button("Clear", variant="secondary")
|
| 229 |
|
|
|
|
| 230 |
send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 231 |
msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 232 |
clear.click(reset_chat, outputs=chatbot)
|
| 233 |
|
| 234 |
return demo
|
| 235 |
|
|
|
|
| 236 |
# ---------------------------
|
| 237 |
# Entrypoint
|
| 238 |
# ---------------------------
|
|
|
|
| 240 |
print("🚀 Starting OhamLab Assistant...")
|
| 241 |
demo = build_ui()
|
| 242 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|