Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
# Load a Hugging Face model pipeline (sentiment analysis in this example) | |
model = pipeline("sentiment-analysis") | |
# If you don't hafe HF_TOKEN in your enviroment, define it explicitly like this | |
# hf_token = "your_hf_token" | |
# model = pipeline("sentiment-analysis", use_auth_token=hf_token) | |
# Function to perform sentiment analysis | |
def analyze_sentiment(text): | |
results = model(text) | |
return f"Label: {results[0]['label']}, Score: {results[0]['score']:.4f}" | |
# Define the Gradio interface | |
with gr.Blocks() as demo: | |
# Components | |
gr.Markdown("# Welcome to the Sentiment Analysis App!") | |
gr.Markdown("## Enter your text and we'll analyze the sentiment behind it.") | |
with gr.Row(): | |
with gr.Column(): | |
input_text = gr.Textbox(lines=2, placeholder="Enter text to analyze...", label="Input Text") | |
analyze_button = gr.Button("Analyze", elem_id="analyze-btn") | |
with gr.Column(): | |
output_text = gr.Textbox(label="Analysis Result", interactive=False) | |
# Attach function | |
analyze_button.click(fn=analyze_sentiment, inputs=input_text, outputs=output_text) | |
# Add some CSS to style your interface | |
demo.css = """ | |
#analyze-btn { | |
background-color: #4CAF50; | |
color: white; | |
} | |
#analyze-btn:hover { | |
background-color: #45a049; | |
} | |
""" | |
# Launch the app, use share=True to obtain shareable link that works for 72 hours | |
demo.launch(share=True) | |