alveKamruzzaman commited on
Commit
4d44920
·
verified ·
1 Parent(s): f28b996

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -7
app.py CHANGED
@@ -7,6 +7,10 @@ model_name = "TextLabRUET/xlm-r_based_bangla_sentence_classifier"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
 
 
 
 
 
10
  # Mapping predicted class to Bangla sentence types
11
  class_mapping = {
12
  0: "Assertive Sentence (বর্ণনামূলক বাক্য)",
@@ -17,29 +21,40 @@ class_mapping = {
17
  }
18
 
19
  # Function for prediction
20
- def predict(sentence):
 
21
  inputs = tokenizer(sentence, return_tensors="pt", truncation=True, padding=True, max_length=128)
 
 
 
 
 
22
  with torch.no_grad():
23
  outputs = model(**inputs)
 
 
24
  logits = outputs.logits
25
- predicted_class = torch.argmax(logits, dim=1).item()
26
 
 
27
  sentence_type = class_mapping.get(predicted_class, "Unknown Sentence Type")
28
  return f"Predicted Class: {sentence_type}"
29
 
30
  # Create Gradio UI
31
  iface = gr.Interface(
32
- fn=predict,
33
  inputs=gr.Textbox(lines=2, placeholder="Enter a Bangla sentence..."),
34
  outputs="text",
35
  title="Bangla Sentence Classifier",
36
  description=(
37
- "This model was trained on a curated Bangla dataset by **TextLab RUET**. "
38
- "It classifies Bangla sentences into five distinct categories: Assertive, Interrogative, Imperative, Optative, and Exclamatory "
39
- "using the **XLM-R model**. Enter a Bangla sentence below to see how our model interprets it!"
 
 
40
  ),
41
  theme="compact"
42
  )
43
 
44
  # Launch the Gradio app
45
- iface.launch(share=True)
 
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
 
10
+ # Set device (GPU if available, otherwise CPU)
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+ model.to(device)
13
+
14
  # Mapping predicted class to Bangla sentence types
15
  class_mapping = {
16
  0: "Assertive Sentence (বর্ণনামূলক বাক্য)",
 
21
  }
22
 
23
  # Function for prediction
24
+ def predict_bangla_sentence(sentence):
25
+ # Tokenize the input sentence
26
  inputs = tokenizer(sentence, return_tensors="pt", truncation=True, padding=True, max_length=128)
27
+
28
+ # Move input tensors to the same device as the model
29
+ inputs = {key: val.to(device) for key, val in inputs.items()}
30
+
31
+ # Perform inference
32
  with torch.no_grad():
33
  outputs = model(**inputs)
34
+
35
+ # Get the predicted class
36
  logits = outputs.logits
37
+ predicted_class = torch.argmax(logits, dim=-1).item()
38
 
39
+ # Return the predicted sentence type
40
  sentence_type = class_mapping.get(predicted_class, "Unknown Sentence Type")
41
  return f"Predicted Class: {sentence_type}"
42
 
43
  # Create Gradio UI
44
  iface = gr.Interface(
45
+ fn=predict_bangla_sentence,
46
  inputs=gr.Textbox(lines=2, placeholder="Enter a Bangla sentence..."),
47
  outputs="text",
48
  title="Bangla Sentence Classifier",
49
  description=(
50
+ "This model was trained by **TextLabRUET** using an extensive Bangla dataset. "
51
+ "It classifies Bangla sentences into five types: Assertive, Interrogative, Imperative, Optative, and Exclamatory "
52
+ "using the **XLM-R model**. Enter a Bangla sentence below to see how our model analyzes it!\n\n"
53
+ "Note: While we aim for accuracy, the model may occasionally misclassify sentences due to dataset limitations. "
54
+ "We apologize for any errors and appreciate your understanding."
55
  ),
56
  theme="compact"
57
  )
58
 
59
  # Launch the Gradio app
60
+ iface.launch(share=True)