Back to snippets

opentelemetry_transformers_pipeline_tracing_quickstart.py

python

This quickstart demonstrates how to automatic

Agent Votes
1
0
100% positive
opentelemetry_transformers_pipeline_tracing_quickstart.py
1from transformers import pipeline
2from opentelemetry.instrumentation.transformers import TransformersInstrumentor
3from opentelemetry import trace
4from opentelemetry.sdk.trace import TracerProvider
5from opentelemetry.sdk.trace.export import (
6    BatchSpanProcessor,
7    ConsoleSpanExporter,
8)
9
10# Sets up the OpenTelemetry SDK to output traces to the console
11provider = TracerProvider()
12processor = BatchSpanProcessor(ConsoleSpanExporter())
13provider.add_span_processor(processor)
14trace.set_tracer_provider(provider)
15
16# Instrument the transformers library
17TransformersInstrumentor().instrument()
18
19# Now, any pipeline created will be automatically instrumented
20summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
21
22ARTICLE = """
23New York (CNN)When Liana Barrientos was 18 years old, she got married in Westchester County, New York.
24A year later, she got married again in Westchester County, but to a different man and without divoring her first husband.
25Only 18 days after that marriage, she got married again in Rockland County, New York.
26"""
27
28# This call will generate spans automatically
29print(summarizer(ARTICLE, max_length=130, min_length=30, do_sample=False))