Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,60 +0,0 @@
|
|
1 |
-
import os
|
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:
|
37 |
-
response = requests.post(API_URL, headers=HEADERS, json={"inputs": prompt}, timeout=10)
|
38 |
-
response.raise_for_status()
|
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))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|