Huzaifa367 commited on
Commit
dedbfd2
·
verified ·
1 Parent(s): 6aa1f96

Update pages/summarizer.py

Browse files
Files changed (1) hide show
  1. pages/summarizer.py +23 -7
pages/summarizer.py CHANGED
@@ -1,6 +1,8 @@
1
  import streamlit as st
2
  import os
3
  import requests
 
 
4
 
5
  # Define Hugging Face API details
6
  API_URL = "https://api-inference.huggingface.co/models/Huzaifa367/chat-summarizer"
@@ -9,13 +11,27 @@ HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
9
 
10
  # Function to query Hugging Face API
11
  def query_huggingface(payload):
12
- try:
13
- response = requests.post(API_URL, headers=HEADERS, json=payload)
14
- response.raise_for_status() # Raise exception for non-2xx status codes
15
- return response.json()
16
- except requests.exceptions.RequestException as e:
17
- st.error(f"Error querying Hugging Face API: {e}")
18
- return {"summary_text": f"Error querying Hugging Face API: {e}"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  def main():
21
  st.set_page_config(layout="wide")
 
1
  import streamlit as st
2
  import os
3
  import requests
4
+ import time
5
+
6
 
7
  # Define Hugging Face API details
8
  API_URL = "https://api-inference.huggingface.co/models/Huzaifa367/chat-summarizer"
 
11
 
12
  # Function to query Hugging Face API
13
  def query_huggingface(payload):
14
+ max_retries=3
15
+ retries = 0
16
+ while retries < max_retries:
17
+ try:
18
+ response = requests.post(API_URL, headers=HEADERS, json=payload)
19
+ response.raise_for_status() # Raise exception for non-2xx status codes
20
+ return response.json()
21
+ except requests.exceptions.RequestException as e:
22
+ st.error(f"Error querying Hugging Face API: {e}")
23
+ return {"summary_text": f"Error querying Hugging Face API: {e}"}
24
+ except requests.exceptions.HTTPError as e:
25
+ if response.status_code == 503:
26
+ st.warning("Service temporarily unavailable. Retrying...")
27
+ time.sleep(5) # Wait for a few seconds before retrying
28
+ retries += 1
29
+ else:
30
+ st.error(f"HTTP Error querying Hugging Face API: {e.response.status_code}")
31
+ return {"summary_text": "Service Unavailable. Please try again later."}
32
+ except Exception as e:
33
+ st.error(f"An unexpected error occurred: {e}")
34
+ return {"summary_text": "An unexpected error occurred. Please try again later."}
35
 
36
  def main():
37
  st.set_page_config(layout="wide")