HaniaEmaan commited on
Commit
447a07c
·
verified ·
1 Parent(s): 5d49dbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -2,28 +2,36 @@ import gradio as gr
2
  import spaces
3
  from transformers import pipeline
4
 
5
- # Load pipeline
6
- model_id = "HaniaEmaan/code-switching-codesaviours-si26-hania"
 
7
  nlp_pipeline = pipeline(
8
  "token-classification",
9
  model=model_id,
10
- aggregation_strategy="simple",
11
  ignore_labels=[]
12
  )
13
 
14
- # The @spaces.GPU decorator dynamically requests GPU compute for inference
15
  @spaces.GPU
16
  def classify_text(text):
17
- predictions = nlp_pipeline(text)
18
- formatted_output = []
19
- for p in predictions:
20
- formatted_output.append(f"Word/Phrase: '{p['word']}' | Label: {p['entity_group']} | Score: {p['score']:.2f}")
21
- return "\n".join(formatted_output)
 
 
 
 
 
 
 
 
 
22
 
23
- # Gradio Interface
24
  demo = gr.Interface(
25
  fn=classify_text,
26
- inputs=gr.Textbox(placeholder="Enter a sentence... e.g., Photo postain checko"),
27
  outputs="textbox",
28
  title="Code-Switched Language Identification (ZeroGPU)",
29
  description="XLM-RoBERTa Token Classification powered by Hugging Face ZeroGPU."
 
2
  import spaces
3
  from transformers import pipeline
4
 
5
+ model_id = "your-actual-username/code-switching-codesaviours-si26-hania"
6
+
7
+ # Set aggregation_strategy to None to stop merging words into single chunks
8
  nlp_pipeline = pipeline(
9
  "token-classification",
10
  model=model_id,
11
+ aggregation_strategy=None,
12
  ignore_labels=[]
13
  )
14
 
 
15
  @spaces.GPU
16
  def classify_text(text):
17
+ # Split input by words to classify each token individually
18
+ words = text.split()
19
+ results = []
20
+
21
+ for word in words:
22
+ predictions = nlp_pipeline(word)
23
+ if predictions:
24
+ # Pick the top token prediction for each individual word
25
+ top_pred = predictions[0]
26
+ label = top_pred.get('entity', top_pred.get('entity_group', 'UNK'))
27
+ score = top_pred['score']
28
+ results.append(f"Word: '{word}' | Label: {label} | Score: {score:.2f}")
29
+
30
+ return "\n".join(results)
31
 
 
32
  demo = gr.Interface(
33
  fn=classify_text,
34
+ inputs=gr.Textbox(placeholder="Enter text..."),
35
  outputs="textbox",
36
  title="Code-Switched Language Identification (ZeroGPU)",
37
  description="XLM-RoBERTa Token Classification powered by Hugging Face ZeroGPU."