Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -19,6 +19,9 @@ logger = logging.getLogger(__name__)
|
|
| 19 |
# Initialize HF API token (get this from your HF account)
|
| 20 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "")
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
# Initialize intent classifier
|
| 23 |
try:
|
| 24 |
intent_classifier = pipeline(
|
|
@@ -174,4 +177,38 @@ def text_to_speech(text):
|
|
| 174 |
|
| 175 |
def get_fallback_response():
|
| 176 |
"""Return a fallback response"""
|
| 177 |
-
return random.choice(FALLBACK_RESPONSES)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Initialize HF API token (get this from your HF account)
|
| 20 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "")
|
| 21 |
|
| 22 |
+
# Initialize HF API token (get this from your HF account)
|
| 23 |
+
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "")
|
| 24 |
+
|
| 25 |
# Initialize intent classifier
|
| 26 |
try:
|
| 27 |
intent_classifier = pipeline(
|
|
|
|
| 177 |
|
| 178 |
def get_fallback_response():
|
| 179 |
"""Return a fallback response"""
|
| 180 |
+
return random.choice(FALLBACK_RESPONSES)
|
| 181 |
+
|
| 182 |
+
def get_rag_response(query, intent, hf_space_url):
|
| 183 |
+
"""Get response using the RAG system via Hugging Face Spaces"""
|
| 184 |
+
try:
|
| 185 |
+
# Prepare data for the Hugging Face Space
|
| 186 |
+
api_url = f"{hf_space_url}/api/predict"
|
| 187 |
+
payload = {
|
| 188 |
+
"data": [
|
| 189 |
+
query,
|
| 190 |
+
intent
|
| 191 |
+
]
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
# Check if we should use API token
|
| 195 |
+
headers = {}
|
| 196 |
+
if HF_API_TOKEN:
|
| 197 |
+
headers["Authorization"] = f"Bearer {HF_API_TOKEN}"
|
| 198 |
+
|
| 199 |
+
# Call the Hugging Face Space
|
| 200 |
+
response = requests.post(api_url, json=payload, headers=headers)
|
| 201 |
+
|
| 202 |
+
if response.status_code == 200:
|
| 203 |
+
result = response.json()
|
| 204 |
+
# Extract the response text from the result
|
| 205 |
+
# Structure will depend on your Space's output format
|
| 206 |
+
response_text = result.get("data", ["I'm sorry, I couldn't process that request."])[0]
|
| 207 |
+
return response_text
|
| 208 |
+
else:
|
| 209 |
+
logger.error(f"Error from HF Space: {response.status_code} - {response.text}")
|
| 210 |
+
return get_fallback_response()
|
| 211 |
+
|
| 212 |
+
except Exception as e:
|
| 213 |
+
logger.error(f"Error getting RAG response: {e}")
|
| 214 |
+
return get_fallback_response()
|