Skip to content

Instantly share code, notes, and snippets.

@bbelderbos
Created February 20, 2025 09:49
Show Gist options
  • Save bbelderbos/3342b479dad904397afb31d6db42d965 to your computer and use it in GitHub Desktop.
Save bbelderbos/3342b479dad904397afb31d6db42d965 to your computer and use it in GitHub Desktop.
# /// script
# dependencies = [
# "marvin",
# ]
# ///
import os
import sys
import argparse
import marvin
from pydantic import BaseModel, Field
if "MARVIN_OPENAI_API_KEY" not in os.environ:
sys.exit("Set MARVIN_OPENAI_API_KEY in env first")
class SentimentResult(BaseModel):
polarity: float = Field(..., description="Sentiment polarity (-1 to 1)")
subjectivity: float = Field(..., description="Subjectivity (0 to 1)")
summary: str = Field(..., description="Short summary of sentiment")
@marvin.fn
def analyze_sentiment(text: str) -> SentimentResult:
"""
Given a text input, return a sentiment analysis result.
The output includes polarity (-1 to 1), subjectivity (0 to 1), and a summary.
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Analyze sentiment of a given text.")
parser.add_argument("--text", required=True, help="Text to analyze")
args = parser.parse_args()
sentiment = analyze_sentiment(args.text)
print(f"Polarity: {sentiment.polarity}")
print(f"Subjectivity: {sentiment.subjectivity}")
print(f"Summary: {sentiment.summary}")
@bbelderbos
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment