File size: 1,948 Bytes
9f3f4d0
40399bd
 
9f3f4d0
 
 
40399bd
 
837232b
40399bd
 
d81f4dd
40399bd
 
 
 
 
9f3f4d0
40399bd
9f3f4d0
40399bd
d81f4dd
 
9f3f4d0
40399bd
837232b
d81f4dd
 
 
 
837232b
d81f4dd
9f3f4d0
 
 
 
40399bd
 
9f3f4d0
 
 
 
 
 
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
import requests
import streamlit as st

# API details
API_URL = "https://api-inference.huggingface.co/models/openai-community/gpt2-xl"
HEADERS = {"Authorization": "Bearer HUGGINGFACEHUB_API_TOKEN"}

# Streamlit UI
st.title("GPT-2 Movie Sentiment Analysis")

# Input text for sentiment analysis
input_text = st.text_area("Enter movie review:", "")

# Choose analysis type
analysis_type = st.radio("Select analysis type:", ["Zero-shot", "One-shot", "Few-shot"])

if st.button("Analyze Sentiment"):
    # Prepare payload for API request
    if analysis_type == "Zero-shot":
        payload = {"inputs": f"Label the text as either 'positive', 'negative', or 'mixed' related to a movie:\n\n{input_text}"}
    elif analysis_type == "One-shot":
        prompt = "Label the sentence as either 'positive', 'negative', or 'mixed' related to a movie:\n\n" \
                 "Sentence: This movie exceeded my expectations.\nLabel: positive"
        payload = {"inputs": f"{prompt} {input_text}"}
    elif analysis_type == "Few-shot":
        examples = [
            "Sentence: The cinematography in this movie is outstanding.\nLabel: positive",
            "Sentence: I didn't enjoy the plot twists in the movie.\nLabel: negative",
            "Sentence: The acting was great, but the pacing felt off.\nLabel: mixed",
            "Sentence: This movie didn't live up to the hype.\nLabel: negative",
        ]
        prompt = "Label the sentences as either 'positive', 'negative', or 'mixed' related to a movie:\n\n" + "\n".join(examples)
        payload = {"inputs": f"{prompt}\n\n{input_text}"}

    # Make API request
    response = requests.post(API_URL, headers=HEADERS, json=payload)

    # Display results
    if response.status_code == 200:
        result = response.json()
        st.write("Sentiment:", result[0]['label'])
        st.write("Confidence:", result[0]['score'])
    else:
        st.write("Error:", response.status_code, response.text)