abcd1234davidchen commited on
Commit
41bb39e
·
1 Parent(s): 24c14c8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -3
app.py CHANGED
@@ -2,6 +2,7 @@ import torch
2
  import torch.nn as nn
3
  from transformers import AutoTokenizer, AutoModel
4
  import gradio as gr
 
5
 
6
  class StanceClassifier(nn.Module):
7
  def __init__(self,transformer_model, num_classes, dropout_rate=0.6):
@@ -47,14 +48,19 @@ def predict_stance(text):
47
  return labels[predicted_class], confidence
48
 
49
  def gradio_interface(text):
50
- stance, conf = predict_stance(text)
51
- return f"Predicted Stance: {stance} with confidence {conf:.4f}"
 
 
 
 
 
52
 
53
  def ui():
54
  gr.Interface(
55
  fn=gradio_interface,
56
  inputs=gr.Textbox(label="Input Text", placeholder="Enter text to predict political stance..."),
57
- outputs=gr.Textbox(label="Prediction Result"),
58
  title="Political Stance Prediction",
59
  description="Enter a text to predict its political stance (KMT, DPP, Neutral)."
60
  ).launch()
 
2
  import torch.nn as nn
3
  from transformers import AutoTokenizer, AutoModel
4
  import gradio as gr
5
+ import re
6
 
7
  class StanceClassifier(nn.Module):
8
  def __init__(self,transformer_model, num_classes, dropout_rate=0.6):
 
48
  return labels[predicted_class], confidence
49
 
50
  def gradio_interface(text):
51
+ sentences = re.split(r"[。!?\n]", text)
52
+ sentences = [s for s in sentences if s.strip()]
53
+ results = []
54
+ for s in sentences:
55
+ stance, conf = predict_stance(s)
56
+ results.append((s + f" (Confidence: {conf:.4f})", stance))
57
+ return results
58
 
59
  def ui():
60
  gr.Interface(
61
  fn=gradio_interface,
62
  inputs=gr.Textbox(label="Input Text", placeholder="Enter text to predict political stance..."),
63
+ outputs=gr.HighlightedText(label="Prediction Result",color_map={"KMT":"blue","DPP":"green","Neutral":"purple"}),
64
  title="Political Stance Prediction",
65
  description="Enter a text to predict its political stance (KMT, DPP, Neutral)."
66
  ).launch()