tmt3103 commited on
Commit
03adbbe
·
1 Parent(s): 04e1367

update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -20
app.py CHANGED
@@ -16,7 +16,7 @@ def predict_sentiment(text):
16
  encoding = tokenizer_sentiment(text, return_tensors='pt', truncation=True, padding=True, max_length=128)
17
  output = model_sentiment(**encoding)
18
  pred = torch.argmax(output.logits, dim=1).item()
19
- sentiment_labels = ["Negative", "Neutral", "Positive"] # Cập nhật nhãn nếu cần
20
  return sentiment_labels[pred] # Trả về nhãn cảm xúc
21
 
22
  def predict_topic(text):
@@ -25,28 +25,23 @@ def predict_topic(text):
25
  encoding = tokenizer_topic(text, return_tensors='pt', truncation=True, padding=True, max_length=128)
26
  output = model_topic(**encoding)
27
  pred = torch.argmax(output.logits, dim=1).item()
28
- topic_labels = ["Topic 0", "Topic 1", "Topic 2", "Topic 3"] # Cập nhật nhãn nếu cần
29
  return topic_labels[pred] # Trả về nhãn chủ đề
30
 
31
- # Create Gradio Interface
32
- iface_sentiment = gr.Interface(
33
- fn=predict_sentiment,
34
- inputs="text",
35
- outputs="text",
36
- title="Sentiment Classifier",
37
- description="Classify sentiment as Positive, Neutral, or Negative."
38
- )
39
 
40
- iface_topic = gr.Interface(
41
- fn=predict_topic,
42
- inputs="text",
43
- outputs="text",
44
- title="Topic Classifier",
45
- description="Classify the topic of the text."
 
46
  )
47
 
48
- # Create Tabbed Interface
49
- tabbed_interface = gr.TabbedInterface([iface_sentiment, iface_topic], ["Sentiment", "Topic"])
50
-
51
  # Launch the app
52
- tabbed_interface.launch()
 
16
  encoding = tokenizer_sentiment(text, return_tensors='pt', truncation=True, padding=True, max_length=128)
17
  output = model_sentiment(**encoding)
18
  pred = torch.argmax(output.logits, dim=1).item()
19
+ sentiment_labels = ["Tiêu cực", "Trung lập", "Tích cực"]
20
  return sentiment_labels[pred] # Trả về nhãn cảm xúc
21
 
22
  def predict_topic(text):
 
25
  encoding = tokenizer_topic(text, return_tensors='pt', truncation=True, padding=True, max_length=128)
26
  output = model_topic(**encoding)
27
  pred = torch.argmax(output.logits, dim=1).item()
28
+ topic_labels = ["Giảng viên", "Chương trình đào tạo", " sở vật chất", "Khác"]
29
  return topic_labels[pred] # Trả về nhãn chủ đề
30
 
31
+ # Define a function to handle both predictions at once
32
+ def classify_sentiment_and_topic(text):
33
+ sentiment = predict_sentiment(text)
34
+ topic = predict_topic(text)
35
+ return sentiment, topic
 
 
 
36
 
37
+ # Create Gradio Interface
38
+ iface = gr.Interface(
39
+ fn=classify_sentiment_and_topic,
40
+ inputs=gr.Textbox(label="Enter text for Classification", placeholder="Type your text here..."),
41
+ outputs=[gr.Textbox(label="Sentiment Output"), gr.Textbox(label="Topic Output")],
42
+ title="Vietnamese students Feedback Corpus Classifier",
43
+ description="Classify the sentiment and topic of a given text into separate outputs."
44
  )
45
 
 
 
 
46
  # Launch the app
47
+ iface.launch()