File size: 2,445 Bytes
3e7fb54
 
7def5a8
3e7fb54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e79c88
3e7fb54
7def5a8
 
3e7fb54
 
 
0e79c88
3e7fb54
0e79c88
 
d7a68f8
0e79c88
d7a68f8
 
0e79c88
 
 
 
 
 
 
 
 
 
 
 
 
3e7fb54
 
 
 
0e79c88
 
3e7fb54
0e79c88
 
3e7fb54
 
 
 
 
 
 
 
 
047d22b
3e7fb54
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import warnings
import gradio as gr
from transformers import AutoTokenizer, AutoConfig
import torch
from custom_model import CustomModel

# Suppress the FutureWarning
warnings.filterwarnings("ignore", category=FutureWarning, module="torch")

# Load the model and tokenizer
model_name = "deepseek-ai/DeepSeek-V3"
revision = "main"  # Specify the revision directly

print(f"Loading tokenizer from {model_name}...")
tokenizer = AutoTokenizer.from_pretrained(model_name, revision=revision, trust_remote_code=True)

print(f"Loading configuration from {model_name}...")
config = AutoConfig.from_pretrained(model_name, revision=revision, trust_remote_code=True)

print(f"Loading model from {model_name}...")
model = CustomModel.from_pretrained(model_name, config=config, revision=revision, trust_remote_code=True)

# Check if the model loaded successfully
if model is None:
    print("Failed to load model. Exiting...")
    exit(1)
else:
    print("Model loaded successfully.")

# Define the text classification function
def classify_text(text):
    try:
        # Tokenize the input text
        inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
        # Pass the inputs to the model
        logits = model(**inputs)
        # Get the probabilities
        probabilities = torch.softmax(logits, dim=-1).tolist()[0]
        # Get the predicted class
        predicted_class = torch.argmax(logits, dim=-1).item()
        return {
            "Predicted Class": predicted_class,
            "Probabilities": probabilities
        }
    except Exception as e:
        print(f"Error during text classification: {e}")
        return {
            "Predicted Class": "Error",
            "Probabilities": []
        }

# Create a Gradio interface
try:
    iface = gr.Interface(
        fn=classify_text,  # Function to call
        inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),  # Input component
        outputs=[
            gr.Label(label="Predicted Class"),  # Output component for predicted class
            gr.Label(label="Probabilities")  # Output component for probabilities
        ],
        title="DeepSeek-V3 Text Classification",
        description="Classify text using the DeepSeek-V3 model."
    )
except Exception as e:
    print(f"Failed to create Gradio interface: {e}")

# Launch the interface
try:
    iface.launch()
except Exception as e:
    print(f"Failed to launch Gradio interface: {e}")