alperugurcan commited on
Commit
d9f655b
·
verified ·
1 Parent(s): 81c62ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -19
app.py CHANGED
@@ -3,48 +3,33 @@ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
  import torch
4
  import re
5
 
6
- # Load model and tokenizer
7
  model_name = "alperugurcan/nlp-disaster"
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
10
 
11
  def clean_text(text):
12
- # Remove URLs and special characters
13
- text = re.sub(r'http\S+|[^\w\s]', '', text)
14
- return text.strip()
15
 
16
  def predict(text):
17
  if not text or len(text.strip()) == 0:
18
  return "Please enter some text"
19
 
20
  try:
21
- # Preprocess text
22
  text = clean_text(text)
 
23
 
24
- # Tokenize the input text
25
- inputs = tokenizer(
26
- text,
27
- return_tensors="pt",
28
- truncation=True,
29
- padding=True,
30
- max_length=128
31
- )
32
-
33
- # Get model predictions
34
  with torch.no_grad():
35
  outputs = model(**inputs)
36
  probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
37
  prediction = torch.argmax(outputs.logits, dim=-1)
38
  confidence = probabilities[0][prediction.item()].item()
39
-
40
- # Return result with confidence
41
  result = "Disaster" if prediction.item() == 1 else "Not Disaster"
42
  return f"{result} (Confidence: {confidence:.2%})"
43
 
44
  except Exception as e:
45
  return f"Error in prediction: {str(e)}"
46
 
47
- # Create a Gradio interface with improved styling
48
  iface = gr.Interface(
49
  fn=predict,
50
  inputs=gr.Textbox(
@@ -67,5 +52,4 @@ iface = gr.Interface(
67
  )
68
  )
69
 
70
- # Launch with share=True for public URL
71
  iface.launch(share=True)
 
3
  import torch
4
  import re
5
 
 
6
  model_name = "alperugurcan/nlp-disaster"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
 
10
  def clean_text(text):
11
+ return re.sub(r'http\S+|[^\w\s]', '', text).strip()
 
 
12
 
13
  def predict(text):
14
  if not text or len(text.strip()) == 0:
15
  return "Please enter some text"
16
 
17
  try:
 
18
  text = clean_text(text)
19
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
20
 
 
 
 
 
 
 
 
 
 
 
21
  with torch.no_grad():
22
  outputs = model(**inputs)
23
  probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
24
  prediction = torch.argmax(outputs.logits, dim=-1)
25
  confidence = probabilities[0][prediction.item()].item()
26
+
 
27
  result = "Disaster" if prediction.item() == 1 else "Not Disaster"
28
  return f"{result} (Confidence: {confidence:.2%})"
29
 
30
  except Exception as e:
31
  return f"Error in prediction: {str(e)}"
32
 
 
33
  iface = gr.Interface(
34
  fn=predict,
35
  inputs=gr.Textbox(
 
52
  )
53
  )
54
 
 
55
  iface.launch(share=True)