Spaces:
Sleeping
Sleeping
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) |