import gradio as gr from transformers import pipeline # Choose a reliable model (you can change this) MODEL_NAME = "distilbert-base-uncased-finetuned-sst-2-english" # Load the model once sentiment_analyzer = pipeline("sentiment-analysis", model=MODEL_NAME) # Function to analyze text def analyze_text(text): try: result = sentiment_analyzer(text)[0] label = result.get("label", "UNKNOWN") score = round(result.get("score", 3)) return f"{label} ({score})" except Exception as e: return f"Error during inference: {e}" # Gradio interface with gr.Blocks(title="CIS1160 LLM Inference Demo") as demo: gr.Markdown( """ ### Explore Inference Enter any sentence and see how a trained model interprets it. Try clearly positive, negative, and neutral examples. """ ) input_text = gr.Textbox(label="Enter text to analyze", lines=2) output_text = gr.Textbox(label="Model Prediction") run_button = gr.Button("Run Inference") run_button.click(analyze_text, inputs=input_text, outputs=output_text) if __name__ == "__main__": demo.launch()