Spaces:
Runtime error
Runtime error
Update pages/summarizer.py
Browse files- pages/summarizer.py +8 -25
pages/summarizer.py
CHANGED
@@ -12,27 +12,14 @@ HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
|
12 |
|
13 |
# Function to query Hugging Face API
|
14 |
def query_huggingface(payload):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
st.error(f"Error querying Hugging Face API: {e}")
|
24 |
-
return {"summary_text": f"Error querying Hugging Face API: {e}"}
|
25 |
-
except requests.exceptions.HTTPError as e:
|
26 |
-
if response.status_code == 503:
|
27 |
-
st.warning("Service temporarily unavailable. Retrying...")
|
28 |
-
time.sleep(5) # Wait for a few seconds before retrying
|
29 |
-
retries += 1
|
30 |
-
else:
|
31 |
-
st.error(f"HTTP Error querying Hugging Face API: {e.response.status_code}")
|
32 |
-
return {"summary_text": "Service Unavailable. Please try again later."}
|
33 |
-
except Exception as e:
|
34 |
-
st.error(f"An unexpected error occurred: {e}")
|
35 |
-
return {"summary_text": "An unexpected error occurred. Please try again later."}
|
36 |
def text_to_speech(text):
|
37 |
tts = gTTS(text=text, lang='en')
|
38 |
audio_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
@@ -56,17 +43,13 @@ def main():
|
|
56 |
if user_message:
|
57 |
# Add user message to chat history
|
58 |
chat_history.append({"speaker": "User", "message": user_message})
|
59 |
-
|
60 |
# Construct input text for summarization
|
61 |
input_text = f"User: {user_message}"
|
62 |
-
|
63 |
# Query Hugging Face API for summarization
|
64 |
payload = {"inputs": input_text}
|
65 |
response = query_huggingface(payload)
|
66 |
-
|
67 |
# Extract summary text from the API response
|
68 |
summary_text = response[0]["summary_text"] if isinstance(response, list) else response.get("summary_text", "")
|
69 |
-
|
70 |
# Add summarization response to chat history
|
71 |
chat_history.append({"speaker": "Bot", "message": summary_text})
|
72 |
|
|
|
12 |
|
13 |
# Function to query Hugging Face API
|
14 |
def query_huggingface(payload):
|
15 |
+
try:
|
16 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
17 |
+
response.raise_for_status() # Raise exception for non-2xx status codes
|
18 |
+
return response.json()
|
19 |
+
except requests.exceptions.RequestException as e:
|
20 |
+
st.error(f"Error querying Hugging Face API: {e}")
|
21 |
+
return {"summary_text": f"Error querying Hugging Face API: {e}"}
|
22 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def text_to_speech(text):
|
24 |
tts = gTTS(text=text, lang='en')
|
25 |
audio_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
|
|
43 |
if user_message:
|
44 |
# Add user message to chat history
|
45 |
chat_history.append({"speaker": "User", "message": user_message})
|
|
|
46 |
# Construct input text for summarization
|
47 |
input_text = f"User: {user_message}"
|
|
|
48 |
# Query Hugging Face API for summarization
|
49 |
payload = {"inputs": input_text}
|
50 |
response = query_huggingface(payload)
|
|
|
51 |
# Extract summary text from the API response
|
52 |
summary_text = response[0]["summary_text"] if isinstance(response, list) else response.get("summary_text", "")
|
|
|
53 |
# Add summarization response to chat history
|
54 |
chat_history.append({"speaker": "Bot", "message": summary_text})
|
55 |
|