File size: 1,435 Bytes
bc4e787
c61aa36
eea1f8d
 
c61aa36
bc4e787
de6e536
 
249116b
c61aa36
249116b
 
 
 
 
bc4e787
 
 
c61aa36
 
bc4e787
eea1f8d
 
 
 
 
6179462
c61aa36
 
 
 
eea1f8d
c61aa36
 
6179462
 
 
eea1f8d
c61aa36
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
import gradio as gr
from transformers import pipeline
import streamlit as st
import socket
import threading

# Specify the multilingual DistilBERT model
model_name = "distilbert-base-multilingual-cased"

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

# Function to classify sentiment
def classify_text(text):
    result = classifier(text)[0]
    return f"{result['label']} with score {result['score']}"

# 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]

# Function to run Gradio in a separate thread
def run_gradio():
    iface = gr.Interface(fn=classify_text, inputs="text", outputs="text")
    iface.launch(server_port=find_free_port())

# Start Gradio in a separate thread
threading.Thread(target=run_gradio).start()

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

st.text_input("Enter text for sentiment analysis", key="input_text")
if st.button("Classify"):
    text = st.session_state.input_text
    if text:
        result = classify_text(text)
        st.write(result)
    else:
        st.write("Please enter text for classification.")