Spaces:
Build error
Build error
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146import gradio as grfrom transformers import pipelineimport jsonimport timefrom datetime import datetimeimport os
class AdaptiveUI: def __init__(self): self.sentiment_model = pipeline("sentiment-analysis") self.preferences_file = "user_preferences.json" self.load_preferences() def load_preferences(self): if os.path.exists(self.preferences_file): with open(self.preferences_file, 'r') as f: self.preferences = json.load(f) else: self.preferences = { 'usage_count': 0, 'avg_text_length': 100, 'advanced_mode_uses': 0, 'last_layout': 'simple', 'common_features': set(), 'last_used': None } def save_preferences(self): # Convert set to list for JSON serialization prefs_to_save = self.preferences.copy() prefs_to_save['common_features'] = list(self.preferences['common_features']) with open(self.preferences_file, 'w') as f: json.dump(prefs_to_save, f)
def should_show_advanced(self): return self.preferences['usage_count'] > 5 or self.preferences['advanced_mode_uses'] > 2
def update_preferences(self, text_length, used_features): self.preferences['usage_count'] += 1 self.preferences['avg_text_length'] = ( (self.preferences['avg_text_length'] * (self.preferences['usage_count'] - 1) + text_length) / self.preferences['usage_count'] ) if 'advanced' in used_features: self.preferences['advanced_mode_uses'] += 1 self.preferences['common_features'].update(used_features) self.preferences['last_used'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") self.save_preferences()
def analyze(self, text, show_advanced): # Update usage patterns self.update_preferences(len(text), {'advanced'} if show_advanced else {'basic'}) # Get sentiment analysis result = self.sentiment_model(text)[0] # Determine interface adaptations adaptations = [] # Adapt based on text length if len(text) > self.preferences['avg_text_length'] * 1.5: adaptations.append("Expanded text area for longer inputs") elif len(text) < self.preferences['avg_text_length'] * 0.5: adaptations.append("Compact text area for brief inputs") # Adapt based on usage frequency if self.preferences['usage_count'] > 10: adaptations.append("Advanced features unlocked") # Adapt based on time of day current_hour = datetime.now().hour if current_hour >= 20 or current_hour <= 6: adaptations.append("Night mode activated") return { 'sentiment': result['label'], 'confidence': f"{result['score']:.2%}", 'adaptations': "\n".join(adaptations), 'show_advanced': self.should_show_advanced(), 'input_size': 'large' if self.preferences['avg_text_length'] > 150 else 'normal' }
def create_interface(): ui = AdaptiveUI() with gr.Blocks(theme=gr.themes.Soft()) as interface: gr.Markdown("# Adaptive Sentiment Analysis") # Input Section with gr.Row(): with gr.Column(scale=2): text_input = gr.Textbox( label="Enter Text", placeholder=f"Suggested length: {int(ui.preferences['avg_text_length'])} characters", lines=4 if ui.preferences['avg_text_length'] > 150 else 2 ) show_advanced = gr.Checkbox( label="Advanced Mode", value=ui.should_show_advanced(), visible=ui.preferences['usage_count'] > 5 ) analyze_btn = gr.Button("Analyze Text") # Output Section with gr.Column(scale=2): sentiment_output = gr.Label(label="Sentiment") with gr.Group(visible=False) as advanced_group: confidence_output = gr.Label(label="Confidence") adaptations_output = gr.Textbox( label="Interface Adaptations", lines=3 )
def process_text(text, show_adv): result = ui.analyze(text, show_adv) # Update interface based on adaptations return { sentiment_output: result['sentiment'], confidence_output: result['confidence'], adaptations_output: result['adaptations'], advanced_group: gr.Group(visible=show_adv), text_input: gr.Textbox(lines=4 if result['input_size'] == 'large' else 2), show_advanced: gr.Checkbox(visible=result['show_advanced']) }
# Event handlers analyze_btn.click( fn=process_text, inputs=[text_input, show_advanced], outputs=[ sentiment_output, confidence_output, adaptations_output, advanced_group, text_input, show_advanced ] )
return interface
# Launch the appif __name__ == "__main__": demo = create_interface() demo.launch()app.pyAdded+146