Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import json
|
| 4 |
+
import time
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
class AdaptiveUI:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.sentiment_model = pipeline("sentiment-analysis")
|
| 11 |
+
self.preferences_file = "user_preferences.json"
|
| 12 |
+
self.load_preferences()
|
| 13 |
+
|
| 14 |
+
def load_preferences(self):
|
| 15 |
+
if os.path.exists(self.preferences_file):
|
| 16 |
+
with open(self.preferences_file, 'r') as f:
|
| 17 |
+
self.preferences = json.load(f)
|
| 18 |
+
else:
|
| 19 |
+
self.preferences = {
|
| 20 |
+
'usage_count': 0,
|
| 21 |
+
'avg_text_length': 100,
|
| 22 |
+
'advanced_mode_uses': 0,
|
| 23 |
+
'last_layout': 'simple',
|
| 24 |
+
'common_features': set(),
|
| 25 |
+
'last_used': None
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
def save_preferences(self):
|
| 29 |
+
# Convert set to list for JSON serialization
|
| 30 |
+
prefs_to_save = self.preferences.copy()
|
| 31 |
+
prefs_to_save['common_features'] = list(self.preferences['common_features'])
|
| 32 |
+
with open(self.preferences_file, 'w') as f:
|
| 33 |
+
json.dump(prefs_to_save, f)
|
| 34 |
+
|
| 35 |
+
def should_show_advanced(self):
|
| 36 |
+
return self.preferences['usage_count'] > 5 or self.preferences['advanced_mode_uses'] > 2
|
| 37 |
+
|
| 38 |
+
def update_preferences(self, text_length, used_features):
|
| 39 |
+
self.preferences['usage_count'] += 1
|
| 40 |
+
self.preferences['avg_text_length'] = (
|
| 41 |
+
(self.preferences['avg_text_length'] * (self.preferences['usage_count'] - 1) + text_length)
|
| 42 |
+
/ self.preferences['usage_count']
|
| 43 |
+
)
|
| 44 |
+
if 'advanced' in used_features:
|
| 45 |
+
self.preferences['advanced_mode_uses'] += 1
|
| 46 |
+
self.preferences['common_features'].update(used_features)
|
| 47 |
+
self.preferences['last_used'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 48 |
+
self.save_preferences()
|
| 49 |
+
|
| 50 |
+
def analyze(self, text, show_advanced):
|
| 51 |
+
# Update usage patterns
|
| 52 |
+
self.update_preferences(len(text), {'advanced'} if show_advanced else {'basic'})
|
| 53 |
+
|
| 54 |
+
# Get sentiment analysis
|
| 55 |
+
result = self.sentiment_model(text)[0]
|
| 56 |
+
|
| 57 |
+
# Determine interface adaptations
|
| 58 |
+
adaptations = []
|
| 59 |
+
|
| 60 |
+
# Adapt based on text length
|
| 61 |
+
if len(text) > self.preferences['avg_text_length'] * 1.5:
|
| 62 |
+
adaptations.append("Expanded text area for longer inputs")
|
| 63 |
+
elif len(text) < self.preferences['avg_text_length'] * 0.5:
|
| 64 |
+
adaptations.append("Compact text area for brief inputs")
|
| 65 |
+
|
| 66 |
+
# Adapt based on usage frequency
|
| 67 |
+
if self.preferences['usage_count'] > 10:
|
| 68 |
+
adaptations.append("Advanced features unlocked")
|
| 69 |
+
|
| 70 |
+
# Adapt based on time of day
|
| 71 |
+
current_hour = datetime.now().hour
|
| 72 |
+
if current_hour >= 20 or current_hour <= 6:
|
| 73 |
+
adaptations.append("Night mode activated")
|
| 74 |
+
|
| 75 |
+
return {
|
| 76 |
+
'sentiment': result['label'],
|
| 77 |
+
'confidence': f"{result['score']:.2%}",
|
| 78 |
+
'adaptations': "\n".join(adaptations),
|
| 79 |
+
'show_advanced': self.should_show_advanced(),
|
| 80 |
+
'input_size': 'large' if self.preferences['avg_text_length'] > 150 else 'normal'
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
def create_interface():
|
| 84 |
+
ui = AdaptiveUI()
|
| 85 |
+
|
| 86 |
+
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
| 87 |
+
gr.Markdown("# Adaptive Sentiment Analysis")
|
| 88 |
+
|
| 89 |
+
# Input Section
|
| 90 |
+
with gr.Row():
|
| 91 |
+
with gr.Column(scale=2):
|
| 92 |
+
text_input = gr.Textbox(
|
| 93 |
+
label="Enter Text",
|
| 94 |
+
placeholder=f"Suggested length: {int(ui.preferences['avg_text_length'])} characters",
|
| 95 |
+
lines=4 if ui.preferences['avg_text_length'] > 150 else 2
|
| 96 |
+
)
|
| 97 |
+
show_advanced = gr.Checkbox(
|
| 98 |
+
label="Advanced Mode",
|
| 99 |
+
value=ui.should_show_advanced(),
|
| 100 |
+
visible=ui.preferences['usage_count'] > 5
|
| 101 |
+
)
|
| 102 |
+
analyze_btn = gr.Button("Analyze Text")
|
| 103 |
+
|
| 104 |
+
# Output Section
|
| 105 |
+
with gr.Column(scale=2):
|
| 106 |
+
sentiment_output = gr.Label(label="Sentiment")
|
| 107 |
+
with gr.Group(visible=False) as advanced_group:
|
| 108 |
+
confidence_output = gr.Label(label="Confidence")
|
| 109 |
+
adaptations_output = gr.Textbox(
|
| 110 |
+
label="Interface Adaptations",
|
| 111 |
+
lines=3
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
def process_text(text, show_adv):
|
| 115 |
+
result = ui.analyze(text, show_adv)
|
| 116 |
+
|
| 117 |
+
# Update interface based on adaptations
|
| 118 |
+
return {
|
| 119 |
+
sentiment_output: result['sentiment'],
|
| 120 |
+
confidence_output: result['confidence'],
|
| 121 |
+
adaptations_output: result['adaptations'],
|
| 122 |
+
advanced_group: gr.Group(visible=show_adv),
|
| 123 |
+
text_input: gr.Textbox(lines=4 if result['input_size'] == 'large' else 2),
|
| 124 |
+
show_advanced: gr.Checkbox(visible=result['show_advanced'])
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
# Event handlers
|
| 128 |
+
analyze_btn.click(
|
| 129 |
+
fn=process_text,
|
| 130 |
+
inputs=[text_input, show_advanced],
|
| 131 |
+
outputs=[
|
| 132 |
+
sentiment_output,
|
| 133 |
+
confidence_output,
|
| 134 |
+
adaptations_output,
|
| 135 |
+
advanced_group,
|
| 136 |
+
text_input,
|
| 137 |
+
show_advanced
|
| 138 |
+
]
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
return interface
|
| 142 |
+
|
| 143 |
+
# Launch the app
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
demo = create_interface()
|
| 146 |
+
demo.launch()
|