test / app.py
Renecto's picture
Create app.py
2ee2207 verified
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()