Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| print("π Loading models...") | |
| # Load models with automatic device selection | |
| sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english") | |
| translator = pipeline("translation_en_to_es", model="Helsinki-NLP/opus-mt-en-es") | |
| print("β Models loaded successfully!") | |
| def analyze_sentiment(text): | |
| if not text.strip(): | |
| return "β οΈ Please enter some text!" | |
| try: | |
| result = sentiment_analyzer(text[:500])[0] | |
| emoji = "π" if result['label'] == "POSITIVE" else "π" | |
| return f"{emoji} **{result['label']}**\nConfidence: {result['score']:.1%}" | |
| except Exception as e: | |
| return f"β Error: {str(e)}" | |
| def translate_text(text): | |
| if not text.strip(): | |
| return "β οΈ Please enter some text!" | |
| try: | |
| result = translator(text[:400])[0] | |
| return result['translation_text'] | |
| except Exception as e: | |
| return f"β Error: {str(e)}" | |
| # Simple UI | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# π€ AI Text Tools") | |
| with gr.Tab("π Sentiment"): | |
| sent_in = gr.Textbox(label="Text", lines=3) | |
| sent_btn = gr.Button("Analyze") | |
| sent_out = gr.Textbox(label="Result") | |
| sent_btn.click(analyze_sentiment, sent_in, sent_out) | |
| with gr.Tab("π Translate"): | |
| trans_in = gr.Textbox(label="English", lines=3) | |
| trans_btn = gr.Button("Translate") | |
| trans_out = gr.Textbox(label="Spanish") | |
| trans_btn.click(translate_text, trans_in, trans_out) | |
| demo.launch() |