Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
OhamLab — AI Intelligence
|
| 3 |
Loads knowledge from rahul7star/OhamLab-LLM markdown corpus, caches embeddings,
|
| 4 |
and provides retrieval-augmented chat through Hugging Face router.
|
| 5 |
"""
|
|
@@ -25,10 +25,10 @@ HF_TOKEN = (
|
|
| 25 |
if not HF_TOKEN:
|
| 26 |
raise RuntimeError("❌ Missing HF_TOKEN / OPENAI_API_KEY / HUGGINGFACE_TOKEN environment variable.")
|
| 27 |
|
| 28 |
-
MODEL_ID = "openai/gpt-oss-20b"
|
| 29 |
-
EMBED_MODEL = "
|
| 30 |
-
HF_REPO = "rahul7star/OhamLab-LLM"
|
| 31 |
-
CACHE_PATH = "/tmp/ohamlab_emb_cache.json"
|
| 32 |
|
| 33 |
client = OpenAI(base_url="https://router.huggingface.co/v1", api_key=HF_TOKEN)
|
| 34 |
api = HfApi(token=HF_TOKEN)
|
|
@@ -56,10 +56,11 @@ def load_ohamlab_knowledge():
|
|
| 56 |
chunks.append({"file": f, "text": buf.strip()})
|
| 57 |
except Exception as e:
|
| 58 |
print(f"⚠️ Failed to load {f}: {e}")
|
|
|
|
| 59 |
return chunks
|
| 60 |
|
| 61 |
# ---------------------------
|
| 62 |
-
# 3.
|
| 63 |
# ---------------------------
|
| 64 |
def create_embeddings_with_retry(texts, retries=3, delay=2):
|
| 65 |
"""Generate embeddings with retries on failure."""
|
|
@@ -121,7 +122,7 @@ def retrieve_knowledge(query, top_k=3):
|
|
| 121 |
return ""
|
| 122 |
|
| 123 |
# ---------------------------
|
| 124 |
-
# 5. System Prompt
|
| 125 |
# ---------------------------
|
| 126 |
def build_system_prompt(context: str, mode: str = "chat") -> str:
|
| 127 |
return textwrap.dedent(f"""
|
|
@@ -129,8 +130,8 @@ def build_system_prompt(context: str, mode: str = "chat") -> str:
|
|
| 129 |
|
| 130 |
Guidelines:
|
| 131 |
- Only answer using information retrieved from the OhamLab knowledge bank (.md files).
|
| 132 |
-
- Do
|
| 133 |
-
- Always answer with clarity,
|
| 134 |
- Avoid code unless explicitly requested.
|
| 135 |
- Mode: {mode.upper()}
|
| 136 |
|
|
@@ -140,7 +141,7 @@ def build_system_prompt(context: str, mode: str = "chat") -> str:
|
|
| 140 |
""").strip()
|
| 141 |
|
| 142 |
# ---------------------------
|
| 143 |
-
# 6.
|
| 144 |
# ---------------------------
|
| 145 |
def generate_response(user_input, history, mode="chat"):
|
| 146 |
context = retrieve_knowledge(user_input)
|
|
@@ -152,7 +153,7 @@ def generate_response(user_input, history, mode="chat"):
|
|
| 152 |
resp = client.chat.completions.create(
|
| 153 |
model=MODEL_ID,
|
| 154 |
messages=messages,
|
| 155 |
-
temperature=0.0, # deterministic for knowledge-only
|
| 156 |
max_tokens=1200,
|
| 157 |
)
|
| 158 |
return resp.choices[0].message.content.strip()
|
|
@@ -164,10 +165,8 @@ def generate_response(user_input, history, mode="chat"):
|
|
| 164 |
# 7. Chat Logic
|
| 165 |
# ---------------------------
|
| 166 |
def chat_with_model(user_message, chat_history):
|
| 167 |
-
"""Maintains full conversational context and returns updated chat history."""
|
| 168 |
if not user_message:
|
| 169 |
return chat_history, ""
|
| 170 |
-
|
| 171 |
if chat_history is None:
|
| 172 |
chat_history = []
|
| 173 |
|
|
@@ -176,7 +175,6 @@ def chat_with_model(user_message, chat_history):
|
|
| 176 |
for m in chat_history
|
| 177 |
if isinstance(m, dict) and "role" in m
|
| 178 |
]
|
| 179 |
-
|
| 180 |
history.append({"role": "user", "content": user_message})
|
| 181 |
|
| 182 |
try:
|
|
@@ -192,7 +190,7 @@ def reset_chat():
|
|
| 192 |
return []
|
| 193 |
|
| 194 |
# ---------------------------
|
| 195 |
-
# 8. Gradio
|
| 196 |
# ---------------------------
|
| 197 |
def build_ui():
|
| 198 |
with gr.Blocks(
|
|
@@ -212,7 +210,6 @@ def build_ui():
|
|
| 212 |
button.secondary:hover { background-color: #e5e7eb !important; }
|
| 213 |
"""
|
| 214 |
) as demo:
|
| 215 |
-
|
| 216 |
chatbot = gr.Chatbot(
|
| 217 |
label="💠 OhamLab Conversation",
|
| 218 |
height=520,
|
|
@@ -223,7 +220,7 @@ def build_ui():
|
|
| 223 |
|
| 224 |
with gr.Row():
|
| 225 |
msg = gr.Textbox(
|
| 226 |
-
placeholder="Ask OhamLab anything
|
| 227 |
lines=3,
|
| 228 |
show_label=False,
|
| 229 |
scale=12,
|
|
@@ -237,7 +234,6 @@ def build_ui():
|
|
| 237 |
send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 238 |
msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 239 |
clear.click(reset_chat, outputs=chatbot)
|
| 240 |
-
|
| 241 |
return demo
|
| 242 |
|
| 243 |
# ---------------------------
|
|
|
|
| 1 |
"""
|
| 2 |
+
OhamLab — AI Intelligence
|
| 3 |
Loads knowledge from rahul7star/OhamLab-LLM markdown corpus, caches embeddings,
|
| 4 |
and provides retrieval-augmented chat through Hugging Face router.
|
| 5 |
"""
|
|
|
|
| 25 |
if not HF_TOKEN:
|
| 26 |
raise RuntimeError("❌ Missing HF_TOKEN / OPENAI_API_KEY / HUGGINGFACE_TOKEN environment variable.")
|
| 27 |
|
| 28 |
+
MODEL_ID = "openai/gpt-oss-20b" # Chat model (via HF router)
|
| 29 |
+
EMBED_MODEL = "BAAI/bge-small-en-v1.5" # Router-compatible embedding model
|
| 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)
|
|
|
|
| 56 |
chunks.append({"file": f, "text": buf.strip()})
|
| 57 |
except Exception as e:
|
| 58 |
print(f"⚠️ Failed to load {f}: {e}")
|
| 59 |
+
print(f"📚 Loaded {len(chunks)} text chunks from {len(md_files)} markdown files.")
|
| 60 |
return chunks
|
| 61 |
|
| 62 |
# ---------------------------
|
| 63 |
+
# 3. Embeddings (with Cache & Retry)
|
| 64 |
# ---------------------------
|
| 65 |
def create_embeddings_with_retry(texts, retries=3, delay=2):
|
| 66 |
"""Generate embeddings with retries on failure."""
|
|
|
|
| 122 |
return ""
|
| 123 |
|
| 124 |
# ---------------------------
|
| 125 |
+
# 5. System Prompt
|
| 126 |
# ---------------------------
|
| 127 |
def build_system_prompt(context: str, mode: str = "chat") -> str:
|
| 128 |
return textwrap.dedent(f"""
|
|
|
|
| 130 |
|
| 131 |
Guidelines:
|
| 132 |
- Only answer using information retrieved from the OhamLab knowledge bank (.md files).
|
| 133 |
+
- Do NOT hallucinate. If the answer is not found, respond: "I could not find an answer in my knowledge base."
|
| 134 |
+
- Always answer with clarity, precision, and factual grounding.
|
| 135 |
- Avoid code unless explicitly requested.
|
| 136 |
- Mode: {mode.upper()}
|
| 137 |
|
|
|
|
| 141 |
""").strip()
|
| 142 |
|
| 143 |
# ---------------------------
|
| 144 |
+
# 6. Response Generation
|
| 145 |
# ---------------------------
|
| 146 |
def generate_response(user_input, history, mode="chat"):
|
| 147 |
context = retrieve_knowledge(user_input)
|
|
|
|
| 153 |
resp = client.chat.completions.create(
|
| 154 |
model=MODEL_ID,
|
| 155 |
messages=messages,
|
| 156 |
+
temperature=0.0, # deterministic for knowledge-only answers
|
| 157 |
max_tokens=1200,
|
| 158 |
)
|
| 159 |
return resp.choices[0].message.content.strip()
|
|
|
|
| 165 |
# 7. Chat Logic
|
| 166 |
# ---------------------------
|
| 167 |
def chat_with_model(user_message, chat_history):
|
|
|
|
| 168 |
if not user_message:
|
| 169 |
return chat_history, ""
|
|
|
|
| 170 |
if chat_history is None:
|
| 171 |
chat_history = []
|
| 172 |
|
|
|
|
| 175 |
for m in chat_history
|
| 176 |
if isinstance(m, dict) and "role" in m
|
| 177 |
]
|
|
|
|
| 178 |
history.append({"role": "user", "content": user_message})
|
| 179 |
|
| 180 |
try:
|
|
|
|
| 190 |
return []
|
| 191 |
|
| 192 |
# ---------------------------
|
| 193 |
+
# 8. Gradio UI
|
| 194 |
# ---------------------------
|
| 195 |
def build_ui():
|
| 196 |
with gr.Blocks(
|
|
|
|
| 210 |
button.secondary:hover { background-color: #e5e7eb !important; }
|
| 211 |
"""
|
| 212 |
) as demo:
|
|
|
|
| 213 |
chatbot = gr.Chatbot(
|
| 214 |
label="💠 OhamLab Conversation",
|
| 215 |
height=520,
|
|
|
|
| 220 |
|
| 221 |
with gr.Row():
|
| 222 |
msg = gr.Textbox(
|
| 223 |
+
placeholder="Ask OhamLab anything ...",
|
| 224 |
lines=3,
|
| 225 |
show_label=False,
|
| 226 |
scale=12,
|
|
|
|
| 234 |
send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 235 |
msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 236 |
clear.click(reset_chat, outputs=chatbot)
|
|
|
|
| 237 |
return demo
|
| 238 |
|
| 239 |
# ---------------------------
|