Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
# Simple text generation "Hello World" example
|
| 4 |
+
def huggingface_hello_world():
|
| 5 |
+
# Use a pre-trained text generation model
|
| 6 |
+
generator = pipeline('text-generation', model='gpt2')
|
| 7 |
+
|
| 8 |
+
# Generate a response to a "Hello" prompt
|
| 9 |
+
hello_response = generator("Hello, how are you today?", max_length=50, num_return_sequences=1)
|
| 10 |
+
|
| 11 |
+
# Print the generated text
|
| 12 |
+
print("Model's Hello World Response:")
|
| 13 |
+
print(hello_response[0]['generated_text'])
|
| 14 |
+
|
| 15 |
+
# Another example with text classification
|
| 16 |
+
def huggingface_sentiment_hello():
|
| 17 |
+
# Use a sentiment analysis model
|
| 18 |
+
classifier = pipeline('sentiment-analysis')
|
| 19 |
+
|
| 20 |
+
# Classify a "Hello World" sentiment
|
| 21 |
+
sentiment = classifier("Hello World! This is a nice day.")
|
| 22 |
+
|
| 23 |
+
# Print the sentiment result
|
| 24 |
+
print("\nSentiment Analysis of 'Hello World':")
|
| 25 |
+
print(f"Sentiment: {sentiment[0]['label']}")
|
| 26 |
+
print(f"Confidence: {sentiment[0]['score']:.2f}")
|
| 27 |
+
|
| 28 |
+
# Run the examples
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
huggingface_hello_world()
|
| 31 |
+
huggingface_sentiment_hello()
|