alperugurcan commited on
Commit
6eba496
·
verified ·
1 Parent(s): 427f408

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -19
app.py CHANGED
@@ -1,26 +1,71 @@
1
  import gradio as gr
2
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
  import torch
 
4
 
5
- model_name = "alperugurcan/nlp-disaster"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
 
 
 
 
 
 
8
 
9
  def predict(text):
10
- # Tokenize the input text
11
- inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
12
- # Get model predictions
13
- with torch.no_grad():
14
- outputs = model(**inputs)
15
- predictions = torch.argmax(outputs.logits, dim=-1)
16
- return "Positive" if predictions.item() == 1 else "Negative"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- # Create a Gradio interface
19
- iface = gr.Interface(fn=predict,
20
- inputs="text",
21
- outputs="text",
22
- title="Sentiment Analysis",
23
- description="Enter text to analyze sentiment (Positive/Negative).")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- # Launch the app
26
- iface.launch()
 
1
  import gradio as gr
2
+ from transformers import PreTrainedTokenizerFast, PreTrainedModel
3
  import torch
4
+ import re
5
 
6
+ # Model ve tokenizer'ı yükleyin
7
+ model_name = "alperugurcan/nlp-disaster" # Model dosyalarının bulunduğu dizini buraya yazın
8
+ tokenizer = PreTrainedTokenizerFast.from_pretrained(model_name) # Tokenizer'ı yükleyin
9
+ model = PreTrainedModel.from_pretrained(model_name) # Modeli yükleyin (modelin hangi sınıf olduğuna bağlı olarak değişebilir)
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
 
16
  def predict(text):
17
+ if not text or len(text.strip()) == 0:
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",
28
+ truncation=True,
29
+ padding=True,
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(
51
+ label="Tweet Text",
52
+ placeholder="Enter a tweet to analyze...",
53
+ lines=3
54
+ ),
55
+ outputs=gr.Textbox(label="Prediction"),
56
+ title="🚨 Disaster Tweet Classifier",
57
+ description="Enter a tweet to determine if it's about a real disaster or not.",
58
+ examples=[
59
+ ["Just happened: Massive earthquake hits California"],
60
+ ["I'm dying to see the new Spider-Man movie!"],
61
+ ["Forest fire spreading rapidly near residential areas"],
62
+ ["This game is a complete disaster lol"]
63
+ ],
64
+ theme=gr.themes.Base(
65
+ primary_hue="red",
66
+ secondary_hue="yellow",
67
+ )
68
+ )
69
 
70
+ # Uygulamayı başlat
71
+ iface.launch(share=True)