dxnxk commited on
Commit
b46b89c
·
1 Parent(s): 76b40e0

fix HF Gradio error

Browse files
Files changed (1) hide show
  1. app.py +8 -11
app.py CHANGED
@@ -31,15 +31,13 @@ else:
31
  # --- Inference API client ---
32
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=os.getenv("HF_TOKEN"))
33
 
34
- def respond(message, history: list[dict]):
35
- # 1. encode query and retrieve context
36
  query_embedding = embedding_model.encode([message], convert_to_numpy=True)
37
  faiss.normalize_L2(query_embedding)
38
  _, indices = index.search(query_embedding, k=5)
39
 
40
  context = "\n".join([f"{codes[i]}: {descriptions[i]}" for i in indices[0]])
41
 
42
- # 2. prepare system prompt with role + retrieved context
43
  system_prompt = f"""You are an expert assistant specialized in tariff classification.
44
  Your job is to help users find the most appropriate tariff codes based on their description.
45
  Use only the provided context below to answer.
@@ -48,23 +46,22 @@ Context:
48
  {context}
49
  """
50
 
51
- # 3. insert system message at the beginning
52
  messages = [{"role": "system", "content": system_prompt}]
53
  messages += history + [{"role": "user", "content": message}]
54
 
55
- response = {"role": "assistant", "content": ""}
56
-
57
- for message in client.chat_completion(
58
  messages,
59
  max_tokens=512,
60
  stream=True,
61
  temperature=0.7,
62
  top_p=0.95,
63
  ):
64
- token = message.choices[0].delta.content
65
- response["content"] += token
66
- formatted = response["content"].replace("\n", "\n\n")
67
- yield {"role": "assistant", "content": formatted}
 
68
 
69
  demo = gr.ChatInterface(
70
  respond, type="messages",
 
31
  # --- Inference API client ---
32
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=os.getenv("HF_TOKEN"))
33
 
34
+ def respond(message, history):
 
35
  query_embedding = embedding_model.encode([message], convert_to_numpy=True)
36
  faiss.normalize_L2(query_embedding)
37
  _, indices = index.search(query_embedding, k=5)
38
 
39
  context = "\n".join([f"{codes[i]}: {descriptions[i]}" for i in indices[0]])
40
 
 
41
  system_prompt = f"""You are an expert assistant specialized in tariff classification.
42
  Your job is to help users find the most appropriate tariff codes based on their description.
43
  Use only the provided context below to answer.
 
46
  {context}
47
  """
48
 
 
49
  messages = [{"role": "system", "content": system_prompt}]
50
  messages += history + [{"role": "user", "content": message}]
51
 
52
+ full_response = ""
53
+ for chunk in client.chat_completion(
 
54
  messages,
55
  max_tokens=512,
56
  stream=True,
57
  temperature=0.7,
58
  top_p=0.95,
59
  ):
60
+ token = chunk.choices[0].delta.content
61
+ if token:
62
+ full_response += token
63
+ yield full_response.replace("\n", "\n\n")
64
+
65
 
66
  demo = gr.ChatInterface(
67
  respond, type="messages",