Download app.py from alexray/tweets: direct link, hf CLI and curl.
- Browser
- Download file 896 Bytes
-
https://huggingface.co/spaces/alexray/tweets/resolve/main/app.py
- Command line
-
hf download hf://spaces/alexray/tweets/app.py
-
curl -L -o app.py https://huggingface.co/spaces/alexray/tweets/resolve/main/app.py
896 Bytes
| from flask import Flask, request | |
| from transformers import AutoModelForSequenceClassification, AutoTokenizer, TextClassificationPipeline | |
| import torch | |
| app = Flask(__name__) | |
| tokenizer = AutoTokenizer.from_pretrained('alexray/twitter-distilbert') | |
| model = AutoModelForSequenceClassification.from_pretrained('alexray/twitter-distilbert', num_labels=2) | |
| # Create a text classification pipeline | |
| classifier = TextClassificationPipeline(model=model, tokenizer=tokenizer, device=-1) # device=-1 means using CPU | |
| def predict() -> str: | |
| args = request.args | |
| data = args.get("data") | |
| if data is None: | |
| return 'data is none' | |
| # Make prediction using the pipeline | |
| result = classifier(data) | |
| # Get predicted class (assuming it's a binary classification) | |
| predicted_class = result[0]['label'] | |
| http_response = str(predicted_class) | |
| return http_response | |