Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +28 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModel
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.linear_model import LogisticRegression
|
| 6 |
+
|
| 7 |
+
# Load Hugging Face model
|
| 8 |
+
model_name = "bert-base-uncased"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModel.from_pretrained(model_name)
|
| 11 |
+
|
| 12 |
+
# Function to get text embeddings
|
| 13 |
+
def get_embedding(text):
|
| 14 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
outputs = model(**inputs)
|
| 17 |
+
return outputs.last_hidden_state[:, 0, :].numpy()
|
| 18 |
+
|
| 19 |
+
# Sample dataset (sentiment analysis)
|
| 20 |
+
texts = ["I love this!", "This is terrible.", "Fantastic experience!", "I hate it."]
|
| 21 |
+
labels = [1, 0, 1, 0] # 1 = Positive, 0 = Negative
|
| 22 |
+
|
| 23 |
+
# Convert text to embeddings
|
| 24 |
+
X = np.vstack([get_embedding(text) for text in texts])
|
| 25 |
+
y = np.array(labels)
|
| 26 |
+
|
| 27 |
+
# Train Logistic Regression model
|
| 28 |
+
clf =
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
scikit-learn
|
| 3 |
+
torch
|
| 4 |
+
streamlit
|
| 5 |
+
numpy
|