rahul7star commited on
Commit
489075d
Β·
verified Β·
1 Parent(s): 3cca735

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -70
app.py CHANGED
@@ -1,52 +1,52 @@
1
- import os
2
- import time
3
- import json
4
- import numpy as np
5
- import logging
6
  from typing import List
7
  from huggingface_hub import HfApi, hf_hub_download, list_repo_files
 
8
  from openai import OpenAI
9
 
10
- # ---------------------------
11
  # Logging setup
12
- # ---------------------------
13
  logging.basicConfig(level=logging.INFO)
14
  logger = logging.getLogger("ohamlab_agent")
15
 
16
  # ---------------------------
17
- # Config
18
  # ---------------------------
19
- HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("OPENAI_API_KEY") or os.environ.get("HUGGINGFACE_TOKEN")
 
 
 
 
20
  if not HF_TOKEN:
21
- logger.critical("Missing HF_TOKEN / OPENAI_API_KEY / HUGGINGFACE_TOKEN environment variable.")
22
- raise RuntimeError("ERROR: set env var HF_TOKEN or OPENAI_API_KEY with your Hugging Face / Router token.")
23
 
24
- MODEL_ID = "openai/gpt-oss-20b" # chat model via HF router
25
- EMBED_MODEL = "text-embedding-3-small"
26
 
27
  HF_REPO = "rahul7star/OhamLab-LLM"
28
- HF_REPO_DIR = "./hf_capsules" # local cache
29
  os.makedirs(HF_REPO_DIR, exist_ok=True)
30
 
31
  # ---------------------------
32
- # Client (OpenAI router via HF)
33
  # ---------------------------
34
  try:
35
  client = OpenAI(base_url="https://router.huggingface.co/v1", api_key=HF_TOKEN)
36
- logger.info("βœ… OpenAI client initialized via HF router.")
37
  except Exception as e:
38
- logger.exception("❌ Failed initializing OpenAI client: %s", e)
39
  raise
40
 
 
 
 
41
  # ---------------------------
42
- # Knowledge Loader
43
  # ---------------------------
44
  def load_markdown_files(repo_id: str, local_dir: str) -> List[str]:
45
- """Downloads all .md files from Hugging Face repo."""
46
  api = HfApi(token=HF_TOKEN)
47
  files = list_repo_files(repo_id, repo_type="model", token=HF_TOKEN)
48
  md_files = [f for f in files if f.endswith(".md")]
49
- logger.info(f"πŸ“˜ Found {len(md_files)} markdown files in {repo_id}")
50
 
51
  chunks = []
52
  for f in md_files:
@@ -54,7 +54,6 @@ def load_markdown_files(repo_id: str, local_dir: str) -> List[str]:
54
  path = hf_hub_download(repo_id=repo_id, filename=f, local_dir=local_dir, token=HF_TOKEN)
55
  with open(path, "r", encoding="utf-8") as fh:
56
  content = fh.read()
57
- # Split into 400–600 char segments
58
  buf = ""
59
  for line in content.splitlines():
60
  buf += line.strip() + " "
@@ -64,83 +63,55 @@ def load_markdown_files(repo_id: str, local_dir: str) -> List[str]:
64
  if buf:
65
  chunks.append(buf.strip())
66
  except Exception as e:
67
- logger.warning(f"⚠️ Failed loading {f}: {e}")
68
- logger.info(f"βœ… Loaded {len(chunks)} knowledge chunks.")
69
  return chunks
70
 
71
- def get_embeddings(texts: List[str]) -> np.ndarray:
72
- """Batch embed text list."""
73
- if not texts:
74
- return np.zeros((1, 1536))
75
- res = client.embeddings.create(model=EMBED_MODEL, input=texts)
76
- return np.array([r.embedding for r in res.data])
77
-
78
- # ---------------------------
79
- # Load knowledge and embeddings
80
- # ---------------------------
81
- logger.info("πŸ” Loading OhamLab knowledge base...")
82
  KNOWLEDGE_CHUNKS = load_markdown_files(HF_REPO, HF_REPO_DIR)
