masadonline commited on
Commit
3cdab77
Β·
verified Β·
1 Parent(s): d899598

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -44,7 +44,7 @@ def load_json_orders(json_file):
44
  if isinstance(data, list):
45
  for i, order in enumerate(data):
46
  try:
47
- json.dumps(order) # attempt serialization
48
  valid_orders.append(order)
49
  except Exception as e:
50
  st.warning(f"⚠️ Skipping invalid order at index {i}: {e}")
@@ -59,7 +59,6 @@ def load_json_orders(json_file):
59
  st.error(f"❌ Error parsing JSON file: {e}")
60
  return valid_orders
61
 
62
-
63
  def build_index(text_chunks):
64
  vectors = embedder.encode(text_chunks)
65
  index = faiss.IndexFlatL2(vectors.shape[1])
@@ -67,11 +66,20 @@ def build_index(text_chunks):
67
  return index, text_chunks
68
 
69
  def ask_llm(context, query):
70
- prompt = f"You are a helpful assistant for an online toy shop.\n\nKnowledge base:\n{context}\n\nQuestion: {query}"
 
 
 
 
 
 
 
71
  response = client.chat.completions.create(
72
  model=LLM_MODEL,
73
  messages=[{"role": "user", "content": prompt}]
74
  )
 
 
75
  return response.choices[0].message.content.strip()
76
 
77
  # --- File upload section ---
@@ -90,21 +98,24 @@ if orders_file:
90
  if orders:
91
  order_chunks = [json.dumps(order, ensure_ascii=False) for order in orders]
92
  st.success(f"βœ… Loaded {len(order_chunks)} customer order records.")
93
-
94
- # Try to flatten for DataFrame view
95
  try:
96
  df = pd.json_normalize(orders)
97
  st.dataframe(df, use_container_width=True)
98
  except Exception:
99
  st.warning("⚠️ Nested JSON detected. Showing raw JSON preview instead.")
100
  st.json(orders)
 
 
101
 
102
  # --- Process PDFs ---
103
  if pdf_files:
104
  for pdf_file in pdf_files:
105
  try:
106
  text = extract_pdf_text(pdf_file)
107
- pdf_chunks.extend(text.split("\n\n")) # paragraph-wise
 
 
108
  st.success(f"πŸ“„ Processed {pdf_file.name}")
109
  except Exception as e:
110
  st.error(f"❌ Failed to read {pdf_file.name}: {e}")
@@ -121,12 +132,15 @@ if combined_chunks:
121
  if user_query:
122
  query_vector = embedder.encode([user_query])
123
  D, I = index.search(query_vector, k=5)
 
124
  context = "\n---\n".join([sources[i] for i in I[0]])
 
125
 
126
  with st.spinner("πŸ€” Thinking..."):
127
  try:
128
  answer = ask_llm(context, user_query)
129
  st.markdown("### 🧠 Answer")
 
130
  st.write(answer)
131
  except Exception as e:
132
  st.error(f"❌ GROQ API Error: {e}")
 
44
  if isinstance(data, list):
45
  for i, order in enumerate(data):
46
  try:
47
+ json.dumps(order) # test serialization
48
  valid_orders.append(order)
49
  except Exception as e:
50
  st.warning(f"⚠️ Skipping invalid order at index {i}: {e}")
 
59
  st.error(f"❌ Error parsing JSON file: {e}")
60
  return valid_orders
61
 
 
62
  def build_index(text_chunks):
63
  vectors = embedder.encode(text_chunks)
64
  index = faiss.IndexFlatL2(vectors.shape[1])
 
66
  return index, text_chunks
67
 
68
  def ask_llm(context, query):
69
+ prompt = (
70
+ f"You are a helpful assistant for an online toy shop.\n\n"
71
+ f"Knowledge base:\n{context}\n\n"
72
+ f"Question: {query}"
73
+ )
74
+ # For debugging: show the prompt being sent.
75
+ st.expander("Prompt to LLM").code(prompt)
76
+
77
  response = client.chat.completions.create(
78
  model=LLM_MODEL,
79
  messages=[{"role": "user", "content": prompt}]
80
  )
81
+ # Log full response for inspection (can be commented out in production)
82
+ st.expander("Raw LLM API Response").json(response)
83
  return response.choices[0].message.content.strip()
84
 
85
  # --- File upload section ---
 
98
  if orders:
99
  order_chunks = [json.dumps(order, ensure_ascii=False) for order in orders]
100
  st.success(f"βœ… Loaded {len(order_chunks)} customer order records.")
101
+ # Attempt to flatten for viewing
 
102
  try:
103
  df = pd.json_normalize(orders)
104
  st.dataframe(df, use_container_width=True)
105
  except Exception:
106
  st.warning("⚠️ Nested JSON detected. Showing raw JSON preview instead.")
107
  st.json(orders)
108
+ else:
109
+ st.error("No valid orders found in the JSON file.")
110
 
111
  # --- Process PDFs ---
112
  if pdf_files:
113
  for pdf_file in pdf_files:
114
  try:
115
  text = extract_pdf_text(pdf_file)
116
+ # Split into paragraphs (non-empty lines)
117
+ paragraphs = [p.strip() for p in text.split("\n\n") if p.strip()]
118
+ pdf_chunks.extend(paragraphs)
119
  st.success(f"πŸ“„ Processed {pdf_file.name}")
120
  except Exception as e:
121
  st.error(f"❌ Failed to read {pdf_file.name}: {e}")
 
132
  if user_query:
133
  query_vector = embedder.encode([user_query])
134
  D, I = index.search(query_vector, k=5)
135
+ # Prepare context from the top-K results:
136
  context = "\n---\n".join([sources[i] for i in I[0]])
137
+ st.expander("Combined Context").code(context)
138
 
139
  with st.spinner("πŸ€” Thinking..."):
140
  try:
141
  answer = ask_llm(context, user_query)
142
  st.markdown("### 🧠 Answer")
143
+ # Use st.write() to render the answer as text.
144
  st.write(answer)
145
  except Exception as e:
146
  st.error(f"❌ GROQ API Error: {e}")