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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -13
app.py CHANGED
@@ -1,15 +1,15 @@
1
  import gradio as gr
2
- from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
3
  import torch
4
  import re
5
 
6
- # Model ve tokenizer'ı yükleyin
7
- model_name = "your_model_directory" # Model dosyalarının bulunduğu dizini buraya yazın
8
- tokenizer = DistilBertTokenizer.from_pretrained(model_name) # Doğru tokenizer sınıfını kullanın
9
- model = DistilBertForSequenceClassification.from_pretrained(model_name) # Doğru model sınıfını kullanın
10
 
11
  def clean_text(text):
12
- # URL'leri ve özel karakterleri kaldır
13
  text = re.sub(r'http\S+|[^\w\s]', '', text)
14
  return text.strip()
15
 
@@ -18,10 +18,10 @@ def predict(text):
18
  return "Please enter some text"
19
 
20
  try:
21
- # Metni ön işleme tabi tut
22
  text = clean_text(text)
23
 
24
- # Giriş metnini tokenize et
25
  inputs = tokenizer(
26
  text,
27
  return_tensors="pt",
@@ -30,21 +30,21 @@ def predict(text):
30
  max_length=128
31
  )
32
 
33
- # Model tahminlerini al
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
- # Sonucu ve güven oranını döndür
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
- # Gradio arayüzü oluştur
48
  iface = gr.Interface(
49
  fn=predict,
50
  inputs=gr.Textbox(
@@ -67,5 +67,5 @@ iface = gr.Interface(
67
  )
68
  )
69
 
70
- # Uygulamayı başlat
71
- iface.launch(share=True)
 
1
  import gradio as gr
2
+ 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
 
 
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",
 
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
  )
68
  )
69
 
70
+ # Launch with share=True for public URL
71
+ iface.launch(share=True)