83
- logger.info("πŸ“Š Generating embeddings...")
84
- KNOWLEDGE_EMBS = get_embeddings(KNOWLEDGE_CHUNKS)
85
  logger.info(f"🧠 Knowledge base ready ({len(KNOWLEDGE_CHUNKS)} chunks).")
86
 
87
  # ---------------------------
88
- # Retrieval helper
89
  # ---------------------------
90
  def get_relevant_context(query: str, top_k: int = 3) -> str:
91
- if not KNOWLEDGE_CHUNKS or not query:
92
- return ""
93
- q_emb = get_embeddings([query])[0]
94
- sims = np.dot(KNOWLEDGE_EMBS, q_emb) / (
95
- np.linalg.norm(KNOWLEDGE_EMBS, axis=1) * np.linalg.norm(q_emb)
96
- )
97
  top_idx = np.argsort(sims)[-top_k:][::-1]
98
  return "\n\n".join(KNOWLEDGE_CHUNKS[i] for i in top_idx)
99
 
100
  # ---------------------------
101
- # Chat Logic
102
  # ---------------------------
103
  SYSTEM_PROMPT = (
104
  "You are OhamLab AI β€” factual, concise, and context-aware.\n"
105
- "Use OhamLab Markdown knowledge if relevant.\n"
106
- "Never invent information; be clear and professional."
107
  )
108
 
109
  def chat(query: str, history: List[dict]) -> str:
110
  context = get_relevant_context(query)
111
- user_input = f"{query}\n\n[Context]\n{context[:1500]}" if context else query
112
-
113
  msgs = history + [{"role": "user", "content": user_input}]
114
  try:
115
  resp = client.chat.completions.create(
116
- model=MODEL_ID,
117
  messages=msgs,
118
- temperature=0.5,
119
- max_tokens=800,
120
  )
121
  return resp.choices[0].message.content.strip()
122
  except Exception as e:
123
  logger.error(f"Chat error: {e}")
124
- return "I’m having trouble processing that. Please try again."
125
 
126
- # ---------------------------
127
- # Example usage
128
- # ---------------------------
129
  if __name__ == "__main__":
130
- logger.info("πŸš€ Starting OhamLab AI β€” Knowledge Mode")
131
- history = [{"role": "system", "content": SYSTEM_PROMPT}]
132
  while True:
133
- try:
134
- q = input("\n🧠 Ask OhamLab β†’ ").strip()
135
- if not q:
136
- continue
137
- if q.lower() in ("exit", "quit"):
138
- break
139
- ans = chat(q, history)
140
- print("\nπŸ’¬", ans)
141
- history.append({"role": "user", "content": q})
142
- history.append({"role": "assistant", "content": ans})
143
- except KeyboardInterrupt:
144
  break
145
- except Exception as e:
146
- logger.exception(f"Main loop error: {e}")
 
 
1
+ import os, time, json, numpy as np, logging
 
 
 
 
2
  from typing import List
3
  from huggingface_hub import HfApi, hf_hub_download, list_repo_files
4
+ from sentence_transformers import SentenceTransformer
5
  from openai import OpenAI
6
 
 
7
  # Logging setup
 
8
  logging.basicConfig(level=logging.INFO)
9
  logger = logging.getLogger("ohamlab_agent")
10
 
11
  # ---------------------------
12
+ # Environment / Config
13
  # ---------------------------
14
+ HF_TOKEN = (
15
+ os.environ.get("HF_TOKEN")
16
+ or os.environ.get("OPENAI_API_KEY")
17
+ or os.environ.get("HUGGINGFACE_TOKEN")
18
+ )
19
  if not HF_TOKEN:
20
+ raise RuntimeError("Missing HF_TOKEN / OPENAI_API_KEY / HUGGINGFACE_TOKEN.")
 
21
 
22
+ CHAT_MODEL_ID = "openai/gpt-oss-20b" # via Hugging Face router
23
+ EMBED_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
24
 
25
  HF_REPO = "rahul7star/OhamLab-LLM"
26
+ HF_REPO_DIR = "./hf_capsules"
27
  os.makedirs(HF_REPO_DIR, exist_ok=True)
28
 
29
  # ---------------------------
30
+ # Clients
31
  # ---------------------------
32
  try:
33
  client = OpenAI(base_url="https://router.huggingface.co/v1", api_key=HF_TOKEN)
