chenglu commited on
Commit
aff403e
1 Parent(s): 03295d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -26,12 +26,22 @@ hf_token = os.getenv('HF_TOKEN')
26
  model_id = "BAAI/bge-large-en-v1.5"
27
  feature_extraction_pipeline = pipeline("feature-extraction", model=model_id)
28
 
29
-
30
  # model_id = "BAAI/bge-large-en-v1.5"
31
  # api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_id}"
32
  # headers = {"Authorization": f"Bearer {hf_token}"}
33
 
34
- # @retry(tries=3, delay=10)
 
 
 
 
 
 
 
 
 
 
 
35
  # def query(texts):
36
  # response = requests.post(api_url, headers=headers, json={"inputs": texts})
37
  # if response.status_code == 200:
@@ -79,8 +89,8 @@ def get_tags_for_local(dataset, local_value):
79
  def gradio_query_interface(input_text):
80
  cleaned_text = clean_content(input_text)
81
  no_stopwords_text = remove_stopwords(cleaned_text)
82
- # new_embedding = query(no_stopwords_text)
83
- new_embedding = feature_extraction_pipeline(input_text)
84
  query_embeddings = torch.FloatTensor(new_embedding)
85
  hits = util.semantic_search(query_embeddings, dataset_embeddings, top_k=5)
86
  if all(hit['score'] < 0.6 for hit in hits[0]):
 
26
  model_id = "BAAI/bge-large-en-v1.5"
27
  feature_extraction_pipeline = pipeline("feature-extraction", model=model_id)
28
 
 
29
  # model_id = "BAAI/bge-large-en-v1.5"
30
  # api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_id}"
31
  # headers = {"Authorization": f"Bearer {hf_token}"}
32
 
33
+ @retry(tries=3, delay=10)
34
+ def query(texts):
35
+ # 使用特征提取管道获取特征
36
+ features = feature_extraction_pipeline(texts)
37
+
38
+ # 将特征降维成二维张量(如果它们不是)
39
+ # 假设 features 是一个列表,每个元素是一个句子的特征
40
+ embeddings = [torch.tensor(f).mean(dim=0) for f in features]
41
+ embeddings = torch.stack(embeddings)
42
+
43
+ return embeddings
44
+
45
  # def query(texts):
46
  # response = requests.post(api_url, headers=headers, json={"inputs": texts})
47
  # if response.status_code == 200:
 
89
  def gradio_query_interface(input_text):
90
  cleaned_text = clean_content(input_text)
91
  no_stopwords_text = remove_stopwords(cleaned_text)
92
+ new_embedding = query(no_stopwords_text)
93
+ # new_embedding = feature_extraction_pipeline(input_text)
94
  query_embeddings = torch.FloatTensor(new_embedding)
95
  hits = util.semantic_search(query_embeddings, dataset_embeddings, top_k=5)
96
  if all(hit['score'] < 0.6 for hit in hits[0]):