deepseekv3 / app.py
sapthesh's picture
Update app.py
047d22b verified
raw
history blame
2.45 kB
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}")