Spaces:
Runtime error
Runtime error
Update pages/summarizer.py
Browse files11 unmodified lines12131415161718192021222324252627282930313233343536373817 unmodified lines565758596061626364656667686970717211 unmodified lines# Function to query Hugging Face APIdef query_huggingface(payload):max_retries=3retries = 0while retries < max_retries:try:response = requests.post(API_URL, headers=HEADERS, json=payload)response.raise_for_status() # Raise exception for non-2xx status codesreturn response.json()except requests.exceptions.RequestException as e:st.error(f"Error querying Hugging Face API: {e}")return {"summary_text": f"Error querying Hugging Face API: {e}"}except requests.exceptions.HTTPError as e:if response.status_code == 503:st.warning("Service temporarily unavailable. Retrying...")time.sleep(5) # Wait for a few seconds before retryingretries += 1else:st.error(f"HTTP Error querying Hugging Face API: {e.response.status_code}")return {"summary_text": "Service Unavailable. Please try again later."}except Exception as e:st.error(f"An unexpected error occurred: {e}")return {"summary_text": "An unexpected error occurred. Please try again later."}def text_to_speech(text):tts = gTTS(text=text, lang='en')audio_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)17 unmodified linesif user_message:# Add user message to chat historychat_history.append({"speaker": "User", "message": user_message})# Construct input text for summarizationinput_text = f"User: {user_message}"# Query Hugging Face API for summarizationpayload = {"inputs": input_text}response = query_huggingface(payload)# Extract summary text from the API responsesummary_text = response[0]["summary_text"] if isinstance(response, list) else response.get("summary_text", "")# Add summarization response to chat historychat_history.append({"speaker": "Bot", "message": summary_text})11 unmodified lines121314151617181920212223242517 unmodified lines4344454647484950515253545511 unmodified lines# Function to query Hugging Face APIdef query_huggingface(payload):try:response = requests.post(API_URL, headers=HEADERS, json=payload)response.raise_for_status() # Raise exception for non-2xx status codesreturn response.json()except requests.exceptions.RequestException as e:st.error(f"Error querying Hugging Face API: {e}")return {"summary_text": f"Error querying Hugging Face API: {e}"}def text_to_speech(text):tts = gTTS(text=text, lang='en')audio_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)17 unmodified linesif user_message:# Add user message to chat historychat_history.append({"speaker": "User", "message": user_message})# Construct input text for summarizationinput_text = f"User: {user_message}"# Query Hugging Face API for summarizationpayload = {"inputs": input_text}response = query_huggingface(payload)# Extract summary text from the API responsesummary_text = response[0]["summary_text"] if isinstance(response, list) else response.get("summary_text", "")# Add summarization response to chat historychat_history.append({"speaker": "Bot", "message": summary_text})
pages/summarizer.py+8-25