Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,12 @@ import requests
|
|
3 |
from fpdf import FPDF
|
4 |
import pdfplumber
|
5 |
import os
|
|
|
6 |
|
7 |
# Mistral API key (replace with your key or use environment variable)
|
8 |
api_key = os.getenv("MISTRAL_API_KEY", "gz6lDXokxgR6cLY72oomALWcm7vhjRzQ")
|
9 |
|
10 |
-
# Function to call Mistral API
|
11 |
def call_mistral_api(prompt):
|
12 |
url = "https://api.mistral.ai/v1/chat/completions"
|
13 |
headers = {
|
@@ -25,6 +26,10 @@ def call_mistral_api(prompt):
|
|
25 |
response.raise_for_status() # Raise an error for bad status codes
|
26 |
return response.json()['choices'][0]['message']['content']
|
27 |
except requests.exceptions.HTTPError as err:
|
|
|
|
|
|
|
|
|
28 |
return f"HTTP Error: {err}"
|
29 |
except Exception as err:
|
30 |
return f"Error: {err}"
|
|
|
3 |
from fpdf import FPDF
|
4 |
import pdfplumber
|
5 |
import os
|
6 |
+
import time
|
7 |
|
8 |
# Mistral API key (replace with your key or use environment variable)
|
9 |
api_key = os.getenv("MISTRAL_API_KEY", "gz6lDXokxgR6cLY72oomALWcm7vhjRzQ")
|
10 |
|
11 |
+
# Function to call Mistral API with rate limit handling
|
12 |
def call_mistral_api(prompt):
|
13 |
url = "https://api.mistral.ai/v1/chat/completions"
|
14 |
headers = {
|
|
|
26 |
response.raise_for_status() # Raise an error for bad status codes
|
27 |
return response.json()['choices'][0]['message']['content']
|
28 |
except requests.exceptions.HTTPError as err:
|
29 |
+
if response.status_code == 429: # Rate limit exceeded
|
30 |
+
st.warning("Rate limit exceeded. Please wait a few seconds and try again.")
|
31 |
+
time.sleep(5) # Wait for 5 seconds before retrying
|
32 |
+
return call_mistral_api(prompt) # Retry the request
|
33 |
return f"HTTP Error: {err}"
|
34 |
except Exception as err:
|
35 |
return f"Error: {err}"
|