adi-123 commited on
Commit
40399bd
1 Parent(s): 7436f5b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline, GPT2Tokenizer, GPT2LMHeadModel
2
+ import torch
3
+ import streamlit as st
4
+
5
+ # Load GPT-2 model and tokenizer
6
+ model_name = "openai-community/gpt2-xl"
7
+ model = GPT2LMHeadModel.from_pretrained(model_name)
8
+ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
9
+
10
+ # Set up sentiment analysis pipeline
11
+ sentiment_analysis = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
12
+
13
+ # Streamlit UI
14
+ st.title("GPT-2 Movie Review Sentiment Analysis")
15
+
16
+ # Input text for sentiment analysis
17
+ input_text = st.text_area("Enter movie review:", "")
18
+
19
+ # Choose analysis type
20
+ analysis_type = st.radio("Select analysis type:", ["Zero-shot", "One-shot", "Few-shot"])
21
+
22
+ if st.button("Analyze Sentiment"):
23
+ # Perform sentiment analysis based on the selected type
24
+ if analysis_type == "Zero-shot":
25
+ results = sentiment_analysis(input_text)
26
+ elif analysis_type == "One-shot":
27
+ prompt = "This movie review is about a "
28
+ input_text = prompt + input_text
29
+ results = sentiment_analysis(input_text)
30
+ elif analysis_type == "Few-shot":
31
+ prompt = "This movie review is about a positive topic. " + input_text
32
+ results = sentiment_analysis(prompt)
33
+
34
+ # Display results
35
+ st.write("Sentiment:", results[0]['label'])
36
+ st.write("Confidence:", results[0]['score'])
37
+
38
+ # Note: This is a basic example, and you might need to fine-tune it based on your specific use case and requirements.