gk2410 commited on
Commit
4b8bfab
·
verified ·
1 Parent(s): 2901bbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -21
app.py CHANGED
@@ -7,8 +7,8 @@ from sentence_transformers import SentenceTransformer
7
  from huggingface_hub import InferenceClient
8
 
9
  # 1. INITIALIZE MODELS
10
- # Embedder (CPU) and Llama 3.3 (API)
11
  embedder = SentenceTransformer('all-MiniLM-L6-v2')
 
12
  client = InferenceClient("meta-llama/Llama-3.3-70B-Instruct", token=os.getenv("HF_TOKEN"))
13
 
14
  def fetch_and_index(query):
@@ -20,13 +20,11 @@ def fetch_and_index(query):
20
 
21
  if not data: return None, None
22
 
23
- # Extract strictly Title and Author
24
  catalog = [
25
  f"{d.get('title')} by {', '.join(d.get('author_name', ['Unknown Author']))}"
26
  for d in data
27
  ]
28
 
29
- # Build Vector Index
30
  embeddings = embedder.encode(catalog)
31
  index = faiss.IndexFlatL2(embeddings.shape[1])
32
  index.add(np.array(embeddings).astype('float32'))
@@ -36,7 +34,6 @@ def fetch_and_index(query):
36
  return None, None
37
 
38
  def librarian_logic(message, history, user_state):
39
- # Initialize State if empty
40
  if user_state is None:
41
  user_state = {"step": "ASK_AGE", "age": None, "location": None}
42
 
@@ -45,32 +42,28 @@ def librarian_logic(message, history, user_state):
45
  if message.isdigit():
46
  user_state["age"] = int(message)
47
  user_state["step"] = "ASK_LOCATION"
48
- reply = "Got it. For regional safety compliance, what is your general location (City/Country)?"
49
  return history + [[message, reply]], user_state, ""
50
-
51
- reply = "Welcome! I am your AI Librarian. To begin safely, how old are you?"
52
- return history + [[message, reply]], user_state, ""
53
 
54
  if user_state["step"] == "ASK_LOCATION":
55
  user_state["location"] = message
56
  user_state["step"] = "SEARCH_READY"
57
- reply = f"Verification complete for {user_state['location']}. How can I help you find a book today?"
58
  return history + [[message, reply]], user_state, ""
59
 
60
- # --- PHASE 2: SEARCH & LLM ACTION ---
61
  index, catalog = fetch_and_index(message)
62
- context = "\n- ".join(catalog[:3]) if catalog else "No books found in live search."
63
 
64
- # Constructing the DICT for the model (The fix for your error)
65
- safety_context = "User is under 13; ensure recommendations are child-safe." if user_state["age"] < 13 else ""
66
-
67
  messages = [
68
- {"role": "system", "content": "You are a professional librarian. Use the context provided to recommend titles and authors."},
69
- {"role": "user", "content": f"Context: {context}\nSafety: {safety_context}\nQuery: {message}"}
70
  ]
71
 
72
  response = ""
73
- # Important: Named argument 'messages' is used here
74
  for msg in client.chat_completion(
75
  messages=messages,
76
  max_tokens=300,
@@ -81,15 +74,14 @@ def librarian_logic(message, history, user_state):
81
  response += token
82
  yield history + [[message, response]], user_state, ""
83
 
84
- # --- GRADIO UI ---
85
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
86
- gr.Markdown("# 📚 AI Librarian Agent\n*Live Web Search + Semantic FAISS Ranking*")
87
 
88
  user_state = gr.State()
89
  chatbot = gr.Chatbot()
90
- msg = gr.Textbox(label="Your Input", placeholder="Enter age first...")
91
 
92
- # Logic link
93
  msg.submit(librarian_logic, [msg, chatbot, user_state], [chatbot, user_state, msg])
94
 
95
  if __name__ == "__main__":
 
7
  from huggingface_hub import InferenceClient
8
 
9
  # 1. INITIALIZE MODELS
 
10
  embedder = SentenceTransformer('all-MiniLM-L6-v2')
11
+ # Ensure you have HF_TOKEN in your Space Secrets!
12
  client = InferenceClient("meta-llama/Llama-3.3-70B-Instruct", token=os.getenv("HF_TOKEN"))
13
 
14
  def fetch_and_index(query):
 
20
 
21
  if not data: return None, None
22
 
 
23
  catalog = [
24
  f"{d.get('title')} by {', '.join(d.get('author_name', ['Unknown Author']))}"
25
  for d in data
26
  ]
27
 
 
28
  embeddings = embedder.encode(catalog)
29
  index = faiss.IndexFlatL2(embeddings.shape[1])
30
  index.add(np.array(embeddings).astype('float32'))
 
34
  return None, None
35
 
36
  def librarian_logic(message, history, user_state):
 
37
  if user_state is None:
38
  user_state = {"step": "ASK_AGE", "age": None, "location": None}
39
 
 
42
  if message.isdigit():
43
  user_state["age"] = int(message)
44
  user_state["step"] = "ASK_LOCATION"
45
+ reply = "Got it. For safety compliance, what is your general location (City/Country)?"
46
  return history + [[message, reply]], user_state, ""
47
+ return history + [[message, "How old are you?"]], user_state, ""
 
 
48
 
49
  if user_state["step"] == "ASK_LOCATION":
50
  user_state["location"] = message
51
  user_state["step"] = "SEARCH_READY"
52
+ reply = f"System verified for {user_state['location']}. What book can I find for you?"
53
  return history + [[message, reply]], user_state, ""
54
 
55
+ # --- PHASE 2: SEARCH & LLM ---
56
  index, catalog = fetch_and_index(message)
57
+ context = "\n- ".join(catalog[:3]) if catalog else "No books found."
58
 
59
+ # THE FIX: This specific list-of-dicts format
 
 
60
  messages = [
61
+ {"role": "system", "content": "You are a professional librarian. Respond only with book titles and authors based on the provided context."},
62
+ {"role": "user", "content": f"User Age: {user_state['age']}. Context: {context}. Query: {message}"}
63
  ]
64
 
65
  response = ""
66
+ # Explicitly use messages=messages
67
  for msg in client.chat_completion(
68
  messages=messages,
69
  max_tokens=300,
 
74
  response += token
75
  yield history + [[message, response]], user_state, ""
76
 
77
+ # --- UI SETUP ---
78
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
79
+ gr.Markdown("# 📚 AI Librarian Agent\n*Search for books by title or author.*")
80
 
81
  user_state = gr.State()
82
  chatbot = gr.Chatbot()
83
+ msg = gr.Textbox(label="Your Input", placeholder="Type age first...")
84
 
 
85
  msg.submit(librarian_logic, [msg, chatbot, user_state], [chatbot, user_state, msg])
86
 
87
  if __name__ == "__main__":