34
+ logger.info("βœ… OpenAI client via Hugging Face router initialized.")
35
  except Exception as e:
36
+ logger.exception("Failed initializing chat client.")
37
  raise
38
 
39
+ embedder = SentenceTransformer(EMBED_MODEL_ID)
40
+ logger.info(f"βœ… Loaded local embedding model: {EMBED_MODEL_ID}")
41
+
42
  # ---------------------------
43
+ # Load Markdown Knowledge
44
  # ---------------------------
45
  def load_markdown_files(repo_id: str, local_dir: str) -> List[str]:
 
46
  api = HfApi(token=HF_TOKEN)
47
  files = list_repo_files(repo_id, repo_type="model", token=HF_TOKEN)
48
  md_files = [f for f in files if f.endswith(".md")]
49
+ logger.info(f"πŸ“˜ Found {len(md_files)} markdown files.")
50
 
51
  chunks = []
52
  for f in md_files:
 
54
  path = hf_hub_download(repo_id=repo_id, filename=f, local_dir=local_dir, token=HF_TOKEN)
55
  with open(path, "r", encoding="utf-8") as fh:
56
  content = fh.read()
 
57
  buf = ""
58
  for line in content.splitlines():
59
  buf += line.strip() + " "
 
63
  if buf:
64
  chunks.append(buf.strip())
65
  except Exception as e:
66
+ logger.warning(f"⚠️ Failed to read {f}: {e}")
67
+ logger.info(f"βœ… Loaded {len(chunks)} text chunks.")
68
  return chunks
69
 
 
 
 
 
 
 
 
 
 
 
 
70
  KNOWLEDGE_CHUNKS = load_markdown_files(HF_REPO, HF_REPO_DIR)
71
+ logger.info("πŸ“Š Creating embeddings...")
72
+ KNOWLEDGE_EMBS = embedder.encode(KNOWLEDGE_CHUNKS, normalize_embeddings=True)
73
  logger.info(f"🧠 Knowledge base ready ({len(KNOWLEDGE_CHUNKS)} chunks).")
74
 
75
  # ---------------------------
76
+ # Retrieval
77
  # ---------------------------
78
  def get_relevant_context(query: str, top_k: int = 3) -> str:
79
+ q_emb = embedder.encode([query], normalize_embeddings=True)[0]
80
+ sims = np.dot(KNOWLEDGE_EMBS, q_emb)
 
 
 
 
81
  top_idx = np.argsort(sims)[-top_k:][::-1]
82
  return "\n\n".join(KNOWLEDGE_CHUNKS[i] for i in top_idx)
83
 
84
  # ---------------------------
85
+ # Chat
86
  # ---------------------------
87
  SYSTEM_PROMPT = (
88
  "You are OhamLab AI β€” factual, concise, and context-aware.\n"
89
+ "If applicable, use knowledge from OhamLab Markdown corpus."
 
90
  )
91
 
92
  def chat(query: str, history: List[dict]) -> str:
93
  context = get_relevant_context(query)
94
+ user_input = f"{query}\n\n[Context]\n{context[:1200]}" if context else query
 
95
  msgs = history + [{"role": "user", "content": user_input}]
96
  try:
97
  resp = client.chat.completions.create(
98
+ model=CHAT_MODEL_ID,
99
  messages=msgs,
100
+ temperature=0.6,
101
+ max_tokens=700,
102
  )
103
  return resp.choices[0].message.content.strip()
104
  except Exception as e:
105
  logger.error(f"Chat error: {e}")
106
+ return "There was a problem generating the response."
107
 
 
 
 
108
  if __name__ == "__main__":
109
+ logger.info("πŸš€ OhamLab AI β€” Knowledge Chat Ready")
110
+ hist = [{"role": "system", "content": SYSTEM_PROMPT}]
111
  while True:
112
+ q = input("\nπŸ’¬ Ask β†’ ").strip()
113
+ if q.lower() in ["exit", "quit"]:
 
 
 
 
 
 
 
 
 
114
  break
115
+ ans = chat(q, hist)
116
+ print("\nπŸ€–", ans)
117
+ hist.extend([{"role": "user", "content": q}, {"role": "assistant", "content": ans}])