sailajaai commited on
Commit
35edcc9
·
verified ·
1 Parent(s): c61681f

Update services/huggingface_api.py

Browse files
Files changed (1) hide show
  1. services/huggingface_api.py +25 -66
services/huggingface_api.py CHANGED
@@ -1,96 +1,55 @@
1
  import os
2
- import time
3
  import logging
4
- import requests
5
- from openai import OpenAI
6
  import numpy as np
7
 
8
  logger = logging.getLogger(__name__)
9
 
10
- HF_API_KEY = os.getenv('HF_API_KEY')
11
  if not HF_API_KEY:
12
  raise ValueError("HF_API_KEY not set")
13
 
14
- HF_HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
15
- HF_BASE = "https://api-inference.huggingface.co"
 
 
16
 
17
- def hf_post(url, payload, retry=3):
18
- for attempt in range(retry + 1):
19
- try:
20
- resp = requests.post(url, headers=HF_HEADERS, json=payload, timeout=60)
21
- if resp.status_code == 200:
22
- return resp.json()
23
- else:
24
- logger.warning(f"HF call to {url} status {resp.status_code} attempt {attempt}: {resp.text}")
25
- time.sleep(2 ** attempt)
26
- except requests.exceptions.RequestException as e:
27
- logger.warning(f"HF request to {url} failed attempt {attempt}: {e}")
28
- time.sleep(2 ** attempt)
29
- raise Exception(f"Hugging Face API failed for {url}: {resp.status_code} {resp.text}")
30
-
31
- # -------------------------
32
- # Embeddings
33
- # -------------------------
34
  def get_embeddings(model, text):
35
- """
36
- Calls HF feature-extraction to get embeddings for a single text.
37
- Returns 1D list of floats.
38
- """
39
- url = f"{HF_BASE}/pipeline/feature-extraction/{model}"
40
- payload = {"inputs": text, "options": {"wait_for_model": True}}
41
  try:
42
- out = hf_post(url, payload)
43
- # Handle response: list of token embeddings for the input text
 
44
  if isinstance(out, list) and len(out) > 0:
45
- candidate = out[0]
46
- if isinstance(candidate, list) and len(candidate) > 0:
47
- # Average token embeddings to get sentence embedding
48
- arr = np.array(candidate)
49
- vec = arr.mean(axis=0).tolist()
50
- return vec
51
- else:
52
- raise Exception(f"Unexpected embeddings format: {candidate}")
53
  else:
54
- raise Exception(f"Unexpected HF response: {out}")
55
  except Exception as e:
56
  logger.exception(f"Failed to get embeddings for {model}: {e}")
57
  raise
58
 
59
- # -------------------------
60
- # Cross Encoder
61
- # -------------------------
62
  def get_cross_encoder_score(model, student, teacher):
63
- """
64
- Obtain a similarity/score between student and teacher.
65
- Returns float between 0 and 1.
66
- """
67
- url = f"{HF_BASE}/pipeline/sentence-similarity/{model}"
68
- payload = {"inputs": {"source_sentence": student, "sentences": [teacher]}}
69
  try:
70
- out = hf_post(url, payload)
 
 
 
 
 
71
  if isinstance(out, list) and len(out) > 0 and isinstance(out[0], (float, int)):
72
  return float(out[0])
73
- raise Exception(f"Unexpected cross-encoder response: {out}")
74
  except Exception as e:
75
  logger.exception(f"Failed cross-encoder for {model}: {e}")
76
- # Fallback to default model endpoint
77
- url = f"{HF_BASE}/{model}"
78
- payload = {"inputs": [student, teacher]}
79
- out = hf_post(url, payload)
80
- if isinstance(out, list) and len(out) > 0:
81
- return float(out[0]) if isinstance(out[0], (float, int)) else float(out[0].get("score", 0.0))
82
- raise Exception(f"Unable to parse cross-encoder output: {out}")
83
-
84
- # -------------------------
85
- # Feedback Generation
86
- # -------------------------
87
- # ... (unchanged)
88
 
89
- # -------------------------
90
- # Feedback Generation
91
- # -------------------------
92
  def generate_feedback(question, answer, score, model="meta-llama/Llama-3.2-1B-Instruct:novita"):
93
  try:
 
94
  client = OpenAI(
95
  base_url="https://router.huggingface.co/v1",
96
  api_key=os.getenv("HF_API_KEY"),
 
1
  import os
 
2
  import logging
3
+ from huggingface_hub import InferenceClient
 
4
  import numpy as np
5
 
6
  logger = logging.getLogger(__name__)
7
 
8
+ HF_API_KEY = os.getenv("HF_API_KEY")
9
  if not HF_API_KEY:
10
  raise ValueError("HF_API_KEY not set")
11
 
12
+ client = InferenceClient(
13
+ provider="hf-inference",
14
+ api_key=HF_API_KEY,
15
+ )
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def get_embeddings(model, text):
18
+ if not text or not isinstance(text, str):
19
+ logger.error(f"Invalid input for embeddings: {text}")
20
+ raise ValueError("Text must be a non-empty string")
 
 
 
21
  try:
22
+ # Feature extraction returns a list of token embeddings
23
+ out = client.feature_extraction(text, model=model)
24
+ logger.debug(f"HF feature extraction response: {out}")
25
  if isinstance(out, list) and len(out) > 0:
26
+ arr = np.array(out[0]) # Take the first sequence's embeddings
27
+ vec = arr.mean(axis=0).tolist() # Average token embeddings
28
+ return vec
 
 
 
 
 
29
  else:
30
+ raise Exception(f"Unexpected feature extraction response: {out}")
31
  except Exception as e:
32
  logger.exception(f"Failed to get embeddings for {model}: {e}")
33
  raise
34
 
 
 
 
35
  def get_cross_encoder_score(model, student, teacher):
 
 
 
 
 
 
36
  try:
37
+ # Sentence similarity expects a dict with source_sentence and sentences
38
+ out = client.sentence_similarity(
39
+ {"source_sentence": student, "sentences": [teacher]},
40
+ model=model
41
+ )
42
+ logger.debug(f"HF sentence similarity response: {out}")
43
  if isinstance(out, list) and len(out) > 0 and isinstance(out[0], (float, int)):
44
  return float(out[0])
45
+ raise Exception(f"Unexpected sentence similarity response: {out}")
46
  except Exception as e:
47
  logger.exception(f"Failed cross-encoder for {model}: {e}")
48
+ raise
 
 
 
 
 
 
 
 
 
 
 
49
 
 
 
 
50
  def generate_feedback(question, answer, score, model="meta-llama/Llama-3.2-1B-Instruct:novita"):
51
  try:
52
+ from openai import OpenAI
53
  client = OpenAI(
54
  base_url="https://router.huggingface.co/v1",
55
  api_key=os.getenv("HF_API_KEY"),