arabic-RAG / backend /query_llm.py
derek-thomas's picture
derek-thomas HF staff
Moving endpoint status to backend
91f49a8
import os
import requests
from os import getenv
API_URL = getenv('API_URL')
BEARER = getenv('BEARER')
headers = {
"Authorization": f"Bearer {BEARER}",
"Content-Type": "application/json"
}
def call_jais(payload):
try:
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status() # This will raise an exception for HTTP error codes
return response.json()
except requests.exceptions.HTTPError as http_err:
raise gr.Error(f"An error occurred while processing the request. {http_err}")
except Exception as err:
raise gr.Error(f"An error occurred while processing the request. {err}")
def generate(prompt: str):
payload = {'inputs': '', 'prompt':prompt}
response = call_jais(payload)
return response
def check_endpoint_status():
# Replace with the actual API URL and headers
api_url = os.getenv("ENDPOINT_URL")
headers = {
'accept': 'application/json',
'Authorization': f'Bearer {os.getenv("BEARER")}'
}
try:
response = requests.get(api_url, headers=headers)
response.raise_for_status() # will throw an exception for non-200 status
data = response.json()
# Extracting the status information
status = data.get('status', {}).get('state', 'No status found')
message = data.get('status', {}).get('message', 'No message found')
return f"Status: {status}\nMessage: {message}"
except requests.exceptions.RequestException as e:
return f"Failed to get status: {str(e)}"