Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,24 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
import torch
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
tokenizer =
|
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 |
-
|
20 |
-
|
21 |
-
|
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
|
30 |
-
|
|
|
|
|
|
|
31 |
|
32 |
-
# Launch the interface
|
33 |
-
|
|
|
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()
|