cheberle commited on
Commit
6564d91
·
1 Parent(s): b0e39c2
Files changed (1) hide show
  1. app.py +25 -39
app.py CHANGED
@@ -1,46 +1,32 @@
1
  import gradio as gr
2
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
- import torch
4
 
5
- # Model and tokenizer loading
6
- model_id = "cheberle/autotrain-35swc-b4r9z"
7
- tokenizer = AutoTokenizer.from_pretrained(model_id)
8
- model = AutoModelForSequenceClassification.from_pretrained(model_id)
9
 
10
- # Move model to GPU if available
11
- device = "cuda" if torch.cuda.is_available() else "cpu"
12
- model = model.to(device)
13
 
14
- def predict(text):
15
- # Tokenize input
16
- inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
17
-
18
- # Move inputs to same device as model
19
- inputs = {k: v.to(device) for k, v in inputs.items()}
20
-
21
- # Get prediction
22
- with torch.no_grad():
23
- outputs = model(**inputs)
24
- predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
25
-
26
- # Get prediction probabilities and labels
27
- probs = predictions[0].tolist()
28
- labels = model.config.id2label
29
-
30
- # Create formatted output
31
- results = {labels[i]: float(probs[i]) for i in range(len(probs))}
32
-
33
- return results
34
 
35
- # Create Gradio interface
36
- iface = gr.Interface(
37
- fn=predict,
38
- inputs=gr.Textbox(label="Input Text"),
39
- outputs=gr.Label(label="Prediction"),
40
- title="Model Prediction Interface",
41
- description=f"Enter text to get predictions from {model_id}",
42
- examples=["Example text to try"]
43
  )
44
 
45
- # Launch the interface
46
- iface.launch()
 
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification
 
3
 
4
+ # Load model and tokenizer
5
+ model_name = "cheberle/autotrain-35swc-b4r9z"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
 
7
 
8
+ # Explicitly define the model configuration if needed
9
+ config = AutoConfig.from_pretrained(model_name)
10
+ model = AutoModelForSequenceClassification.from_pretrained(model_name, config=config)
11
 
12
+ # Inference function
13
+ def classify_text(input_text):
14
+ inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True)
15
+ outputs = model(**inputs)
16
+ probabilities = outputs.logits.softmax(dim=-1).tolist()[0]
17
+ labels = {i: f"Label {i}" for i in range(len(probabilities))} # Define label mapping if needed
18
+ result = {labels[i]: prob for i, prob in enumerate(probabilities)}
19
+ return result
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ # Gradio interface
22
+ interface = gr.Interface(
23
+ fn=classify_text,
24
+ inputs="text",
25
+ outputs="label",
26
+ title="DeepSeek-R1 Text Classification",
27
+ description="Classify text inputs using the DeepSeek-R1 model."
 
28
  )
29
 
30
+ # Launch the app
31
+ if __name__ == "__main__":
32
+ interface.launch()