zeyadusf commited on
Commit
278155c
1 Parent(s): 22f6ab4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -0
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ class TextDetectionApp:
6
+ def __init__(self):
7
+ # Load DeBERTa model and tokenizer
8
+ self.deberta_tokenizer = AutoTokenizer.from_pretrained("zeyadusf/deberta-DAIGT-MODELS")
9
+ self.deberta_model = AutoModelForSequenceClassification.from_pretrained("zeyadusf/deberta-DAIGT-MODELS")
10
+
11
+ # Load RoBERTa model and tokenizer
12
+ self.roberta_tokenizer = AutoTokenizer.from_pretrained("zeyadusf/roberta-DAIGT-kaggle")
13
+ self.roberta_model = AutoModelForSequenceClassification.from_pretrained("zeyadusf/roberta-DAIGT-kaggle")
14
+
15
+ # Load Feedforward model
16
+ self.ff_model = torch.jit.load("model_scripted.pt")
17
+
18
+ def api_huggingface(self, text):
19
+ """
20
+ Generate predictions using the DeBERTa and RoBERTa models.
21
+
22
+ Args:
23
+ text (str): The input text to classify.
24
+
25
+ Returns:
26
+ tuple: Predictions from RoBERTa and DeBERTa models.
27
+ """
28
+ # DeBERTa predictions
29
+ deberta_inputs = self.deberta_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
30
+ deberta_outputs = self.deberta_model(**deberta_inputs)
31
+ deberta_logits = deberta_outputs.logits
32
+ deberta_scores = torch.softmax(deberta_logits, dim=1)
33
+ deberta_predictions = [
34
+ {"label": f"LABEL_{i}", "score": score.item()}
35
+ for i, score in enumerate(deberta_scores[0])
36
+ ]
37
+
38
+ # RoBERTa predictions
39
+ roberta_inputs = self.roberta_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
40
+ roberta_outputs = self.roberta_model(**roberta_inputs)
41
+ roberta_logits = roberta_outputs.logits
42
+ roberta_scores = torch.softmax(roberta_logits, dim=1)
43
+ roberta_predictions = [
44
+ {"label": f"LABEL_{i}", "score": score.item()}
45
+ for i, score in enumerate(roberta_scores[0])
46
+ ]
47
+
48
+ return roberta_predictions, deberta_predictions
49
+
50
+ def generate_ff_input(self, models_results):
51
+ """
52
+ Generates input features for the Feedforward model from the API output.
53
+
54
+ Parameters:
55
+ models_results (tuple): Tuple containing the results of DeBERTa and RoBERTa models.
56
+
57
+ Returns:
58
+ torch.Tensor: Feedforward model input features tensor.
59
+ """
60
+ roberta, deberta = models_results
61
+ input_ff = []
62
+ try:
63
+ if roberta[0]['label'] == 'LABEL_0':
64
+ input_ff.append(roberta[0]['score'])
65
+ input_ff.append(roberta[1]['score'])
66
+ else:
67
+ input_ff.append(roberta[1]['score'])
68
+ input_ff.append(roberta[0]['score'])
69
+
70
+ if deberta[0]['label'] == 'LABEL_0':
71
+ input_ff.append(deberta[0]['score'])
72
+ input_ff.append(deberta[1]['score'])
73
+ else:
74
+ input_ff.append(deberta[1]['score'])
75
+ input_ff.append(deberta[0]['score'])
76
+
77
+ except Exception as e:
78
+ print(f"Error {e}: The text is long")
79
+
80
+ input_ff = torch.tensor(input_ff, dtype=torch.float32)
81
+ input_ff = input_ff.view(1, -1)
82
+ return input_ff
83
+
84
+ def detect_text(self, text):
85
+ """
86
+ Detects whether the input text is generated or human-written using the Feedforward model.
87
+
88
+ Returns:
89
+ float: The detection result.
90
+ """
91
+ with torch.no_grad():
92
+ self.output = self.ff_model(self.generate_ff_input(self.api_huggingface(text)))[0][0].item()
93
+ return self.output
94
+
95
+ def classify_text(self, text, model_choice):
96
+ """
97
+ Classifies the input text using the selected model.
98
+
99
+ Args:
100
+ text (str): The input text to classify.
101
+ model_choice (str): The model to use ('DeBERTa', 'RoBERTa', or 'Feedforward').
102
+
103
+ Returns:
104
+ str: The classification result.
105
+ """
106
+ if model_choice == 'DeBERTa':
107
+ # Tokenize input
108
+ inputs = self.deberta_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
109
+
110
+ # Run model
111
+ outputs = self.deberta_model(**inputs)
112
+
113
+ # Get classification results
114
+ logits = outputs.logits
115
+ predicted_class_id = logits.argmax().item()
116
+ return f"DeBERTa Prediction: Class {predicted_class_id}"
117
+
118
+ elif model_choice == 'RoBERTa':
119
+ # Tokenize input
120
+ inputs = self.roberta_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
121
+
122
+ # Run model
123
+ outputs = self.roberta_model(**inputs)
124
+
125
+ # Get classification results
126
+ logits = outputs.logits
127
+ predicted_class_id = logits.argmax().item()
128
+ return f"RoBERTa Prediction: Class {predicted_class_id}"
129
+
130
+ elif model_choice == 'Feedforward':
131
+ # Run feedforward detection
132
+ detection_score = self.detect_text(text)
133
+ return f"Feedforward Detection Score: {detection_score}"
134
+
135
+ else:
136
+ return "Invalid model selection."
137
+
138
+
139
+ # Initialize the app
140
+ app = TextDetectionApp()
141
+
142
+ # Gradio Interface
143
+ iface = gr.Interface(
144
+ fn=app.classify_text,
145
+ inputs=[
146
+ gr.Textbox(lines=2, placeholder="Enter your text here..."),
147
+ gr.Radio(choices=["DeBERTa", "RoBERTa", "Feedforward"], label="Model Choice")
148
+ ],
149
+ outputs="text",
150
+ title="Text Classification with Multiple Models",
151
+ description="Classify text using DeBERTa, RoBERTa, or a custom Feedforward model."
152
+ )
153
+
154
+ iface.launch()