Propensity / app.py
HARISARAVANANM's picture
Update app.py
5e0d588 verified
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()