122Uswa commited on
Commit
2684da1
·
verified ·
1 Parent(s): 43a9d1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -20
app.py CHANGED
@@ -1,50 +1,74 @@
 
 
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
  MODEL_NAME = "122Uswa/code-switching-codesaviours-si26-uswa"
5
 
6
- classifier = pipeline(
7
- "token-classification",
8
- model=MODEL_NAME,
9
- aggregation_strategy="simple"
10
- )
11
 
 
 
12
  def predict_language(text):
13
  if not text.strip():
14
  return "Please enter a sentence."
15
 
16
  words = text.split()
17
- results = classifier(text)
18
 
19
- output = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- for word in words:
22
- label = "UNKNOWN"
23
 
24
- # Find prediction overlapping with this word
25
- for result in results:
26
- predicted_word = result["word"].strip()
27
 
28
- if predicted_word.lower() == word.lower():
29
- label = result["entity_group"]
30
- break
31
 
32
- output.append(f"{word} → {label}")
33
 
34
- return "\n".join(output)
35
 
36
  demo = gr.Interface(
37
  fn=predict_language,
38
  inputs=gr.Textbox(
39
  label="Roman Urdu / English Sentence",
40
- placeholder="Yaar mujhe time chahiye for this assignment",
41
  lines=3
42
  ),
43
  outputs=gr.Textbox(
44
  label="Language Identification"
45
  ),
46
  title="Roman Urdu–English Language Identifier",
47
- description="Identifies each word as Urdu (URD), English (ENG), or Mixed (MIX)."
48
  )
49
 
50
  demo.launch()
 
1
+ import spaces
2
+
3
  import gradio as gr
4
+ import torch
5
+ from transformers import AutoTokenizer, AutoModelForTokenClassification
6
 
7
  MODEL_NAME = "122Uswa/code-switching-codesaviours-si26-uswa"
8
 
9
+ # Load tokenizer and model
10
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
+ model = AutoModelForTokenClassification.from_pretrained(MODEL_NAME)
12
+
13
+ model.eval()
14
 
15
+
16
+ @spaces.GPU(duration=60)
17
  def predict_language(text):
18
  if not text.strip():
19
  return "Please enter a sentence."
20
 
21
  words = text.split()
 
22
 
23
+ # Tokenize words separately so word_ids() can map predictions
24
+ inputs = tokenizer(
25
+ words,
26
+ is_split_into_words=True,
27
+ return_tensors="pt",
28
+ truncation=True
29
+ )
30
+
31
+ with torch.no_grad():
32
+ outputs = model(**inputs)
33
+
34
+ predictions = torch.argmax(outputs.logits, dim=2)[0].tolist()
35
+
36
+ word_ids = tokenizer(
37
+ words,
38
+ is_split_into_words=True,
39
+ truncation=True
40
+ ).word_ids()
41
+
42
+ results = []
43
+ previous_word = None
44
+
45
+ for prediction, word_id in zip(predictions, word_ids):
46
+
47
+ if word_id is None or word_id == previous_word:
48
+ continue
49
 
50
+ word = words[word_id]
51
+ label = model.config.id2label[prediction]
52
 
53
+ results.append(f"{word} → {label}")
 
 
54
 
55
+ previous_word = word_id
 
 
56
 
57
+ return "\n".join(results)
58
 
 
59
 
60
  demo = gr.Interface(
61
  fn=predict_language,
62
  inputs=gr.Textbox(
63
  label="Roman Urdu / English Sentence",
64
+ placeholder="Yaar mujhe thora time chahiye for this assignment",
65
  lines=3
66
  ),
67
  outputs=gr.Textbox(
68
  label="Language Identification"
69
  ),
70
  title="Roman Urdu–English Language Identifier",
71
+ description="Identifies each word as URD, ENG, or MIX."
72
  )
73
 
74
  demo.launch()