File size: 1,145 Bytes
bc4e787
eea1f8d
 
bc4e787
3722485
 
 
 
 
 
bc4e787
3722485
 
 
 
 
bc4e787
 
 
3722485
 
 
 
 
bc4e787
eea1f8d
 
 
 
 
6179462
eea1f8d
 
 
 
 
2dcc3a6
6179462
 
 
eea1f8d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import gradio as gr
import streamlit as st
import socket

try:
    from transformers import pipeline
except ImportError as e:
    st.error(f"ImportError: {e}")
    st.stop()

# Load the pre-trained sentiment-analysis pipeline
try:
    classifier = pipeline('sentiment-analysis')
except Exception as e:
    st.error(f"Error loading pipeline: {e}")
    st.stop()

# Function to classify sentiment
def classify_text(text):
    try:
        result = classifier(text)[0]
        return f"{result['label']} with score {result['score']}"
    except Exception as e:
        return f"Error classifying text: {e}"

# Function to find an available port
def find_free_port():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(('', 0))
        return s.getsockname()[1]

# Find an available port
port = find_free_port()

# Launch the Gradio interface on the dynamically found port
iface = gr.Interface(fn=classify_text, inputs="text", outputs="text")
iface.launch(server_port=port)

# Streamlit code
st.title('IMDb Sentiment Analysis')
st.write('This project performs sentiment analysis on IMDb movie reviews using Streamlit.')