adi-123 commited on
Commit
a218fec
1 Parent(s): 8f5c89a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -17
app.py CHANGED
@@ -2,36 +2,35 @@ import os
2
  import requests
3
  import streamlit as st
4
 
5
- API_URL = "https://api-inference.huggingface.co/models/distilgpt2" # Updated API endpoint
6
  API_TOKEN = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
7
  HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
8
 
9
- def get_sentiment_category(sentiment_label):
10
- sentiment_label = sentiment_label.lower()
11
- if "pos" in sentiment_label:
12
- return "Positive"
13
- elif "neg" in sentiment_label:
14
- return "Negative"
15
  else:
16
- return "Mixed"
17
 
18
- st.title("DistilGPT2 Movie Review Sentiment Analysis")
19
 
20
  input_text = st.text_area("Enter movie review:", "")
21
 
22
  analysis_type = st.radio("Select analysis type:", ["Zero-shot", "One-shot", "Few-shot"])
23
 
24
  if analysis_type == "Zero-shot":
25
- prompt = f"Classify sentiment as positive or negative or mixed: \n\n{input_text}\n\nSentiment:"
26
 
27
  elif analysis_type == "One-shot":
28
  example = st.text_area("Input one example:")
29
- prompt = f"Classify sentiment as positive or negative or mixed: \n{example}\n\nMovie review:\n{input_text}\n\nSentiment:"
30
 
31
  elif analysis_type == "Few-shot":
32
  examples = st.text_area("Input few-shot examples, one per line:")
33
  examples_list = examples.split('\n')
34
- prompt = f"Classify sentiment as positive or negative or mixed: \n{', '.join(examples_list)}\n\nMovie review: \n{input_text}\n\nSentiment:"
35
 
36
  if st.button("Analyze"):
37
  try:
@@ -40,14 +39,22 @@ if st.button("Analyze"):
40
 
41
  result = response.json()[0]['generated_text']
42
 
43
- # Extract sentiment label directly
44
  sentiment_start = result.find("Sentiment:") + len("Sentiment:")
45
  sentiment_end = result.find(".", sentiment_start)
46
- sentiment_label = result[sentiment_start:sentiment_end].strip()
47
 
48
- # Convert sentiment label to category
49
- sentiment_category = get_sentiment_category(sentiment_label)
 
 
 
 
 
 
 
 
50
  st.write(f"Sentiment: {sentiment_category}")
51
 
52
  except requests.exceptions.RequestException as e:
53
- st.error("Error reaching API\n{}".format(e))
 
2
  import requests
3
  import streamlit as st
4
 
5
+ API_URL = "https://api-inference.huggingface.co/models/gpt2" # Updated API endpoint
6
  API_TOKEN = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
7
  HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
8
 
9
+ def get_sentiment_category(sentiment_score):
10
+ if sentiment_score > 0:
11
+ return "positive"
12
+ elif sentiment_score < 0:
13
+ return "negative"
 
14
  else:
15
+ return "mixed"
16
 
17
+ st.title("GPT-2 Movie Review Sentiment Analysis")
18
 
19
  input_text = st.text_area("Enter movie review:", "")
20
 
21
  analysis_type = st.radio("Select analysis type:", ["Zero-shot", "One-shot", "Few-shot"])
22
 
23
  if analysis_type == "Zero-shot":
24
+ prompt = f"Sentiment analysis of the following movie review: \n\n{input_text}\n\nSentiment:"
25
 
26
  elif analysis_type == "One-shot":
27
  example = st.text_area("Input one example:")
28
+ prompt = f"{example}\n\nMovie review:\n{input_text}\n\nSentiment:"
29
 
30
  elif analysis_type == "Few-shot":
31
  examples = st.text_area("Input few-shot examples, one per line:")
32
  examples_list = examples.split('\n')
33
+ prompt = f"Sentiment analysis examples: \n{', '.join(examples_list)}\n\nMovie review: \n{input_text}\n\nSentiment:"
34
 
35
  if st.button("Analyze"):
36
  try:
 
39
 
40
  result = response.json()[0]['generated_text']
41
 
42
+ # Extract sentiment score and category
43
  sentiment_start = result.find("Sentiment:") + len("Sentiment:")
44
  sentiment_end = result.find(".", sentiment_start)
45
+ sentiment_score_text = result[sentiment_start:sentiment_end].strip()
46
 
47
+ # Extract only the numerical part of the score
48
+ if '/' in sentiment_score_text:
49
+ sentiment_score_text = sentiment_score_text.split('/')[0]
50
+
51
+ try:
52
+ sentiment_score = float(sentiment_score_text.split()[0])
53
+ except ValueError:
54
+ sentiment_score = None
55
+
56
+ sentiment_category = get_sentiment_category(sentiment_score) if sentiment_score is not None else "mixed"
57
  st.write(f"Sentiment: {sentiment_category}")
58
 
59
  except requests.exceptions.RequestException as e:
60
+ st.error("Error reaching API\n{}".format(e))