cheesecz commited on
Commit
70b07d3
·
verified ·
1 Parent(s): 85018b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -5
app.py CHANGED
@@ -2,17 +2,29 @@ import os
2
  import gradio as gr
3
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
  import torch
5
- import json
6
 
7
  os.environ["TRANSFORMERS_CACHE"] = "/tmp"
8
 
 
 
 
 
9
  MODEL_NAME = "s-nlp/roberta-base-formality-ranker"
10
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
12
 
 
13
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
  model = model.to(device)
15
 
 
 
 
 
 
 
 
 
16
  def predict_formality(text):
17
  # Tokenize input
18
  encoding = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
@@ -23,9 +35,8 @@ def predict_formality(text):
23
  logits = model(**encoding).logits
24
  score = logits.softmax(dim=1)[:, 1].item()
25
 
26
- # Calculate percentages
27
- formal_percent = round(score * 100)
28
- informal_percent = 100 - formal_percent
29
 
30
  # Create response in the new format
31
  response = {
@@ -37,6 +48,7 @@ def predict_formality(text):
37
 
38
  return response
39
 
 
40
  demo = gr.Interface(
41
  fn=predict_formality,
42
  inputs=gr.Textbox(label="Enter your text", lines=3),
@@ -52,4 +64,9 @@ demo = gr.Interface(
52
 
53
  # Launch the app
54
  if __name__ == "__main__":
55
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
2
  import gradio as gr
3
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
  import torch
 
5
 
6
  os.environ["TRANSFORMERS_CACHE"] = "/tmp"
7
 
8
+ # Suppress NVML warning
9
+ os.environ["CUDA_VISIBLE_DEVICES"] = "0"
10
+ torch.cuda.init()
11
+
12
  MODEL_NAME = "s-nlp/roberta-base-formality-ranker"
13
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
14
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
15
 
16
+ # Move model to GPU if available
17
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
18
  model = model.to(device)
19
 
20
+ def calculate_formality_percentages(score):
21
+ # Convert score to grayscale percentage (0-100)
22
+ grayscale = int(score * 100)
23
+ # Use grayscale to determine formal/informal percentages
24
+ formal_percent = grayscale
25
+ informal_percent = 100 - grayscale
26
+ return formal_percent, informal_percent
27
+
28
  def predict_formality(text):
29
  # Tokenize input
30
  encoding = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
 
35
  logits = model(**encoding).logits
36
  score = logits.softmax(dim=1)[:, 1].item()
37
 
38
+ # Calculate percentages using grayscale
39
+ formal_percent, informal_percent = calculate_formality_percentages(score)
 
40
 
41
  # Create response in the new format
42
  response = {
 
48
 
49
  return response
50
 
51
+ # Create Gradio interface
52
  demo = gr.Interface(
53
  fn=predict_formality,
54
  inputs=gr.Textbox(label="Enter your text", lines=3),
 
64
 
65
  # Launch the app
66
  if __name__ == "__main__":
67
+ demo.launch(
68
+ server_name="0.0.0.0",
69
+ server_port=7860,
70
+ share=True,
71
+ enable_queue=True
72
+ )