willco-afk commited on
Commit
e7075f5
·
verified ·
1 Parent(s): 0ad3b0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -26
app.py CHANGED
@@ -1,33 +1,24 @@
1
  import gradio as gr
2
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
  import torch
 
4
 
5
- # Define the model name correctly
6
- model_name = "willco-afk/my-model-name"
 
7
 
8
- # Load the model and tokenizer from Hugging Face Hub
9
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
10
- tokenizer = AutoTokenizer.from_pretrained(model_name)
11
-
12
- # Function to predict language from input text
13
- def predict_language(text):
14
- # Tokenize the input text
15
- inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
16
-
17
- # Get predictions from the model
18
  with torch.no_grad():
19
- outputs = model(**inputs)
20
-
21
- # Get the logits (raw predictions) and apply softmax to get probabilities
22
- logits = outputs.logits
23
- predicted_class = logits.argmax(dim=-1).item()
24
-
25
- # Return the predicted class label (you can map this to your language labels)
26
- label_map = {0: "english", 1: "spanish", 2: "tagalog"}
27
- return label_map.get(predicted_class, "Unknown")
28
 
29
- # Create a Gradio interface for the model
30
- iface = gr.Interface(fn=predict_language, inputs="text", outputs="text", title="Slang Language Classifier")
 
 
 
31
 
32
- # Launch the interface
33
- iface.launch()
 
1
  import gradio as gr
 
2
  import torch
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
 
5
+ # Load the model and tokenizer
6
+ tokenizer = AutoTokenizer.from_pretrained("willco-afk/my-model-name")
7
+ model = AutoModelForSequenceClassification.from_pretrained("willco-afk/my-model-name")
8
 
9
+ # Function to classify input text
10
+ def classify_text(text):
11
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
 
 
 
 
 
 
 
12
  with torch.no_grad():
13
+ logits = model(**inputs).logits
14
+ predicted_class = logits.argmax().item() # Get the predicted class
15
+ return f"Predicted class: {predicted_class}"
 
 
 
 
 
 
16
 
17
+ # Create a Gradio interface with customized layout
18
+ demo = gr.Interface(fn=classify_text,
19
+ inputs=gr.Textbox(label="Enter your text"),
20
+ outputs=gr.Textbox(label="Prediction"),
21
+ live=True) # This option allows live feedback as you type
22
 
23
+ # Launch the Gradio interface
24
+ demo.launch()