duclo90 commited on
Commit
86a73fe
Β·
verified Β·
1 Parent(s): 27a564c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -21
app.py CHANGED
@@ -1,33 +1,53 @@
1
- # Install dependencies if not already installed
2
- # !pip install transformers gradio
3
-
4
- from transformers import pipeline
5
  import gradio as gr
 
6
 
7
- # Load your model and tokenizer from Hugging Face
8
- model_name = "duclo90/Semeval" # replace with your HF repo
9
 
10
- classifier = pipeline(
11
- "text-classification",
12
- model=model_name,
13
- tokenizer=model_name
14
- )
15
 
16
- # Function to classify text
 
 
 
17
  def classify_text(text):
18
- result = classifier(text)
19
- label = result[0]['label']
20
- score = round(result[0]['score'], 3)
21
- return f"Prediction: {label} (Confidence: {score})"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Create Gradio interface
24
  iface = gr.Interface(
25
  fn=classify_text,
26
- inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
 
 
 
27
  outputs="text",
28
  title="Human vs Machine Text Classifier",
29
- description="Enter text and the model will predict if it was written by a human or generated by a machine."
30
  )
31
 
32
- # Launch the web app
33
- iface.launch(share=True) # share=True generates a public link
 
1
+ import torch
 
 
 
2
  import gradio as gr
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
 
5
+ # Hugging Face model repo
6
+ MODEL_NAME = "duclo90/Semeval"
7
 
8
+ # Load tokenizer and model explicitly
9
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
 
 
11
 
12
+ # Put model in eval mode
13
+ model.eval()
14
+
15
+ # Inference function
16
  def classify_text(text):
17
+ if not text or text.strip() == "":
18
+ return "Please enter some text."
19
+
20
+ inputs = tokenizer(
21
+ text,
22
+ return_tensors="pt",
23
+ truncation=True,
24
+ padding=True,
25
+ max_length=512,
26
+ )
27
+
28
+ with torch.no_grad():
29
+ outputs = model(**inputs)
30
+ logits = outputs.logits
31
+
32
+ probs = torch.softmax(logits, dim=-1)
33
+ pred_id = torch.argmax(probs, dim=1).item()
34
+
35
+ label = model.config.id2label[pred_id]
36
+ confidence = round(probs[0][pred_id].item(), 3)
37
+
38
+ return f"Prediction: {label} (Confidence: {confidence})"
39
 
40
+ # Gradio UI
41
  iface = gr.Interface(
42
  fn=classify_text,
43
+ inputs=gr.Textbox(
44
+ lines=6,
45
+ placeholder="Enter text here..."
46
+ ),
47
  outputs="text",
48
  title="Human vs Machine Text Classifier",
49
+ description="Detect whether a text is written by a human or generated by a machine."
50
  )
51
 
52
+ # Launch app
53
+ iface.launch()