Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,71 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
import torch
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def predict(text):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
-
iface = gr.Interface(
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
#
|
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)
|