Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,70 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from flask import Flask, render_template, request, jsonify
|
| 3 |
import requests
|
| 4 |
-
import
|
| 5 |
from dotenv import load_dotenv
|
|
|
|
| 6 |
|
| 7 |
load_dotenv() # Load environment variables from .env file
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import os
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
load_dotenv() # Load environment variables from .env file
|
| 7 |
|
| 8 |
+
# Ensure the Hugging Face API token is set in your environment
|
| 9 |
+
api_token = os.getenv("HF_API_TOKEN")
|
| 10 |
+
|
| 11 |
+
# Check if the token is available
|
| 12 |
+
if api_token is None:
|
| 13 |
+
print("API token is not set. Please set the 'HF_API_TOKEN' environment variable.")
|
| 14 |
+
exit(1)
|
| 15 |
+
|
| 16 |
+
# Set the authorization header with the token
|
| 17 |
+
headers = {
|
| 18 |
+
"Authorization": f"Bearer {api_token}",
|
| 19 |
+
"Content-Type": "application/json"
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# URL for the Hugging Face model inference
|
| 23 |
+
model_url = "https://api-inference.huggingface.co/models/Ouiam123/Llama-2-7b-chat-finetune-tourism"
|
| 24 |
+
|
| 25 |
+
# Input text you want to send to the model (match the first code's formatting)
|
| 26 |
+
input_text = "What should I do if I get lost in Morocco?"
|
| 27 |
+
formatted_prompt = f"<s>[INST] {input_text} [/INST>"
|
| 28 |
+
|
| 29 |
+
# Request payload
|
| 30 |
+
payload = {
|
| 31 |
+
"inputs": formatted_prompt,
|
| 32 |
+
"parameters": {
|
| 33 |
+
"max_new_tokens": 500,
|
| 34 |
+
"temperature": 0.7,
|
| 35 |
+
"top_p": 0.95,
|
| 36 |
+
"repetition_penalty": 1.15
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Function to make the API request with retry on failure
|
| 41 |
+
def get_model_response():
|
| 42 |
+
try:
|
| 43 |
+
response = requests.post(
|
| 44 |
+
model_url,
|
| 45 |
+
headers=headers,
|
| 46 |
+
json=payload,
|
| 47 |
+
timeout=30
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if response.status_code == 200:
|
| 51 |
+
return response.json() # Return the response if successful
|
| 52 |
+
elif response.status_code == 503: # Retry on service unavailable (503)
|
| 53 |
+
print("Service unavailable, retrying...")
|
| 54 |
+
time.sleep(20) # Wait before retrying
|
| 55 |
+
return get_model_response() # Recursive retry
|
| 56 |
+
else:
|
| 57 |
+
print(f"Error {response.status_code}: {response.text}")
|
| 58 |
+
return None # Return None in case of error
|
| 59 |
+
except requests.exceptions.RequestException as e:
|
| 60 |
+
print(f"Request error: {e}")
|
| 61 |
+
return None # Handle request exceptions
|
| 62 |
+
|
| 63 |
+
# Get the model response
|
| 64 |
+
model_response = get_model_response()
|
| 65 |
+
|
| 66 |
+
# Output the result
|
| 67 |
+
if model_response:
|
| 68 |
+
print("Response:", model_response) # Print the model's response
|
| 69 |
+
else:
|
| 70 |
+
print("Failed to get a valid response from the model.")
|