File size: 1,658 Bytes
2541f1b
28578be
 
2583ba2
 
 
 
 
 
 
3299e8b
 
40ffc63
3299e8b
 
 
40ffc63
 
28578be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import os
from transformers import AutoTokenizer, AutoModel

# Assuming you have set the HF_TOKEN environment variable with your Hugging Face token
huggingface_token = os.getenv('HF_TOKEN')

# Set up the token to use with the Hugging Face API
if huggingface_token is not None:
    os.environ['HUGGINGFACE_CO_API_TOKEN'] = huggingface_token
    tokenizer = AutoTokenizer.from_pretrained("Tokymin/Mood_Anxiety_Disorder_Classify_Model")
else:
    print("error, no token")
    exit(0)
model = AutoModelForSequenceClassification.from_pretrained("Tokymin/Mood_Anxiety_Disorder_Classify_Model",
                                                               num_labels=8)
model.eval()

def predict(text):
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits
    probabilities = torch.softmax(logits, dim=1).squeeze()
    # 假设每个类别(SAS_Class和SDS_Class)都有4个概率值
    sas_probs = probabilities[:4]  # 获取SAS_Class的概率
    sds_probs = probabilities[4:]  # 获取SDS_Class的概率
    return sas_probs, sds_probs

# 创建Streamlit应用
st.title("Multi-label Classification App")

# 用户输入文本
user_input = st.text_area("Enter text here", "Type something...")

if st.button("Predict"):
    # 显示预测结果
    sas_probs, sds_probs = predict(user_input)
    st.write("SAS_Class probabilities:", sas_probs.numpy())
    st.write("SDS_Class probabilities:", sds_probs.numpy())