rviana commited on
Commit
3722485
·
1 Parent(s): 2dcc3a6

Add Error Handling.

Browse files
Files changed (1) hide show
  1. app.py +16 -4
app.py CHANGED
@@ -1,15 +1,27 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  import streamlit as st
4
  import socket
5
 
 
 
 
 
 
 
6
  # Load the pre-trained sentiment-analysis pipeline
7
- classifier = pipeline('sentiment-analysis')
 
 
 
 
8
 
9
  # Function to classify sentiment
10
  def classify_text(text):
11
- result = classifier(text)[0]
12
- return f"{result['label']} with score {result['score']}"
 
 
 
13
 
14
  # Function to find an available port
15
  def find_free_port():
 
1
  import gradio as gr
 
2
  import streamlit as st
3
  import socket
4
 
5
+ try:
6
+ from transformers import pipeline
7
+ except ImportError as e:
8
+ st.error(f"ImportError: {e}")
9
+ st.stop()
10
+
11
  # Load the pre-trained sentiment-analysis pipeline
12
+ try:
13
+ classifier = pipeline('sentiment-analysis')
14
+ except Exception as e:
15
+ st.error(f"Error loading pipeline: {e}")
16
+ st.stop()
17
 
18
  # Function to classify sentiment
19
  def classify_text(text):
20
+ try:
21
+ result = classifier(text)[0]
22
+ return f"{result['label']} with score {result['score']}"
23
+ except Exception as e:
24
+ return f"Error classifying text: {e}"
25
 
26
  # Function to find an available port
27
  def find_free_port():