omm7 commited on
Commit
0f64ce3
·
verified ·
1 Parent(s): 81c4145

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import time
3
+ # REMOVED: import os (Not needed as it's in Dockerfile)
4
+ from transformers import T5Tokenizer, TFT5ForConditionalGeneration
5
+
6
+ # --- Configuration (Unchanged) ---
7
+ MODEL_NAME = "google/flan-t5-base"
8
+
9
+ # -------------------- Model Logic --------------------
10
+
11
+ # CRITICAL FIX: Simplified and highly directive prompt for the smallest model
12
+ # sys_prompt = "Classify the sentiment of the following customer review as either 'positive', 'negative', or 'neutral'. Respond with only one word."
13
+ sys_prompt = """
14
+ Classify the sentiment of the following customer review as either 'positive', 'negative', or 'neutral'. Respond with only one word.
15
+ Leverage your expertise in the aviation industry and deep understanding of industry trends to analyze the nuanced expressions and overall tone.
16
+ It is crucial to accurately identify neutral sentiments, which may indicate a balanced view or neutral stance towards Us Airways. Neutral expressions could involve factual statements without explicit positive or negative opinions.
17
+ Consider the importance of these neutral sentiments in gauging the public sentiment towards the airline company.
18
+ For instance, a positive sentiment might convey satisfaction with the airline's services, a negative sentiment could express dissatisfaction, while neutral sentiment may reflect an impartial observation or a neutral standpoint
19
+ """
20
+ @st.cache_resource
21
+ def load_llm():
22
+ # ... (load_llm function remains identical) ...
23
+ device = "CPU (TensorFlow)"
24
+ try:
25
+ with st.spinner(f"Loading tokenizer and model ({MODEL_NAME}) on {device}..."):
26
+ st.info(f"Using device: **{device}**. Starting model download...")
27
+ start_time = time.time()
28
+ tokenizer = T5Tokenizer.from_pretrained(MODEL_NAME)
29
+ model = TFT5ForConditionalGeneration.from_pretrained(MODEL_NAME)
30
+ end_time = time.time()
31
+ st.success(f"Model {MODEL_NAME} loaded successfully in {end_time - start_time:.2f} seconds!")
32
+ return tokenizer, model, device
33
+ except Exception as e:
34
+ st.error(f"FATAL ERROR LOADING MODEL: {e}")
35
+ st.info("Model load failed.")
36
+ return None, None, None
37
+
38
+ def llm_response(tokenizer, model, device, prompt):
39
+ if tokenizer is None or model is None:
40
+ return "Model not initialized due to previous error."
41
+
42
+ input_ids = tokenizer(prompt, return_tensors="tf").input_ids
43
+ # Set max_length=1 to force a single token output if possible, but 2 is safer for labels
44
+ outputs = model.generate(input_ids, max_length=3, do_sample=False)
45
+
46
+ return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
47
+
48
+ def predict_review_sentiment(tokenizer, model, device, review):
49
+ """
50
+ CLEANED PROMPT FORMATTING.
51
+ The final prompt sent to the model is simple:
52
+ "Classify the sentiment... Respond with only one word. Review: {review text}"
53
+ """
54
+ # FIX: Combine the strict system prompt and the review text clearly
55
+ full_prompt = f"{sys_prompt} Review: '{review}'"
56
+
57
+ # Run the prediction and convert the output to standard casing
58
+ response = llm_response(tokenizer, model, device, full_prompt)
59
+
60
+ # Attempt to normalize the model's output to the three categories
61
+ normalized_response = response.lower().strip()
62
+
63
+ if "positive" in normalized_response:
64
+ return "Positive"
65
+ elif "negative" in normalized_response:
66
+ return "Negative"
67
+ elif "neutral" in normalized_response:
68
+ return "Neutral"
69
+ else:
70
+ # For non-classification outputs like 'hi', return the raw response
71
+ return response
72
+
73
+
74
+ # -------------------- Streamlit UI --------------------
75
+
76
+ # --- 1. Load Resources ---
77
+ tokenizer, model, device = load_llm()
78
+
79
+ # --- 2. System Info Message (Conditional Display) ---
80
+ st.sidebar.markdown("## 📊 Deployment Status")
81
+
82
+ if tokenizer is not None:
83
+ st.sidebar.info(
84
+ f"Model: **{MODEL_NAME}**\n\n"
85
+ f"Framework: **{device}**\n\n"
86
+ "Status: **Local Inference Ready**"
87
+ )
88
+
89
+ # --- 3. Main Application ---
90
+ st.title("✈️ Customer Review Sentiment Analyzer")
91
+ st.markdown(f"Using the TensorFlow-backed **{MODEL_NAME}** model.")
92
+
93
+ st.subheader("Enter the Customer Review:")
94
+
95
+ review_text = st.text_area(
96
+ "Customer Review:",
97
+ height=150,
98
+ placeholder="E.g., The flight was delayed, but the crew was excellent."
99
+ )
100
+
101
+ # Predict button
102
+ if st.button("Predict Sentiment"):
103
+ if not review_text:
104
+ st.warning("Please enter a customer review to predict the sentiment.")
105
+ else:
106
+ with st.spinner('Analyzing sentiment...'):
107
+ pred_sent = predict_review_sentiment(tokenizer, model, device, review_text)
108
+
109
+ # Display result with color coding
110
+ sentiment_text = pred_sent.lower()
111
+ if 'positive' in sentiment_text:
112
+ st.success(f"**Predicted Sentiment:** {pred_sent}")
113
+ elif 'negative' in sentiment_text:
114
+ st.error(f"**Predicted Sentiment:** {pred_sent}")
115
+ elif 'neutral' in sentiment_text:
116
+ st.info(f"**Predicted Sentiment:** {pred_sent}")
117
+ else:
118
+ st.warning(f"**Predicted Sentiment (Model Response):** {pred_sent}")
119
+ else:
120
+ # Display a clear error on the main page if the model load failed
121
+ st.error("Application Failed to Initialize. Model load aborted (likely missing TensorFlow). Check Space logs.")