Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,11 @@ import os
|
|
2 |
from dotenv import load_dotenv
|
3 |
import requests
|
4 |
import gradio as gr
|
|
|
5 |
|
|
|
|
|
|
|
6 |
|
7 |
# Load environment variables
|
8 |
load_dotenv()
|
@@ -10,12 +14,23 @@ load_dotenv()
|
|
10 |
# Access the Hugging Face API key
|
11 |
hf_api_key = os.getenv('HF_API_KEY')
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Set up the API endpoint
|
14 |
API_URL = "https://api-inference.huggingface.co/models/sshleifer/distilbart-cnn-12-6"
|
15 |
headers = {"Authorization": f"Bearer {hf_api_key}"}
|
16 |
|
17 |
def query(payload):
|
18 |
response = requests.post(API_URL, headers=headers, json=payload)
|
|
|
|
|
|
|
19 |
return response.json()
|
20 |
|
21 |
def summarize(input_text):
|
@@ -28,10 +43,13 @@ def summarize(input_text):
|
|
28 |
if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]:
|
29 |
return output[0]['summary_text']
|
30 |
elif isinstance(output, dict) and 'error' in output:
|
|
|
31 |
return f"API Error: {output['error']}"
|
32 |
else:
|
|
|
33 |
return f"Unexpected response format: {output}"
|
34 |
except Exception as e:
|
|
|
35 |
return f"An error occurred: {str(e)}"
|
36 |
|
37 |
# Create and launch the Gradio interface
|
|
|
2 |
from dotenv import load_dotenv
|
3 |
import requests
|
4 |
import gradio as gr
|
5 |
+
import logging
|
6 |
|
7 |
+
# Set up logging
|
8 |
+
logging.basicConfig(level=logging.INFO)
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
|
11 |
# Load environment variables
|
12 |
load_dotenv()
|
|
|
14 |
# Access the Hugging Face API key
|
15 |
hf_api_key = os.getenv('HF_API_KEY')
|
16 |
|
17 |
+
# Check if the API key is available
|
18 |
+
if not hf_api_key:
|
19 |
+
logger.error("HF_API_KEY is not set in the environment variables.")
|
20 |
+
raise ValueError("HF_API_KEY is missing. Please set it in your environment or .env file.")
|
21 |
+
|
22 |
+
# Log a part of the API key for verification (be careful not to log the entire key)
|
23 |
+
logger.info(f"Using API key starting with: {hf_api_key[:4]}{'*' * (len(hf_api_key) - 4)}")
|
24 |
+
|
25 |
# Set up the API endpoint
|
26 |
API_URL = "https://api-inference.huggingface.co/models/sshleifer/distilbart-cnn-12-6"
|
27 |
headers = {"Authorization": f"Bearer {hf_api_key}"}
|
28 |
|
29 |
def query(payload):
|
30 |
response = requests.post(API_URL, headers=headers, json=payload)
|
31 |
+
if response.status_code != 200:
|
32 |
+
logger.error(f"API request failed with status code {response.status_code}")
|
33 |
+
logger.error(f"Response content: {response.text}")
|
34 |
return response.json()
|
35 |
|
36 |
def summarize(input_text):
|
|
|
43 |
if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]:
|
44 |
return output[0]['summary_text']
|
45 |
elif isinstance(output, dict) and 'error' in output:
|
46 |
+
logger.error(f"API Error: {output['error']}")
|
47 |
return f"API Error: {output['error']}"
|
48 |
else:
|
49 |
+
logger.error(f"Unexpected response format: {output}")
|
50 |
return f"Unexpected response format: {output}"
|
51 |
except Exception as e:
|
52 |
+
logger.exception("An error occurred during summarization")
|
53 |
return f"An error occurred: {str(e)}"
|
54 |
|
55 |
# Create and launch the Gradio interface
|