Create api_helper.py
Browse files- api_helper.py +28 -0
api_helper.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
# Constants
|
| 4 |
+
SPACE_URL = "https://abanm-dubs.hf.space/api/generate"
|
| 5 |
+
API_KEY = "your_api_key_here" # Replace with your actual API key
|
| 6 |
+
|
| 7 |
+
def call_api(message):
|
| 8 |
+
"""
|
| 9 |
+
Calls the backend API with the user's input.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
message (str): User's message.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
str: Response from the backend API or an error message.
|
| 16 |
+
"""
|
| 17 |
+
headers = {
|
| 18 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 19 |
+
"Content-Type": "application/json",
|
| 20 |
+
}
|
| 21 |
+
payload = {"input": message}
|
| 22 |
+
try:
|
| 23 |
+
response = requests.post(SPACE_URL, json=payload, headers=headers)
|
| 24 |
+
response.raise_for_status()
|
| 25 |
+
result = response.json()
|
| 26 |
+
return result.get("output", "Error: No response from API.")
|
| 27 |
+
except requests.exceptions.RequestException as e:
|
| 28 |
+
return f"Error: {str(e)}"
|