Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,58 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, PreTrainedTokenizer, AutoConfig
|
| 4 |
+
import torch
|
| 5 |
+
from custom_model import CustomModel
|
| 6 |
+
|
| 7 |
+
# Suppress the FutureWarning
|
| 8 |
+
warnings.filterwarnings("ignore", category=FutureWarning, module="torch")
|
| 9 |
+
|
| 10 |
+
# Load the model and tokenizer
|
| 11 |
+
model_name = "deepseek-ai/DeepSeek-V3"
|
| 12 |
+
revision = "main" # Specify the revision directly
|
| 13 |
+
|
| 14 |
+
print(f"Loading tokenizer from {model_name}...")
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, revision=revision, trust_remote_code=True)
|
| 16 |
+
|
| 17 |
+
print(f"Loading configuration from {model_name}...")
|
| 18 |
+
config = AutoConfig.from_pretrained(model_name, revision=revision, trust_remote_code=True)
|
| 19 |
+
|
| 20 |
+
print(f"Loading model from {model_name}...")
|
| 21 |
+
model = CustomModel.from_pretrained(model_name, config=config, revision=revision, trust_remote_code=True)
|
| 22 |
+
|
| 23 |
+
if model is None:
|
| 24 |
+
print("Failed to load model.")
|
| 25 |
+
else:
|
| 26 |
+
print("Model loaded successfully.")
|
| 27 |
+
|
| 28 |
+
def classify_text(text):
|
| 29 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 30 |
+
outputs = model(**inputs)
|
| 31 |
+
logits = outputs.logits
|
| 32 |
+
probabilities = torch.softmax(logits, dim=-1).tolist()[0]
|
| 33 |
+
predicted_class = torch.argmax(logits, dim=-1).item()
|
| 34 |
+
return {
|
| 35 |
+
"Predicted Class": predicted_class,
|
| 36 |
+
"Probabilities": probabilities
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
# Create a Gradio interface
|
| 40 |
+
try:
|
| 41 |
+
iface = gr.Interface(
|
| 42 |
+
fn=classify_text,
|
| 43 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter text here..."), # Corrected here
|
| 44 |
+
outputs=[
|
| 45 |
+
gr.outputs.Label(label="Predicted Class"),
|
| 46 |
+
gr.outputs.Label(label="Probabilities")
|
| 47 |
+
],
|
| 48 |
+
title="DeepSeek-V3 Text Classification",
|
| 49 |
+
description="Classify text using the DeepSeek-V3 model."
|
| 50 |
+
)
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Failed to create Gradio interface: {e}")
|
| 53 |
+
|
| 54 |
+
# Launch the interface
|
| 55 |
+
try:
|
| 56 |
+
iface.launch()
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"Failed to launch Gradio interface: {e}")
|