Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
-
|
4 |
import gradio as gr
|
5 |
|
6 |
# Load environment variables
|
@@ -9,18 +9,27 @@ load_dotenv()
|
|
9 |
# Access the Hugging Face API key
|
10 |
hf_api_key = os.getenv('HF_API_KEY')
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
)
|
|
|
18 |
|
19 |
def summarize(input_text):
|
20 |
try:
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
except Exception as e:
|
25 |
return f"An error occurred: {str(e)}"
|
26 |
|
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
+
import requests
|
4 |
import gradio as gr
|
5 |
|
6 |
# Load environment variables
|
|
|
9 |
# Access the Hugging Face API key
|
10 |
hf_api_key = os.getenv('HF_API_KEY')
|
11 |
|
12 |
+
# Set up the API endpoint
|
13 |
+
API_URL = "https://api-inference.huggingface.co/models/sshleifer/distilbart-cnn-12-6"
|
14 |
+
headers = {"Authorization": f"Bearer {hf_api_key}"}
|
15 |
+
|
16 |
+
def query(payload):
|
17 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
18 |
+
return response.json()
|
19 |
|
20 |
def summarize(input_text):
|
21 |
try:
|
22 |
+
output = query({
|
23 |
+
"inputs": input_text,
|
24 |
+
"parameters": {"max_length": 130, "min_length": 30}
|
25 |
+
})
|
26 |
+
|
27 |
+
if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]:
|
28 |
+
return output[0]['summary_text']
|
29 |
+
elif isinstance(output, dict) and 'error' in output:
|
30 |
+
return f"API Error: {output['error']}"
|
31 |
+
else:
|
32 |
+
return f"Unexpected response format: {output}"
|
33 |
except Exception as e:
|
34 |
return f"An error occurred: {str(e)}"
|
35 |
|