Spaces:
Runtime error
Runtime error
Create app.py
#2
by
Emily666666
- opened
app.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# 加载模型和 tokenizer
|
6 |
+
model_name3_p2 = "rajesh426/distilbert-base-uncased_Up_Sampling_Sub_Category_SPEECH_TEXT_DISPLAY_v1"
|
7 |
+
num_labels = 10 # 根据你的数据集设置标签的数量
|
8 |
+
model3_p2 = AutoModelForSequenceClassification.from_pretrained(model_name3_p2, num_labels=num_labels, ignore_mismatched_sizes=True)
|
9 |
+
tokenizer3 = AutoTokenizer.from_pretrained(model_name3_p2)
|
10 |
+
|
11 |
+
# Streamlit 应用
|
12 |
+
st.title("Text Classification with HuggingFace Spaces")
|
13 |
+
st.write("Enter a sentence to classify:")
|
14 |
+
|
15 |
+
user_input = st.text_input("")
|
16 |
+
|
17 |
+
if user_input:
|
18 |
+
inputs = tokenizer3(user_input, return_tensors="pt", padding=True, truncation=True)
|
19 |
+
with torch.no_grad():
|
20 |
+
outputs = model3_p2(**inputs)
|
21 |
+
logits = outputs.logits
|
22 |
+
predicted_class_id = torch.argmax(logits, dim=-1).item()
|
23 |
+
|
24 |
+
st.write(f"Predicted class ID: {predicted_class_id}")
|