Created
February 20, 2025 09:49
-
-
Save bbelderbos/3342b479dad904397afb31d6db42d965 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /// 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}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
About Inline Script Metadata -> https://pybit.es/articles/create-project-less-python-utilities-with-uv-and-inline-script-metadata/