File size: 1,069 Bytes
2ee2207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from transformers import pipeline

# Simple text generation "Hello World" example
def huggingface_hello_world():
    # Use a pre-trained text generation model
    generator = pipeline('text-generation', model='gpt2')
    
    # Generate a response to a "Hello" prompt
    hello_response = generator("Hello, how are you today?", max_length=50, num_return_sequences=1)
    
    # Print the generated text
    print("Model's Hello World Response:")
    print(hello_response[0]['generated_text'])

# Another example with text classification
def huggingface_sentiment_hello():
    # Use a sentiment analysis model
    classifier = pipeline('sentiment-analysis')
    
    # Classify a "Hello World" sentiment
    sentiment = classifier("Hello World! This is a nice day.")
    
    # Print the sentiment result
    print("\nSentiment Analysis of 'Hello World':")
    print(f"Sentiment: {sentiment[0]['label']}")
    print(f"Confidence: {sentiment[0]['score']:.2f}")

# Run the examples
if __name__ == "__main__":
    huggingface_hello_world()
    huggingface_sentiment_hello()