dxnxk commited on
Commit
c7192ee
·
verified ·
1 Parent(s): c78054d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -26,25 +26,32 @@ client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
26
 
27
  # --- RAG pipeline ---
28
  def generate_answer(messages):
29
- user_query = messages[-1]["content"]
30
- query_embedding = embedding_model.encode([user_query], convert_to_numpy=True)
31
- faiss.normalize_L2(query_embedding)
32
- _, indices = index.search(query_embedding, k=5)
33
-
34
- context = "\n".join([f"{codes[i]}: {descriptions[i]}" for i in indices[0]])
35
- prompt = f"""Here are some tariff code descriptions:
 
36
  {context}
37
 
38
  Question: {user_query}
39
  Answer:"""
40
 
41
- response = client.text_generation(
42
- prompt,
43
- max_new_tokens=200,
44
- temperature=0.7,
45
- stop_sequences=["\n\n"]
46
- )
47
- return {"role": "assistant", "content": response.strip()}
 
 
 
 
 
 
48
 
49
  # --- Gradio Chat Interface ---
50
  gr.ChatInterface(
 
26
 
27
  # --- RAG pipeline ---
28
  def generate_answer(messages):
29
+ try:
30
+ user_query = messages[-1]["content"]
31
+ query_embedding = embedding_model.encode([user_query], convert_to_numpy=True)
32
+ faiss.normalize_L2(query_embedding)
33
+ _, indices = index.search(query_embedding, k=5)
34
+
35
+ context = "\n".join([f"{codes[i]}: {descriptions[i]}" for i in indices[0]])
36
+ prompt = f"""Here are some tariff code descriptions:
37
  {context}
38
 
39
  Question: {user_query}
40
  Answer:"""
41
 
42
+ print("Prompt sent to model:\n", prompt)
43
+
44
+ response = client.text_generation(
45
+ prompt,
46
+ max_new_tokens=200,
47
+ temperature=0.7,
48
+ stop_sequences=["\n\n"]
49
+ )
50
+ return {"role": "assistant", "content": response.strip()}
51
+
52
+ except Exception as e:
53
+ print("Error during inference:", e)
54
+ return {"role": "assistant", "content": "An internal error occurred. Please try again."}
55
 
56
  # --- Gradio Chat Interface ---
57
  gr.ChatInterface(