Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,44 @@
|
|
1 |
import os
|
2 |
-
from dotenv import load_dotenv
|
3 |
-
import requests
|
4 |
-
|
5 |
import gradio as gr
|
6 |
|
|
|
7 |
load_dotenv()
|
|
|
8 |
# Access the Hugging Face API key and endpoint URL
|
9 |
hf_api_key = os.getenv('HF_API_KEY')
|
10 |
MODEL_NAME = "sshleifer/distilbart-cnn-12-6"
|
11 |
ENDPOINT_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
12 |
|
13 |
-
#Summarization endpoint
|
14 |
def get_completion(inputs, parameters=None):
|
15 |
headers = {
|
16 |
"Authorization": f"Bearer {hf_api_key}",
|
17 |
"Content-Type": "application/json"
|
18 |
}
|
19 |
data = {
|
20 |
-
"inputs": inputs
|
|
|
21 |
}
|
22 |
if parameters is not None:
|
23 |
data.update({"parameters": parameters})
|
24 |
|
|
|
|
|
|
|
|
|
25 |
try:
|
26 |
response = requests.post(ENDPOINT_URL, headers=headers, json=data)
|
|
|
|
|
|
|
27 |
response.raise_for_status()
|
28 |
return response.json()
|
29 |
except requests.exceptions.RequestException as e:
|
30 |
print(f"Request failed: {e}")
|
|
|
|
|
31 |
return {"error": f"Request failed: {str(e)}"}
|
32 |
|
33 |
def summarize(input_text):
|
@@ -35,6 +46,8 @@ def summarize(input_text):
|
|
35 |
output = get_completion(input_text)
|
36 |
if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]:
|
37 |
return output[0]['summary_text']
|
|
|
|
|
38 |
else:
|
39 |
return f"Unexpected response format: {output}"
|
40 |
except Exception as e:
|
|
|
1 |
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
import gradio as gr
|
6 |
|
7 |
+
# Load environment variables
|
8 |
load_dotenv()
|
9 |
+
|
10 |
# Access the Hugging Face API key and endpoint URL
|
11 |
hf_api_key = os.getenv('HF_API_KEY')
|
12 |
MODEL_NAME = "sshleifer/distilbart-cnn-12-6"
|
13 |
ENDPOINT_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
14 |
|
|
|
15 |
def get_completion(inputs, parameters=None):
|
16 |
headers = {
|
17 |
"Authorization": f"Bearer {hf_api_key}",
|
18 |
"Content-Type": "application/json"
|
19 |
}
|
20 |
data = {
|
21 |
+
"inputs": inputs,
|
22 |
+
"options": {"wait_for_model": True}
|
23 |
}
|
24 |
if parameters is not None:
|
25 |
data.update({"parameters": parameters})
|
26 |
|
27 |
+
print(f"Sending request to {ENDPOINT_URL}")
|
28 |
+
print(f"Request headers: {headers}")
|
29 |
+
print(f"Request data: {data}")
|
30 |
+
|
31 |
try:
|
32 |
response = requests.post(ENDPOINT_URL, headers=headers, json=data)
|
33 |
+
print(f"Response status code: {response.status_code}")
|
34 |
+
print(f"Response content: {response.text}")
|
35 |
+
|
36 |
response.raise_for_status()
|
37 |
return response.json()
|
38 |
except requests.exceptions.RequestException as e:
|
39 |
print(f"Request failed: {e}")
|
40 |
+
if hasattr(e, 'response') and e.response is not None:
|
41 |
+
print(f"Error response content: {e.response.text}")
|
42 |
return {"error": f"Request failed: {str(e)}"}
|
43 |
|
44 |
def summarize(input_text):
|
|
|
46 |
output = get_completion(input_text)
|
47 |
if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]:
|
48 |
return output[0]['summary_text']
|
49 |
+
elif isinstance(output, dict) and 'error' in output:
|
50 |
+
return f"API Error: {output['error']}"
|
51 |
else:
|
52 |
return f"Unexpected response format: {output}"
|
53 |
except Exception as e:
|