Spaces:
Running
Running
Johnny
commited on
Commit
·
892f06a
1
Parent(s):
cca9b28
updated gemma endpoint
Browse files
config.py
CHANGED
@@ -1,41 +1,50 @@
|
|
|
|
1 |
import os
|
|
|
|
|
2 |
from dotenv import load_dotenv
|
3 |
from supabase import create_client
|
4 |
-
import
|
5 |
-
import
|
6 |
-
from sentence_transformers import SentenceTransformer # Import the transformer model
|
7 |
|
8 |
-
# Load
|
9 |
load_dotenv()
|
10 |
|
11 |
-
# Supabase
|
12 |
SUPABASE_URL = "https://lmpazoxzucnlqqxjoihi.supabase.co"
|
13 |
SUPABASE_KEY = os.getenv("SUPABASE_API_KEY")
|
14 |
if not SUPABASE_KEY:
|
15 |
raise ValueError("SUPABASE_KEY is not set in the environment variables.")
|
16 |
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
17 |
|
18 |
-
#
|
19 |
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
20 |
|
21 |
-
|
22 |
-
"pegasus": "https://router.huggingface.co/hf-inference/models/google/pegasus-xsum",
|
23 |
-
"gemma": "https://router.huggingface.co/nebius/v1/chat/completions"
|
24 |
-
|
25 |
-
}
|
26 |
-
|
27 |
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
28 |
if not HF_API_TOKEN:
|
29 |
raise ValueError("Missing Hugging Face API key. Check your .env file.")
|
30 |
-
|
31 |
HF_HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def query(payload, model="pegasus", retries=5, delay=5):
|
34 |
"""
|
35 |
-
Sends a request to the Hugging Face API with retries and
|
36 |
"""
|
37 |
if model not in HF_MODELS:
|
38 |
-
raise ValueError(f"Invalid model name: {model}.
|
39 |
|
40 |
api_url = HF_MODELS[model]
|
41 |
|
@@ -44,15 +53,13 @@ def query(payload, model="pegasus", retries=5, delay=5):
|
|
44 |
response = requests.post(api_url, headers=HF_HEADERS, json=payload, timeout=10)
|
45 |
|
46 |
if response.status_code == 401:
|
47 |
-
print(
|
48 |
return None
|
49 |
-
|
50 |
if response.status_code == 402:
|
51 |
-
print(
|
52 |
return None
|
53 |
-
|
54 |
if response.status_code in [500, 503]:
|
55 |
-
print(f"⚠️ Server error ({response.status_code}) on attempt {attempt + 1}. Retrying in {delay}
|
56 |
time.sleep(delay)
|
57 |
continue
|
58 |
|
@@ -60,11 +67,10 @@ def query(payload, model="pegasus", retries=5, delay=5):
|
|
60 |
return response.json()
|
61 |
|
62 |
except requests.exceptions.Timeout:
|
63 |
-
print(f"⏳ Timeout
|
64 |
time.sleep(delay)
|
65 |
-
|
66 |
except requests.exceptions.RequestException as e:
|
67 |
-
print(f"❌
|
68 |
time.sleep(delay)
|
69 |
|
70 |
print("🚨 All retry attempts failed.")
|
|
|
1 |
+
# === Imports ===
|
2 |
import os
|
3 |
+
import time
|
4 |
+
import requests
|
5 |
from dotenv import load_dotenv
|
6 |
from supabase import create_client
|
7 |
+
from sentence_transformers import SentenceTransformer
|
8 |
+
from openai import OpenAI
|
|
|
9 |
|
10 |
+
# === Load Environment Variables ===
|
11 |
load_dotenv()
|
12 |
|
13 |
+
# === Supabase Configuration ===
|
14 |
SUPABASE_URL = "https://lmpazoxzucnlqqxjoihi.supabase.co"
|
15 |
SUPABASE_KEY = os.getenv("SUPABASE_API_KEY")
|
16 |
if not SUPABASE_KEY:
|
17 |
raise ValueError("SUPABASE_KEY is not set in the environment variables.")
|
18 |
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
19 |
|
20 |
+
# === Embedding Model for Scoring ===
|
21 |
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
22 |
|
23 |
+
# === Hugging Face API Configuration ===
|
|
|
|
|
|
|
|
|
|
|
24 |
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
25 |
if not HF_API_TOKEN:
|
26 |
raise ValueError("Missing Hugging Face API key. Check your .env file.")
|
|
|
27 |
HF_HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
28 |
|
29 |
+
# === Hugging Face Model Endpoints ===
|
30 |
+
HF_MODELS = {
|
31 |
+
"pegasus": "https://router.huggingface.co/hf-inference/models/google/pegasus-xsum",
|
32 |
+
"gemma": "tgi" # Used as the model name with OpenAI-compatible client
|
33 |
+
}
|
34 |
+
|
35 |
+
# === OpenAI-Compatible Client (for Gemma) ===
|
36 |
+
client = OpenAI(
|
37 |
+
base_url="https://vzwjawyxvu030jsw.us-east-1.aws.endpoints.huggingface.cloud/v1/",
|
38 |
+
api_key=HF_API_TOKEN,
|
39 |
+
)
|
40 |
+
|
41 |
+
# === Optional: General Query Helper (for non-chat models like pegasus) ===
|
42 |
def query(payload, model="pegasus", retries=5, delay=5):
|
43 |
"""
|
44 |
+
Sends a request to the Hugging Face API with retries and error handling.
|
45 |
"""
|
46 |
if model not in HF_MODELS:
|
47 |
+
raise ValueError(f"Invalid model name: {model}. Available: {list(HF_MODELS.keys())}")
|
48 |
|
49 |
api_url = HF_MODELS[model]
|
50 |
|
|
|
53 |
response = requests.post(api_url, headers=HF_HEADERS, json=payload, timeout=10)
|
54 |
|
55 |
if response.status_code == 401:
|
56 |
+
print("❌ Unauthorized (401). Check HF_API_TOKEN.")
|
57 |
return None
|
|
|
58 |
if response.status_code == 402:
|
59 |
+
print("💰 Payment Required (402). Free tier may not support this model.")
|
60 |
return None
|
|
|
61 |
if response.status_code in [500, 503]:
|
62 |
+
print(f"⚠️ Server error ({response.status_code}) on attempt {attempt + 1}. Retrying in {delay}s...")
|
63 |
time.sleep(delay)
|
64 |
continue
|
65 |
|
|
|
67 |
return response.json()
|
68 |
|
69 |
except requests.exceptions.Timeout:
|
70 |
+
print(f"⏳ Timeout on attempt {attempt + 1}. Retrying in {delay}s...")
|
71 |
time.sleep(delay)
|
|
|
72 |
except requests.exceptions.RequestException as e:
|
73 |
+
print(f"❌ Request failed: {e}")
|
74 |
time.sleep(delay)
|
75 |
|
76 |
print("🚨 All retry attempts failed.")
|
utils.py
CHANGED
@@ -18,20 +18,21 @@ from fuzzywuzzy import fuzz
|
|
18 |
from sentence_transformers import SentenceTransformer, util
|
19 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
20 |
from huggingface_hub import InferenceClient
|
|
|
21 |
|
22 |
# Local Configuration
|
23 |
from config import (
|
24 |
SUPABASE_URL, SUPABASE_KEY, HF_API_TOKEN, HF_HEADERS,
|
25 |
-
supabase, HF_MODELS, query, embedding_model
|
26 |
)
|
27 |
|
28 |
# === Initialization ===
|
29 |
|
30 |
-
# Hugging Face inference client for Gemma model
|
31 |
-
client = InferenceClient(
|
32 |
-
|
33 |
-
|
34 |
-
)
|
35 |
|
36 |
# Load or download spaCy model
|
37 |
try:
|
@@ -173,18 +174,26 @@ def summarize_resume(resume_text):
|
|
173 |
)
|
174 |
|
175 |
try:
|
176 |
-
response = client.
|
|
|
177 |
messages=[{"role": "user", "content": prompt}],
|
178 |
temperature=0.5,
|
179 |
max_tokens=300,
|
180 |
)
|
181 |
result = response.choices[0].message.content.strip()
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
except Exception as e:
|
184 |
print(f"❌ Error generating structured summary: {e}")
|
185 |
return "Summary unavailable due to API issues."
|
186 |
|
187 |
-
|
188 |
# === Data Storage & Reporting ===
|
189 |
|
190 |
def store_in_supabase(resume_text, score, candidate_name, email, summary):
|
@@ -242,11 +251,13 @@ def generate_interview_questions_from_summaries(candidates):
|
|
242 |
)
|
243 |
|
244 |
try:
|
245 |
-
response = client.
|
|
|
246 |
messages=[{"role": "user", "content": prompt}],
|
247 |
temperature=0.7,
|
248 |
-
max_tokens=500
|
249 |
-
|
|
|
250 |
result = response.choices[0].message.content
|
251 |
|
252 |
# Clean and normalize questions
|
@@ -260,14 +271,17 @@ def generate_interview_questions_from_summaries(candidates):
|
|
260 |
if not q or re.match(r"^#+\s*", q):
|
261 |
continue
|
262 |
|
263 |
-
# Remove leading "
|
264 |
-
q = re.sub(r"^(?:Q?\d+[\.\)\-]?\s*)+", "", q)
|
265 |
|
266 |
# Remove markdown bold/italics (**, *, etc.)
|
267 |
q = re.sub(r"[*_]+", "", q)
|
|
|
|
|
|
|
268 |
|
269 |
questions.append(q.strip())
|
270 |
-
|
271 |
return [f"Q{i+1}. {q}" for i, q in enumerate(questions[:5])] or ["⚠️ No questions generated."]
|
272 |
|
273 |
except Exception as e:
|
|
|
18 |
from sentence_transformers import SentenceTransformer, util
|
19 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
20 |
from huggingface_hub import InferenceClient
|
21 |
+
from openai import OpenAI
|
22 |
|
23 |
# Local Configuration
|
24 |
from config import (
|
25 |
SUPABASE_URL, SUPABASE_KEY, HF_API_TOKEN, HF_HEADERS,
|
26 |
+
supabase, HF_MODELS, query, embedding_model, client
|
27 |
)
|
28 |
|
29 |
# === Initialization ===
|
30 |
|
31 |
+
# # Hugging Face inference client for Gemma model
|
32 |
+
# client = InferenceClient(
|
33 |
+
# model="tgi",
|
34 |
+
# token=HF_API_TOKEN
|
35 |
+
# )
|
36 |
|
37 |
# Load or download spaCy model
|
38 |
try:
|
|
|
174 |
)
|
175 |
|
176 |
try:
|
177 |
+
response = client.chat.completions.create(
|
178 |
+
model="tgi",
|
179 |
messages=[{"role": "user", "content": prompt}],
|
180 |
temperature=0.5,
|
181 |
max_tokens=300,
|
182 |
)
|
183 |
result = response.choices[0].message.content.strip()
|
184 |
+
|
185 |
+
# Clean up generic lead-ins from the model
|
186 |
+
cleaned = re.sub(
|
187 |
+
r"^(Sure,|Certainly,)?\s*(here is|here’s|this is)?\s*(the)?\s*(extracted)?\s*(professional)?\s*summary.*?:\s*",
|
188 |
+
"", result, flags=re.IGNORECASE
|
189 |
+
).strip()
|
190 |
+
|
191 |
+
return cleaned
|
192 |
+
|
193 |
except Exception as e:
|
194 |
print(f"❌ Error generating structured summary: {e}")
|
195 |
return "Summary unavailable due to API issues."
|
196 |
|
|
|
197 |
# === Data Storage & Reporting ===
|
198 |
|
199 |
def store_in_supabase(resume_text, score, candidate_name, email, summary):
|
|
|
251 |
)
|
252 |
|
253 |
try:
|
254 |
+
response = client.chat.completions.create(
|
255 |
+
model="tgi",
|
256 |
messages=[{"role": "user", "content": prompt}],
|
257 |
temperature=0.7,
|
258 |
+
max_tokens=500,
|
259 |
+
)
|
260 |
+
|
261 |
result = response.choices[0].message.content
|
262 |
|
263 |
# Clean and normalize questions
|
|
|
271 |
if not q or re.match(r"^#+\s*", q):
|
272 |
continue
|
273 |
|
274 |
+
# Remove leading bullets like "1.", "1)", "- 1.", etc.
|
275 |
+
q = re.sub(r"^(?:[-*]?\s*)?(?:Q?\d+[\.\)\-]?\s*)+", "", q)
|
276 |
|
277 |
# Remove markdown bold/italics (**, *, etc.)
|
278 |
q = re.sub(r"[*_]+", "", q)
|
279 |
+
|
280 |
+
# Remove duplicate trailing punctuation
|
281 |
+
q = q.strip(" .")
|
282 |
|
283 |
questions.append(q.strip())
|
284 |
+
|
285 |
return [f"Q{i+1}. {q}" for i, q in enumerate(questions[:5])] or ["⚠️ No questions generated."]
|
286 |
|
287 |
except Exception as e:
|